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

Commit 5ae7fb4

Browse files
added missing exercises
1 parent e5fca84 commit 5ae7fb4

File tree

1 file changed

+114
-61
lines changed

1 file changed

+114
-61
lines changed

mandatory/2-function-creation.js

Lines changed: 114 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,144 @@
11
/*
2-
Complete the function to check if the variable `num` satisfies the following requirements:
3-
- is a number
4-
- is even
5-
- is less than or equal to 100
6-
Tip: use logical operators
2+
Complete the function to return a boolean describing whether a user is acceptable.
3+
To be acceptable, two conditions must be fulfilled
4+
1. the user should be 18 or older
5+
2. the user must be logged in
76
*/
7+
function isAcceptableUser(userAge, isLoggedIn) {}
88

9-
function validate(num) {}
9+
/*
10+
Complete the function to apply discount percent based on how much is totalPrice in user cart.
11+
- When the total price is greater than 200, a 10% discount should be applied
12+
- When the total price is less than 200, a 5% discount should be applied
13+
14+
The function should return the new price to be paid (e.g. if the totalPrice is 150, a 5% discount
15+
is applieds and 142.5 should be returned)
16+
*/
17+
18+
function applyDiscount(totalPrice) {}
1019

1120
/*
12-
Write a function that:
13-
- takes a number as input
14-
- return a string formatted as percentages (e.g. 10 => "10%")
15-
- the number must be rounded to 2 decimal places
16-
- numbers greater 100 must be replaced with 100
21+
Complete the function to print to the console the odd numbers between 1 and limit (use a while loop):
22+
*/
23+
function printOddNumbers(limit) {}
24+
25+
/*
26+
Complete the buyTwoGetTheCheapestFree function: if user buys two items, the cheapest item will be free!
27+
The function should return the price to be paid once the discount is applied.
1728
*/
29+
function buyTwoGetTheCheapestFree(price1, price2) {}
1830

19-
function formatPercentage(num) {}
31+
/*
32+
Complete the function to determine if it is suitable for a person to register based on their age!
33+
- if the person is 12 or younger it should return "You Are Too Young To Register"
34+
- if the person is older than 12 and younger than 90 it should return "You Can Register"
35+
- if the person is 90 or older it should return "You Don't Need To Register"
36+
*/
37+
function canRegister(age) {}
2038

2139
/*
22-
Write a function that:
23-
- takes an array of strings as input
24-
- removes any spaces in the beginning or end of each string
25-
- removes any forward slashes (/) in each string
26-
- makes all strings all lowercase
40+
Complete the function so that it prints out to the console numbers in reverse order starting at
41+
number and going down to 1 (e.g. if number was 3, it would print:
42+
3
43+
2
44+
1
45+
)
2746
*/
28-
function tidyUpStrings(arrayOfStrings) {}
47+
48+
function countReverse(number) {}
2949

3050
/* ======= TESTS - DO NOT MODIFY ===== */
3151

32-
test("validate function accepts valid even number", () => {
33-
expect(validate(10)).toEqual(true);
52+
test("isAcceptableUser function returns true if over 18 and logged in", () => {
53+
expect(isAcceptableUser(21, true)).toEqual(true);
54+
});
55+
56+
test("isAcceptableUser function returns true if 18 and logged in", () => {
57+
expect(isAcceptableUser(18, true)).toEqual(true);
58+
});
59+
60+
test("isAcceptableUser function returns false if under 18 and logged in", () => {
61+
expect(isAcceptableUser(17, true)).toEqual(false);
62+
});
63+
64+
test("isAcceptableUser function returns false if over 18 and not logged in", () => {
65+
expect(isAcceptableUser(21, false)).toEqual(false);
66+
});
67+
68+
test("applyDiscount function returns price with 5% discount", () => {
69+
expect(applyDiscount(120)).toEqual(114);
70+
});
71+
72+
test("applyDiscount function returns price with 10% discount", () => {
73+
expect(applyDiscount(280)).toEqual(252);
74+
});
75+
76+
test("printOddNumbers function prints odd numbers between 1 and 2", () => {
77+
expectprintOddNumbersToLog([1], 2);
78+
});
79+
80+
test("printOddNumbers function prints odd numbers between 1 and 10", () => {
81+
expectprintOddNumbersToLog([1, 3, 5, 7, 9], 10);
82+
});
83+
84+
function expectprintOddNumbersToLog(expectedValues, limit) {
85+
const consoleLogSpy = jest.spyOn(console, "log");
86+
printOddNumbers(limit);
87+
expect(consoleLogSpy).toBeCalledTimes(expectedValues.length);
88+
expectedValues.forEach((value, i) => {
89+
expect(consoleLogSpy).nthCalledWith(i + 1, value);
90+
});
91+
consoleLogSpy.mockRestore();
92+
}
93+
94+
test("buyTwoGetTheCheapestFree function returns first price when it is largest", () => {
95+
expect(buyTwoGetTheCheapestFree(700, 500)).toEqual(700);
3496
});
3597

36-
test("validate function accepts other valid even number", () => {
37-
expect(validate(18)).toEqual(true);
98+
test("buyTwoGetTheCheapestFree function returns second price when it is largest", () => {
99+
expect(buyTwoGetTheCheapestFree(500, 700)).toEqual(700);
38100
});
39101

40-
test("validate function accepts exactly 100", () => {
41-
expect(validate(100)).toEqual(true);
102+
test("(canRegister function returns in case of a person of age 7", () => {
103+
expect(canRegister(7)).toEqual("You Are Too Young To Register");
42104
});
43105

44-
test("validate function rejects odd number", () => {
45-
expect(validate(17)).toEqual(false);
106+
test("canRegister function returns in case of a person of age 12", () => {
107+
expect(canRegister(12)).toEqual("You Are Too Young To Register");
46108
});
47109

48-
test("validate function rejects string", () => {
49-
expect(validate("Ten")).toEqual(false);
110+
test("canRegister function returns in case of a person of age 13", () => {
111+
expect(canRegister(13)).toEqual("You Can Register");
50112
});
51113

52-
test("validate function rejects stringified number", () => {
53-
expect(validate("10")).toEqual(false);
114+
test("canRegister function returns in case of a person of age 89", () => {
115+
expect(canRegister(89)).toEqual("You Can Register");
54116
});
55117

56-
test("validate function rejects too large number", () => {
57-
expect(validate(108)).toEqual(false);
118+
test("canRegister function returns in case of a person of age 90", () => {
119+
expect(canRegister(90)).toEqual("You Don't Need To Register");
58120
});
59121

60-
test.each([
61-
[23, "23%"],
62-
[18.103, "18.1%"],
63-
[187.2, "100%"],
64-
[0.372, "0.37%"],
65-
])("formatPercentage function works for %s", (input, expected) => {
66-
expect(formatPercentage(input)).toEqual(expected);
122+
test("canRegister function returns in case of a person of age 112", () => {
123+
expect(canRegister(112)).toEqual("You Don't Need To Register");
67124
});
68125

69-
test("tidyUpString function works", () => {
70-
expect(
71-
tidyUpStrings([
72-
"/Daniel",
73-
" /Sanyia",
74-
"AnTHonY",
75-
"irina",
76-
" Gordon",
77-
"ashleigh ",
78-
" Alastair ",
79-
" anne marie ",
80-
])
81-
).toEqual([
82-
"daniel",
83-
"sanyia",
84-
"anthony",
85-
"irina",
86-
"gordon",
87-
"ashleigh",
88-
"alastair",
89-
"anne marie",
90-
]);
126+
test("countReverse function logs values from 7 to 1", () => {
127+
expectcountReverseToLog([7, 6, 5, 4, 3, 2, 1], 7);
91128
});
129+
130+
function expectcountReverseToLog(expectedValues, start) {
131+
const consoleLogSpy = jest.spyOn(console, "log");
132+
countReverse(start);
133+
expect(consoleLogSpy).toBeCalledTimes(expectedValues.length);
134+
expectedValues.forEach((value, i) => {
135+
expect(consoleLogSpy).nthCalledWith(i + 1, value);
136+
});
137+
consoleLogSpy.mockRestore();
138+
}
139+
140+
/*
141+
CHECK OUT solutions.md FOR MORE INFO ON OUR SOLUTION
142+
*/
143+
144+
/* ======= TESTS - DO NOT MODIFY ===== */

0 commit comments

Comments
 (0)