This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy path2-function-creation.js
More file actions
204 lines (168 loc) · 5.87 KB
/
2-function-creation.js
File metadata and controls
204 lines (168 loc) · 5.87 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Complete the function to return a boolean describing whether a user is acceptable.
To be acceptable, two conditions must be fulfilled
1. the user should be 18 or older
2. the user must be logged in
*/
function isAcceptableUser(userAge, isLoggedIn) {
if (userAge>=18 && isLoggedIn===true){
return true;
}else if (userAge>18 && isLoggedIn===true){
return false;
}else {
return false;
}
}
/*
Complete the function to apply discount percent based on how much is totalPrice in user cart.
- When the total price is greater than 200, a 10% discount should be applied
- When the total price is less than 200, a 5% discount should be applied
The function should return the new price to be paid (e.g. if the totalPrice is 150, a 5% discount
is applieds and 142.5 should be returned)
*/
function applyDiscount(totalPrice) {
if (totalPrice>200){
return totalPrice *0.9;
}else if (totalPrice<200){
return totalPrice * 0.95;
}
}
/*
Complete the function to print to the console the odd numbers between 1 and limit (use a while loop):
*/
function printOddNumbers(limit) {
for (let i = 1; i <= limit; i++) {
if (i % 2 === 1) {
console.log(i);
}
}
}
/*
Complete the buyTwoGetTheCheapestFree function: if user buys two items, the cheapest item will be free!
The function should return the price to be paid once the discount is applied.
*/
function buyTwoGetTheCheapestFree(price1, price2) {
if (price1 <= 0 || price2 <= 0) {
return "Invalid prices. Prices must be more than 0.";
}
if (price1 >= price2) {
return price1;
} else {
return price2;
}
}
/*
Complete the function to determine if it is suitable for a person to register based on their age!
- if the person is 12 or younger it should return "You Are Too Young To Register"
- if the person is older than 12 and younger than 90 it should return "You Can Register"
- if the person is 90 or older it should return "You Don't Need To Register"
*/
function canRegister(age) {
if (age <= 12) {
return "You Are Too Young To Register"
} else if (age > 12 && age < 90) {
return"You Can Register"
}else if (age >= 90) {
return "You Don't Need To Register"
}
}
/*
Complete the function so that it prints out to the console numbers in reverse order starting at
number and going down to 1 (e.g. if number was 3, it would print:
3
2
1
)
*/
function countReverse(number) {
for (let i = number; i >= 1; i--) {
console.log(i);
}
}
/* ======= TESTS - DO NOT MODIFY ===== */
describe("isAcceptableUser", () => {
test("returns true if over 18 and logged in", () => {
expect(isAcceptableUser(21, true)).toEqual(true);
});
test("returns true if 18 and logged in", () => {
expect(isAcceptableUser(18, true)).toEqual(true);
});
test("returns false if under 18 and logged in", () => {
expect(isAcceptableUser(17, true)).toEqual(false);
});
test("returns false if over 18 and not logged in", () => {
expect(isAcceptableUser(21, false)).toEqual(false);
});
});
describe("applyDiscount", () => {
test("returns price with 5% discount", () => {
expect(applyDiscount(120)).toEqual(114);
});
test("returns price with 10% discount", () => {
expect(applyDiscount(280)).toEqual(252);
});
});
describe("printOddNumbers", () => {
test("printOddNumbers function prints odd numbers between 1 and 2", () => {
const consoleLogSpy = jest.spyOn(console, "log");
printOddNumbers(2);
expect(consoleLogSpy).toBeCalledWith(1);
consoleLogSpy.mockRestore();
});
test("printOddNumbers function prints odd numbers between 1 and 10", () => {
const consoleLogSpy = jest.spyOn(console, "log");
printOddNumbers(10);
expect(consoleLogSpy).toHaveBeenNthCalledWith(1,1);
expect(consoleLogSpy).toHaveBeenNthCalledWith(2,3);
expect(consoleLogSpy).toHaveBeenNthCalledWith(3,5);
expect(consoleLogSpy).toHaveBeenNthCalledWith(4,7);
expect(consoleLogSpy).toHaveBeenNthCalledWith(5,9);
consoleLogSpy.mockRestore();
});
});
describe("buyTwoGetTheCheapestFree", () => {
test("buyTwoGetTheCheapestFree function returns first price when it is largest", () => {
expect(buyTwoGetTheCheapestFree(700, 500)).toEqual(700);
});
test("buyTwoGetTheCheapestFree function returns second price when it is largest", () => {
expect(buyTwoGetTheCheapestFree(500, 700)).toEqual(700);
});
});
describe("canRegister", () => {
test("returns in case of a person of age 7", () => {
expect(canRegister(7)).toEqual("You Are Too Young To Register");
});
test("returns in case of a person of age 12", () => {
expect(canRegister(12)).toEqual("You Are Too Young To Register");
});
test("returns in case of a person of age 13", () => {
expect(canRegister(13)).toEqual("You Can Register");
});
test("returns in case of a person of age 89", () => {
expect(canRegister(89)).toEqual("You Can Register");
});
test("returns in case of a person of age 90", () => {
expect(canRegister(90)).toEqual("You Don't Need To Register");
});
test("returns in case of a person of age 112", () => {
expect(canRegister(112)).toEqual("You Don't Need To Register");
});
});
describe("countReverse", () => {
test("countReverse function logs values from 7 to 1", () => {
const consoleLogSpy = jest.spyOn(console, "log");
countReverse(7);
expect(consoleLogSpy).toHaveBeenNthCalledWith(1,7);
expect(consoleLogSpy).toHaveBeenNthCalledWith(2,6);
expect(consoleLogSpy).toHaveBeenNthCalledWith(3,5);
expect(consoleLogSpy).toHaveBeenNthCalledWith(4,4);
expect(consoleLogSpy).toHaveBeenNthCalledWith(5,3);
expect(consoleLogSpy).toHaveBeenNthCalledWith(6,2);
expect(consoleLogSpy).toHaveBeenNthCalledWith(7,1);
consoleLogSpy.mockRestore();
});
});
/*
CHECK OUT solutions.md FOR MORE INFO ON OUR SOLUTION
*/
/* ======= TESTS - DO NOT MODIFY ===== */