You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Write a function called specialMultiply which accepts two parameters. If the function is passed both parameters, it should return the product of the two. If the function is only passed one parameter - it should return a function which can later be passed another parameter to return the product. You will have to use closure and arguments to solve this.
2
2
3
-
specialMultiply(3,4);// 12
3
+
specialMultiply(3,4);// 12
4
4
specialMultiply(3)(4);// 12
5
5
specialMultiply(3);// returns a function
6
6
7
-
functionspecialMultiply(a,b){
8
-
if(arguments.length===1){
9
-
returnfunction(b){
10
-
returna*b;
11
-
}
12
-
}
13
-
returna*b;
14
-
}
7
+
functionspecialMultiply(a,b){}
15
8
16
9
// Write a function called guessingGame which takes in one parameter amount. The function should return another function that takes in a parameter called guess. In the outer function, you should create a variable called answer which is the result of a random number between 0 and 10 as well as a variable called guesses which should be set to 0.
17
10
18
11
// In the inner function, if the guess passed in is the same as the random number (defined in the outer function) - you should return the string "You got it!". If the guess is too high return "Your guess is too high!" and if it is too low, return "Your guess is too low!". You should stop the user from guessing if the amount of guesses they have made is greater than the initial amount passed to the outer function.
19
12
20
13
// You will have to make use of closure to solve this problem.
21
14
22
-
functionguessingGame(amount){
23
-
varanswer=Math.floor(Math.random()*11);
24
-
varguesses=0;
25
-
varcompleted=false;
26
-
returnfunction(guess){
27
-
if(!completed){
28
-
guesses++
29
-
if(guess===answer){
30
-
completed=true;
31
-
return"You got it!"
32
-
}
33
-
elseif(guess>answer)return"Your guess is too high!"
34
-
elseif(guess<answer)return"Your guess is too low!"
0 commit comments