Skip to content

Commit 0d84cbe

Browse files
committed
Worked on callbacks, function-conversions, array-methods,closures
1 parent 91ff21a commit 0d84cbe

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

assignments/callbacks.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@ 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+
cb(arr[0]);
6+
console.log(cb(arr[0]));
57
}
68

79
function getLength(arr, cb) {
810
// getLength passes the length of the array into the callback.
9-
}
11+
cb(arr.length);}
1012

1113
function last(arr, cb) {
1214
// last passes the last item of the array into the callback.
13-
}
15+
cb(arr[arr.length-1]);}
16+
last(last,console.log)
1417

1518
function sumNums(x, y, cb) {
1619
// sumNums adds two numbers (x, y) and passes the result to the callback.
17-
}
20+
cb(x+y);}
21+
sumNums(items, console.log)
1822

1923
function multiplyNums(x, y, cb) {
2024
// multiplyNums multiplies two numbers and passes the result to the callback.
21-
}
25+
cb(x*y);}
26+
multiplyNums(items,console.log)
2227

2328
function contains(item, list, cb) {
2429
// contains checks if an item is present inside of the given array/list.
2530
// Pass true to the callback if it is, otherwise pass false.
31+
cb(list.includes(item));
2632
}
33+
contains('Pencil', items, console.log)
2734

2835
/* STRETCH PROBLEM */
2936

assignments/closure.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
function deck (){
4+
let lands= 32;
5+
return lands + "Swamp";
6+
function totalLands(){
7+
let nonBasicLands=5;
8+
return lands+nonBasicLands;
9+
}
10+
return nonBasicLands;
11+
}
12+
deck();
413

514
// ==== Challenge 2: Create a counter function ====
615
const counter = () => {

0 commit comments

Comments
 (0)