Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit 22873f7

Browse files
week2Exercises file is added!
1 parent b4eafbe commit 22873f7

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

mandatory/week2Exercises.js

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
2+
/*
3+
Fizz-Buzz Game: write the program that prints the numbers from 20 to 35 and for
4+
multiples of ‘3’ print “Fizz” instead of the number,
5+
and for the multiples of ‘5’ print “Buzz”,
6+
and for multiples of '5' and '3' print "FizzBuzz".
7+
//* this exercise is for practicing "Conditionals: if-without-else , for" and "Logical Operators" and also "Comparison : ==="!
8+
*/
9+
function fizzBuzz (number){
10+
if (number % 3 === 0 && number % 5 === 0) return "FizzBuzz";
11+
if (number % 3 === 0) return "Fizz";
12+
if (number % 5 === 0) return "Buzz";
13+
return number;
14+
}
15+
console.log(fizzBuzz(1));
16+
console.log(fizzBuzz(3));
17+
console.log(fizzBuzz(5));
18+
console.log(fizzBuzz(15));
19+
20+
// for (let i = 20 ; i<= 35 ; i++){
21+
// console.log(fizzBuzz(i));
22+
// }
23+
// let j = 20;
24+
// while(j<=35){
25+
// console.log(fizzBuzz(j));
26+
// j+=1;
27+
// }
28+
29+
/*
30+
complete function to check if user is acceptable or not acceptable (+18 years old is acceptable!):
31+
*/
32+
function isAcceptableUser(userAge,isLoggedIn){
33+
let isAcceptable = userAge>18 && isLoggedIn;
34+
return(isAcceptable);
35+
}
36+
console.log(isAcceptableUser(21,true));
37+
38+
/*
39+
complete function to apply discount percent based on how much is totalPrice in user cart.
40+
*/
41+
42+
function applyDiscount(totalPrice){
43+
let discountPercent;
44+
if (totalPrice>200) {
45+
discountPercent = 10;
46+
}else{
47+
discountPercent = 5;
48+
}
49+
return (totalPrice-(totalPrice*discountPercent)/100);
50+
}
51+
applyDiscount(120);
52+
applyDiscount(280);
53+
54+
/*
55+
complete function to print odd numbers between 1 to limitNumber with while loop:
56+
*/
57+
function printOddNumbers(limit){
58+
let i = 1;
59+
while(i<=limit){
60+
61+
if(i%2!==0){
62+
console.log(i);
63+
}
64+
i+=1;
65+
}
66+
}
67+
printOddNumbers(10);
68+
69+
/*
70+
complete buyTwoGetTheCheapestFree Function: if user buy two items, cheapest item will be free!
71+
*/
72+
function buyTwoGetTheCheapestFree(price1,price2){
73+
if (price1>price2) return price1;
74+
return price2;
75+
}
76+
buyTwoGetTheCheapestFree(700,500);
77+
/*
78+
a function that check if user selected a color apply that else apply default color:
79+
*/
80+
function productColor(selectedColor){
81+
82+
let defaulColor = "purple";
83+
return selectedColor||defaulColor;
84+
}
85+
productColor("pink");
86+
productColor();
87+
88+
/*
89+
complete function to print mood: happy/not happy.
90+
*/
91+
function mood(isHappy) {
92+
if (isHappy) {
93+
return 'I am happy';
94+
} else {
95+
return 'I am not happy';
96+
}
97+
}
98+
/*
99+
complete function to determine if it is suitable for person to register based on his age!
100+
*/
101+
function canRegister(age) {
102+
if (age<12){
103+
return "You Are Too Young To Register";
104+
}
105+
else if(age>12 && age<90){
106+
return "You Can Register";
107+
}
108+
else{
109+
return "You Don't Need To Register";
110+
}
111+
}
112+
113+
function countReverse(num){
114+
while(num>0){
115+
console.log(num);
116+
num = num-1;
117+
}
118+
}
119+
countReverse(20);
120+
121+
122+
123+
/* ======= TESTS - DO NOT MODIFY ===== */
124+
125+
126+
test("FizzBuzz function return fizz or buzz or fizzbuzz or number ", () => {
127+
expect(fizzBuzz(1)).toEqual(1);
128+
});
129+
test("FizzBuzz function return fizz or buzz or fizzbuzz or number ", () => {
130+
expect(fizzBuzz(3)).toEqual("Fizz");
131+
});
132+
test("FizzBuzz function return fizz or buzz or fizzbuzz or number ", () => {
133+
expect(fizzBuzz(5)).toEqual("Buzz");
134+
});
135+
test("FizzBuzz function return fizz or buzz or fizzbuzz or number ", () => {
136+
expect(fizzBuzz(15)).toEqual("FizzBuzz");
137+
});
138+
139+
test("isAcceptableUser function retern user is allowed or not", () => {
140+
expect(isAcceptableUser(21,true)).toEqual(true,false);
141+
});
142+
143+
test("applyDiscount function retern price after discount", () => {
144+
expect(applyDiscount(120)).toEqual(114);
145+
});
146+
test("(applyDiscount function retern price after discount", () => {
147+
expect(applyDiscount(280)).toEqual(252);
148+
});
149+
150+
function expectprintOddNumbersToLog(expectedValues) {
151+
const consoleLogSpy = jest.spyOn(console, 'log');
152+
printOddNumbers(10);
153+
expect(consoleLogSpy).toBeCalledTimes(5);
154+
expectedValues.forEach((value, i) => {
155+
expect(consoleLogSpy).nthCalledWith(i+1, value);
156+
});
157+
consoleLogSpy.mockRestore();
158+
};
159+
160+
test("(buyTwoGetTheCheapestFree function retern maximum price to pay", () => {
161+
expect(buyTwoGetTheCheapestFree(700,500)).toEqual(700);
162+
});
163+
164+
test("productColor function retern user-selected color or default color", () => {
165+
expect(productColor("pink")).toEqual("pink");
166+
});
167+
test("productColor function retern user-selected color or default color", () => {
168+
expect(productColor()).toEqual("purple");
169+
});
170+
171+
test("mood function works for true", () => {
172+
expect(mood(true)).toEqual("I am happy");
173+
});
174+
175+
test("mood function works for false", () => {
176+
expect(mood(false)).toEqual("I am not happy");
177+
});
178+
179+
test("(canRegister function retern if people can register or not", () => {
180+
expect(canRegister(45)).toEqual("You Can Register");
181+
});
182+
test("(canRegister function retern if people can register or not", () => {
183+
expect(canRegister(111)).toEqual("You Don't Need To Register");
184+
});
185+
186+
function expectcountReverseToLog(expectedValues) {
187+
const consoleLogSpy = jest.spyOn(console, 'log');
188+
countReverse(20);
189+
expect(consoleLogSpy).toBeCalledTimes(20);
190+
expectedValues.forEach((value, i) => {
191+
expect(consoleLogSpy).nthCalledWith(i+1, value);
192+
});
193+
consoleLogSpy.mockRestore();
194+
};
195+
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+

0 commit comments

Comments
 (0)