From 2e8c33a450dd6bbb364a7933e16e2785c4c46cb4 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Sun, 11 Oct 2020 15:15:31 +0530 Subject: [PATCH 01/11] added: new question left --- 1-hoisting/README.md | 4 ++ 2-higher-order-functions/eloquent/exercise.js | 22 ++++++- 2-higher-order-functions/exercise.js | 61 ++++++++++++++++--- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/1-hoisting/README.md b/1-hoisting/README.md index f28a371..f6297e0 100644 --- a/1-hoisting/README.md +++ b/1-hoisting/README.md @@ -18,6 +18,10 @@ function sayHello(name) { let message = sayHello(username); var nextMessage = sayHello('Test'); ``` +//- re-write +```js +var username = un +``` 2. diff --git a/2-higher-order-functions/eloquent/exercise.js b/2-higher-order-functions/eloquent/exercise.js index 01f23cb..8724254 100644 --- a/2-higher-order-functions/eloquent/exercise.js +++ b/2-higher-order-functions/eloquent/exercise.js @@ -2,12 +2,24 @@ let arrays = [[1, 2, 3], [4, 5], [6]]; // Your code here. +arrays.reduce((a, c) => a.concat(c), []); // → [1, 2, 3, 4, 5, 6] // Challenge 2. Your own loop -// Your code here. loop(3, n => n > 0, n => n - 1, console.log); +// Your code here. + +function loop(num, test, test2, op){ + for(let i = 0; i <= num; i++){ + if(test(i)){ + op(i); + num = test2(num) + + } + } +} + // → 3 // → 2 // → 1 @@ -15,7 +27,13 @@ loop(3, n => n > 0, n => n - 1, console.log); // Challenge 3. Everything function every(array, test) { // Your code here. -} + let result = true + for(let item of array){ + result = result && test(item) + } + return result; + } + console.log(every([1, 3, 5], n => n < 10)); // → true diff --git a/2-higher-order-functions/exercise.js b/2-higher-order-functions/exercise.js index 927e3d3..60e6632 100644 --- a/2-higher-order-functions/exercise.js +++ b/2-higher-order-functions/exercise.js @@ -1,24 +1,39 @@ // Challenge 1 -function addTwo(num) {} +function addTwo(num) { + return num + 2; +} // To check if you've completed it, uncomment these console.logs! // console.log(addTwo(3)); // console.log(addTwo(10)); // Challenge 2 -function addS(word) {} +function addS(word) { + return word + "s"; +} // uncomment these to check your work // console.log(addS('pizza')); // console.log(addS('bagel')); // Challenge 3 -function map(array, callback) {} +function map(array, callback) { + let newAr = []; + for (let item of array) { + newAr.push(callback(item)); + } + return newAr +} // console.log(map([1, 2, 3], addTwo)); // Challenge 4 -function forEach(array, callback) {} +function forEach(array, callback) { + for (let item of array) { + callback(item); + } +} + // see for yourself if your forEach works! @@ -27,21 +42,49 @@ function forEach(array, callback) {} //-------------------------------------------------- //Extension 1 -function mapWith(array, callback) {} +function mapWith(array, callback) { + let newAr = []; + array.forEach(item => { + console.log(item) + newAr.push(callback(item)) + }); + return newAr +} //Extension 2 -function reduce(array, callback, initialValue) {} +function reduce(array, callback, initialValue) { + let result = initialValue; + array.forEachO(array, (e) => { + result = callback(result, e); + }); + return result; +} //Extension 3 -function intersection(arrays) {} +function intersection(arrays) { + + +} // console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20])); // should log: [5, 15] //Extension 4 -function union(arrays) {} +function union(...arrays) { + let newAr = []; + arrays = arrays.reduce((a, c) => a.concat(c)); + arrays.forEach((num) => { + if (!newAr.includes(num)) { + newAr.push(num) + } + }) + + + return newAr +} // console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5])); + // should log: [5, 10, 15, 88, 1, 7, 100] //Extension 5 @@ -54,4 +97,4 @@ function objOfMatches(array1, array2, callback) {} function multiMap(arrVals, arrCallbacks) {} // console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }])); -// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } +// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } \ No newline at end of file From 53b77cf7af14d33365bebec8220f63ee313fba6e Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Sun, 11 Oct 2020 19:40:20 +0530 Subject: [PATCH 02/11] added: update --- 2-higher-order-functions/exercise.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/2-higher-order-functions/exercise.js b/2-higher-order-functions/exercise.js index 60e6632..1a16d21 100644 --- a/2-higher-order-functions/exercise.js +++ b/2-higher-order-functions/exercise.js @@ -62,8 +62,14 @@ function reduce(array, callback, initialValue) { //Extension 3 function intersection(arrays) { - - + return arrays.reduce((a, c) => { + a.forEach((e, i) => { + if(!c.includes(e)){ + a.splice(i, 1); + } + }) + return a; + }) } // console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20])); @@ -77,9 +83,7 @@ function union(...arrays) { if (!newAr.includes(num)) { newAr.push(num) } - }) - - + }); return newAr } @@ -88,13 +92,23 @@ function union(...arrays) { // should log: [5, 10, 15, 88, 1, 7, 100] //Extension 5 -function objOfMatches(array1, array2, callback) {} +function objOfMatches(array1, array2, callback) { + let ob = {}; + array1.forEach((word) => { + if (array2.includes(callback(word))) { + ob[word] = callback(word) + } + }) + return ob; +} // console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); })); // should log: { hi: 'HI', bye: 'BYE', later: 'LATER' } //Extension 6 -function multiMap(arrVals, arrCallbacks) {} +function multiMap(arrVals, arrCallbacks) { + +} // console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }])); // should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } \ No newline at end of file From 8e43511385b8bf559ab844793086d1c8ab2b1292 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Mon, 12 Oct 2020 02:00:06 +0530 Subject: [PATCH 03/11] Added: completed few question left --- 3-closure/closure.js | 39 +++++++++++++++++++++++++++++++++++++++ practice/closure.md | 26 +++++++++++++++++++++++++- practice/hoisting.md | 9 ++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/3-closure/closure.js b/3-closure/closure.js index e69de29..1e7004a 100644 --- a/3-closure/closure.js +++ b/3-closure/closure.js @@ -0,0 +1,39 @@ +// first +/* function nonesense(string){ + var blab = function(){ + alert(string) + } + return blab(); + +} */ +//second +/* +function nonesense(string){ + var blab = function(){ + alert(string) + } + setTimeout(blab(), 2000) +} + */ + +//third +function nonesense(string){ + var blab = function(){ + alert(string) + } + return blab; +} + +let blabLater = nonesense('Ali'); +let blabAgainLater = nonesense('Rai'); +console.log(blabLater, blabAgainLater); + + +function FullName(firstName){ + return function(lastName){ + console.log(`${firstName} ${lastName}`); + }; +} +let name = FullName('Shadab'); +name('Ali'); + diff --git a/practice/closure.md b/practice/closure.md index 80cec16..35738ec 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -4,6 +4,11 @@ ```js // Your code goes here +function multiplyBy(num){ + return function(num1){ + return num * num1 + } +} const double = multiplyBy(2); const final = double(15); // final should be 30 @@ -13,7 +18,11 @@ const final = double(15); // final should be 30 ```js // Your code goes here - +function fullName(firstName){ + return function(lastName){ + return firstName + " " +lastName + } +} const name = fullName("Will"); const final = name("Smith"); // final should be "Will Smith" ``` @@ -23,6 +32,14 @@ const final = name("Smith"); // final should be "Will Smith" ```js function isInBetween(a, b) { // your code goes here + return function(num){ + if(num => a && num <= b){ + return true + }else{ + return false + } + } + } const isChild = isInBetween(10, 100); @@ -36,6 +53,9 @@ isChild(103); // false ```js function letsWishThem(greeting) { // your code goes here + return function(message){ + return greeting+" "+message + } } const callWithHey = letsWishThem("Hey"); @@ -49,6 +69,10 @@ callWithHello("How Are You?"); // Hello How Are You? ```js function addGame(gameName) { // your code goes here +let score = 0; +return function(){ + return score++ +} } // Output diff --git a/practice/hoisting.md b/practice/hoisting.md index 18c9f2b..051a4c9 100644 --- a/practice/hoisting.md +++ b/practice/hoisting.md @@ -7,18 +7,20 @@ console.log(animal); var animal = "monkey"; // Output or Error Message ``` +Error: undefined; ```js console.log(animal); let animal = "monkey"; // Output or Error Message ``` - +Error: error: Uncaught ReferenceError: Cannot access 'animal' before initialization; ```js console.log(animal); const animal = "monkey"; // Output or Error Message ``` +Error: Uncaught ReferenceError: Cannot access 'animal' before initialization ```js function sayHello(msg) { @@ -27,6 +29,7 @@ function sayHello(msg) { sayHello("Hey Everyone"); // Output or Error Message ``` +Output: "Hey Everyone" ```js sayHello("Hey Everyone"); @@ -35,6 +38,7 @@ function sayHello(msg) { } // Output or Error Message ``` +output: 'Hey Everyone' ```js sayHello("Hey Everyone"); @@ -43,6 +47,7 @@ var sayHello = msg => { }; // Output or Error Message ``` +Error: Uncaught TypeError: sayHello is not a function ```js sayHello("Hey Everyone"); @@ -50,3 +55,5 @@ let sayHello = msg => { alert(msg); }; ``` +Error: Uncaught ReferenceError: Cannot access 'sayHello' before initialization + From c173b4b26b288330ed1c35a08e9b545c74288dc1 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Mon, 12 Oct 2020 02:04:40 +0530 Subject: [PATCH 04/11] update: --- practice/closure.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/practice/closure.md b/practice/closure.md index 35738ec..35ecc2d 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -89,6 +89,15 @@ cricket(); // Your score of Cricket is 2 ```js function getCard(suit) { // your code goes here + const options = ["club", "spade", "heart", "dimond"] + if(options.includes(suit.toLowerCase())) { + const cards = [2,3,4,5,6,7,8,9,10,"J", "Q", "K", "A"]; + return function(){ + const random = Math.floor(Math.random() * cards.length); + return `Card is: ${cards[random]} ${suit}`; + } + }s + } // Output From 7ef462ffd219f784e7c5b17195212eff5a692bbb Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 08:30:34 +0530 Subject: [PATCH 05/11] updated: hoisting task --- 1-hoisting/README.md | 202 ++++++++++++++++++++++++++- 2-higher-order-functions/exercise.js | 4 +- 3-closure/closure.js | 16 +++ 3 files changed, 216 insertions(+), 6 deletions(-) diff --git a/1-hoisting/README.md b/1-hoisting/README.md index f6297e0..ea074c6 100644 --- a/1-hoisting/README.md +++ b/1-hoisting/README.md @@ -20,7 +20,23 @@ var nextMessage = sayHello('Test'); ``` //- re-write ```js -var username = un +// Declaration Phase +username = undefined; +brother; + +function sayHello(name){ + return `Hello ${name}` +} +message; +nextMessage = undefined; + + // Execution Phase + username = 'Arya'; + brothers = ['John', 'Ryan', 'Bran']; + console.log(username, brothers[0]); // 'Arya', 'John'; + +message = sayHello(username); +nextMessage = sayHello('Test'); ``` 2. @@ -39,6 +55,22 @@ let message = sayHello(username); var nextMessage = sayHello('Test'); ``` +```js +// Declaration Phase + +username = undefined; +number; +function sayHello(name){ + return `Hello ${name}`; +} +message; +nextMessage = undefined; + +Execution Phase +console.log(username, number); // ReferenceError: Can not access 'number before initialization + +``` + 3. ```js @@ -55,6 +87,19 @@ let message = sayHello(username); var nextMessage = sayHello('Test'); ``` +```js +// Declaration Phase +username; +number; +sayHello; +message; +nextMessage = undefined; + +//Execution Phase + +console.log(username, number) // ReferenceError: Cannot access 'username' before initialization +``` + 4. ```js @@ -71,6 +116,19 @@ let sayHello = function (name) { var nextMessage = sayHello('Test'); ``` +```js +// Declaration Phase +username = 'Arya'; +number = 21; +message; +sayHello; +nextMessage = undefined; + +//Execution Phase +username = 'Arya'; +console.log(username,number) // ReferenceError: Cannot access 'number' before initialization +``` + 5. ```js @@ -79,7 +137,13 @@ console.log(age); var name = 'Lydia'; let age = 21; ``` - +```js +name = undefined; +age; +// Execution Phase +console.log(name) // undefined +console.log(age) // ReferenceError: Cannot access 'age' before initialization +``` 6. ```js @@ -91,6 +155,28 @@ function sayHi(name) { } sayHi(); +``` +```js +// Declaration Phase +function sayHi(name){ + console.log(name); + console.log(age); + var name = 'Lydia'; + let age = 21 +} + +//Execution Phase + +//F.E.C + +//D +name = undefined; +age; + +//Execution Phase +console.log(name) // undefined +console.log(age) // ReferenceError: Cannot access 'age' before initialization + ``` 6. @@ -105,6 +191,24 @@ function sayHi(name) { } ``` +```js +// Declaration Phase +function sayHi(name) { + console.log(name); + console.log(age); + var name = 'Lydia'; + let age = 21; +} +//Execution Phase +//F.E.C +//Declaration Phase +name = undefined; +age; + +//Execution Phase + console.log(name) // undefined; + console.log(age) // ReferenceError: Cannot access 'age' before initialization +``` 7. ```js @@ -116,7 +220,24 @@ let sayHi = function sayHi(name) { let age = 21; }; ``` - +```js +// Declaration Phase +function sayHi(name) { + console.log(name); + console.log(age); + var name = 'Lydia'; + let age = 21; +} +//Execution Phase +//F.E.C +//Declaration Phase +name = undefined; +age; + +//Execution Phase + console.log(name) // undefined; + console.log(age) // ReferenceError: Cannot access 'age' before initialization +``` 8. ```js @@ -124,6 +245,18 @@ let num1 = 21; console.log(sum); var sum = num1 + num2; let num2 = 30; +``` +```js +//Declaration Phase + +num1; +sum = undefined; +num2; + +//Execution Phase +num1 = 21; +// ReferenceError: num2 is not defined + ``` 9. @@ -136,7 +269,7 @@ let sum2 = addAgain(num1, num2, 4, 5, 6); let add = (a, b, c, d, e) => { return a + b + c + d + e; }; -function addAgian(a, b) { +function addAgain(a, b) { return a + b; } let num2 = 200; @@ -144,6 +277,46 @@ let num2 = 200; let sum = add(num1, num2, 4, 5, 6); ``` +```js + +num1 = undefined; +sum2; +add; +function addAgain(){ + return a + b; +} +num2; +sum; +//Execution Phase +num1 = 21; +sum2 = // function call + // F.E.C sum2 + //F.E.C D + a = undefined; + b = undefined; + // + //F.E.C E + a = 21; + b = num2; + // num2 is not defined + +add = // function call +//F.E.C +//Declaration Phase + a = undefined; + b = undefined; + c = undefined; + d = undefined; + e = undefined; + // Execution Phase + a = 21; + b=200; + c=4; + d=5; + e=6; + return a+b+c+d+e // 236 +sum = 236; +``` 10. ```js @@ -159,6 +332,27 @@ let add = (a, b) => { }; ``` +```js +// Declaration Phase +function test(a) { + let num1 = 21; + return add(a, num1); +} +sum; +add; +// Execution Phase +sum = //function call + // F.E.C + // F.E.C D + a = undefined; + b = undefined; + // F.E.C E + a = 100; + b = 21; + return // 121 + + + ``` 11. ```js diff --git a/2-higher-order-functions/exercise.js b/2-higher-order-functions/exercise.js index 1a16d21..9f43655 100644 --- a/2-higher-order-functions/exercise.js +++ b/2-higher-order-functions/exercise.js @@ -54,7 +54,7 @@ function mapWith(array, callback) { //Extension 2 function reduce(array, callback, initialValue) { let result = initialValue; - array.forEachO(array, (e) => { + array.forEach(array, (e) => { result = callback(result, e); }); return result; @@ -63,7 +63,7 @@ function reduce(array, callback, initialValue) { //Extension 3 function intersection(arrays) { return arrays.reduce((a, c) => { - a.forEach((e, i) => { + a.forEach((e, i) => { if(!c.includes(e)){ a.splice(i, 1); } diff --git a/3-closure/closure.js b/3-closure/closure.js index 1e7004a..34be31a 100644 --- a/3-closure/closure.js +++ b/3-closure/closure.js @@ -37,3 +37,19 @@ function FullName(firstName){ let name = FullName('Shadab'); name('Ali'); +function storyWriter(){ + let story; + return { + addWords: function(sen){ + return story + }, + erase: function(){ + story = ''; + return story + } + }; +} +var storyOfMyLife = storyWriter(); +storyOfMyLife.addWords('My code broke.'); // 'My code broke.' +storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.' + From 48e5ade77cc776a7ae7c4a28610f9d174a5ad32d Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 08:39:39 +0530 Subject: [PATCH 06/11] All task completed --- 1-hoisting/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/1-hoisting/README.md b/1-hoisting/README.md index ea074c6..40302e7 100644 --- a/1-hoisting/README.md +++ b/1-hoisting/README.md @@ -367,3 +367,27 @@ function add(a, b) { return a + b; } ``` +```js +// Declaration Phase +function test(a) { + let num1 = 21; + return add(a, num1); +} +sum; +function add(a, b) { + return a + b; +} +// Execution Phase +sum = //function call + //F.E.C + //F.E.C D + a = undefined; + num1; + + // F.E.C E + a = 100; + num1 = 21; + return 121 + +sum = 121 +``` \ No newline at end of file From 6634fc1d8146084dd2790db33b42cce045b70947 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 08:41:13 +0530 Subject: [PATCH 07/11] added: --- 2-higher-order-functions/exercise.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/2-higher-order-functions/exercise.js b/2-higher-order-functions/exercise.js index 9f43655..63323aa 100644 --- a/2-higher-order-functions/exercise.js +++ b/2-higher-order-functions/exercise.js @@ -107,8 +107,9 @@ function objOfMatches(array1, array2, callback) { //Extension 6 function multiMap(arrVals, arrCallbacks) { - + return arrVals.reduce((acc, cv) => { + acc[cv] = arrCallbacks.map(fn => fn(cv)) + }, {}) } - // console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }])); // should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } \ No newline at end of file From 395252d41606d4ba5c899b94b528b4a0291a3de4 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 11:58:40 +0530 Subject: [PATCH 08/11] Update closure.md --- practice/closure.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/practice/closure.md b/practice/closure.md index 35ecc2d..612f483 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -87,18 +87,6 @@ cricket(); // Your score of Cricket is 2 6. Write a function called `getCard` which takes one of these options (club, spade, heart, dimond) returns a function calling that function returns random card (2,3,4,5,6,7,8,9,10,J, Q, K, A) of that suit. ```js -function getCard(suit) { - // your code goes here - const options = ["club", "spade", "heart", "dimond"] - if(options.includes(suit.toLowerCase())) { - const cards = [2,3,4,5,6,7,8,9,10,"J", "Q", "K", "A"]; - return function(){ - const random = Math.floor(Math.random() * cards.length); - return `Card is: ${cards[random]} ${suit}`; - } - }s - -} // Output const randomClub = addGame("Club"); From c9b9ba12ae34fa8d28c27195e2bd928d6532a2a5 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 12:21:58 +0530 Subject: [PATCH 09/11] update : new name change --- 2-higher-order-functions/exercise.js | 10 +++++----- 3-closure/closure.js | 2 +- practice/closure.md | 16 ++++------------ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/2-higher-order-functions/exercise.js b/2-higher-order-functions/exercise.js index 63323aa..b1946c7 100644 --- a/2-higher-order-functions/exercise.js +++ b/2-higher-order-functions/exercise.js @@ -53,9 +53,9 @@ function mapWith(array, callback) { //Extension 2 function reduce(array, callback, initialValue) { - let result = initialValue; - array.forEach(array, (e) => { - result = callback(result, e); + let result = array[0] || initialValue; + array.forEach(array, (item) => { + result = callback(result, item); }); return result; } @@ -93,13 +93,13 @@ function union(...arrays) { //Extension 5 function objOfMatches(array1, array2, callback) { - let ob = {}; + let finalOb = {}; array1.forEach((word) => { if (array2.includes(callback(word))) { ob[word] = callback(word) } }) - return ob; + return finalOb; } // console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); })); diff --git a/3-closure/closure.js b/3-closure/closure.js index 34be31a..d01d307 100644 --- a/3-closure/closure.js +++ b/3-closure/closure.js @@ -25,7 +25,7 @@ function nonesense(string){ } let blabLater = nonesense('Ali'); -let blabAgainLater = nonesense('Rai'); +let blabAgainLater = nonesense('Ali'); console.log(blabLater, blabAgainLater); diff --git a/practice/closure.md b/practice/closure.md index 35ecc2d..9e7bf44 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -87,19 +87,11 @@ cricket(); // Your score of Cricket is 2 6. Write a function called `getCard` which takes one of these options (club, spade, heart, dimond) returns a function calling that function returns random card (2,3,4,5,6,7,8,9,10,J, Q, K, A) of that suit. ```js -function getCard(suit) { - // your code goes here - const options = ["club", "spade", "heart", "dimond"] - if(options.includes(suit.toLowerCase())) { - const cards = [2,3,4,5,6,7,8,9,10,"J", "Q", "K", "A"]; - return function(){ - const random = Math.floor(Math.random() * cards.length); - return `Card is: ${cards[random]} ${suit}`; - } - }s - +function getCard(){ + let cards= ['club', 'spade', 'heart', 'dimod']; + } - +``` // Output const randomClub = addGame("Club"); randomClub(); // Card is: 6 Club From 369276ec0a516b420887a3c8f85894e44f7e2483 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Tue, 13 Oct 2020 12:29:06 +0530 Subject: [PATCH 10/11] update: few name and function --- practice/closure.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/practice/closure.md b/practice/closure.md index 9e7bf44..d00f28f 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -87,10 +87,14 @@ cricket(); // Your score of Cricket is 2 6. Write a function called `getCard` which takes one of these options (club, spade, heart, dimond) returns a function calling that function returns random card (2,3,4,5,6,7,8,9,10,J, Q, K, A) of that suit. ```js -function getCard(){ - let cards= ['club', 'spade', 'heart', 'dimod']; - +function getCard(suit) { + return function () { + let arr = [2,3,4,5,6,7,8,9,10,"J", "Q", "K", "A"]; + randomNum = Math.floor(Math.random() * 12); + return `Card is: ${arr[randomNum]} ${suit}`; + } } + ``` // Output const randomClub = addGame("Club"); From abf12906f0ac815f6c8ba367f728f2d4f967bd84 Mon Sep 17 00:00:00 2001 From: Shadab Ali Date: Wed, 14 Oct 2020 07:44:10 +0530 Subject: [PATCH 11/11] update --- practice/closure.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/practice/closure.md b/practice/closure.md index d93198b..5bd5b97 100644 --- a/practice/closure.md +++ b/practice/closure.md @@ -87,7 +87,13 @@ cricket(); // Your score of Cricket is 2 6. Write a function called `getCard` which takes one of these options (club, spade, heart, dimond) returns a function calling that function returns random card (2,3,4,5,6,7,8,9,10,J, Q, K, A) of that suit. ```js - +function getCard(suit) { + return function () { + let arr = [2,3,4,5,6,7,8,9,10,"J", "Q", "K", "A"]; + randomNum = Math.floor(Math.random() * 12); + return `Card is: ${arr[randomNum]} ${suit}`; + } +} ``` // Output const randomClub = addGame("Club");