-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.js
92 lines (78 loc) · 2.86 KB
/
exercise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
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.
Examples:
specialMultiply(3,4); // 12
specialMultiply(3)(4); // 12
specialMultiply(3); // function(){}....
*/
function specialMultiply(a,b){
if( a && b ) {
return a * b;
}
var numOne = a || b;
return function (numTwo) {
return numOne * numTwo;
}
}
// console.log(specialMultiply(3,4));
// console.log(specialMultiply(3)(4));
// console.log(specialMultiply(3));
//OR
function specialMultiply(a,b){
if (arguments.length === 1) {
return function(b) {
return a * b;
}
}
return a * b;
}
/*
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.
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.
You will have to make use of closure to solve this problem.
Examples (yours might not be like this, since the answer is random every time):
var game = guessingGame(5)
game(1) // "You're too low!"
game(8) // "You're too high!"
game(5) // "You're too low!"
game(7) // "You got it!"
game(1) // "You are all done playing!"
var game2 = guessingGame(3)
game2(5) // "You're too low!"
game2(3) // "You're too low!"
game2(1) // "No more guesses the answer was 0"
game2(1) // "You are all done playing!"
*/
function guessingGame(amount){
var guesses = 0;
var answer = Math.floor((Math.random() * 10) + 1);
return function (guess) {
if ( guess === answer && guesses < amount) {
guesses = amount;
return "You got it";
} else if ( guess > answer && guesses < amount ) {
guesses ++;
return "Your guess is too high!";
} else if ( guess < answer && guesses < amount ) {
guesses ++;
return "Your guess is too low!";
} else {
return "The answer was " + answer + "! You are all done playing!";
}
}
}
// var game = guessingGame(5);
// console.log(game(3));