Skip to content

Commit 8180871

Browse files
committed
callbacks
1 parent a6f4a68 commit 8180871

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

assignments/arrays.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year"
6363

6464
// ==== Challenge 1 ====
6565
// The dealer can't recall the information for a car with an id of 33 on his lot. Help the dealer find out which car has an id of 33 by logging the car's year, make, and model in the console log provided to you below:
66-
console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*` );
66+
console.log(`Car 33 is a ${inventory[32].car_year} ${inventory[32].car_make} ${inventory[32].car_model}`);
6767

6868
console.log(inventory[32].id, inventory[32].car_make, inventory[32].car_model)
6969

assignments/callbacks.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,43 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
22

33
function firstItem(arr, cb) {
44
// firstItem passes the first item of the given array to the callback function.
5+
const firstItemArr = arr[0];
6+
return firstItemArr;
57
}
68

79
function getLength(arr, cb) {
810
// getLength passes the length of the array into the callback.
11+
const arrayLength = arr.length;
12+
return arrayLength;
913
}
1014

1115
function last(arr, cb) {
1216
// last passes the last item of the array into the callback.
17+
const lastItem = arr[arr.length - 1];
18+
return lastItem;
1319
}
1420

1521
function sumNums(x, y, cb) {
1622
// sumNums adds two numbers (x, y) and passes the result to the callback.
23+
const sumNums = x + y;
24+
return sumNums;
1725
}
1826

1927
function multiplyNums(x, y, cb) {
2028
// multiplyNums multiplies two numbers and passes the result to the callback.
29+
const multiplyNums = x * y;
30+
return multiplyNums;
2131
}
2232

2333
function contains(item, list, cb) {
2434
// contains checks if an item is present inside of the given array/list.
2535
// Pass true to the callback if it is, otherwise pass false.
36+
for (let i = 0; i < list.length; i++ ) {
37+
if (item === list[i]){
38+
return true;
39+
}
40+
}
41+
return false;
2642
}
2743

2844
/* STRETCH PROBLEM */
@@ -31,4 +47,14 @@ function removeDuplicates(array, cb) {
3147
// removeDuplicates removes all duplicate values from the given array.
3248
// Pass the duplicate free array to the callback function.
3349
// Do not mutate the original array.
50+
const noDups = array.map(item => {
51+
for (let i = 0; i < array.length; i++ ) {
52+
if (item === array[i]) {
53+
array.splice(i, 0);
54+
}
55+
return item;
56+
}
57+
return item;
58+
})
59+
return noDups;
3460
}

0 commit comments

Comments
 (0)