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 476
Expand file tree
/
Copy path2-piping.js
More file actions
76 lines (58 loc) · 2.15 KB
/
2-piping.js
File metadata and controls
76 lines (58 loc) · 2.15 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
/*
PIPING FUNCTIONS
================
1. Write 3 functions:
- one that adds 2 numbers together
- one that multiplies 2 numbers together
- one that formats a number so it's returned as a string with a £ sign before it (e.g. 20 -> £20)
2. Using the variable startingValue as input, perform the following operations using your functions all
on one line (assign the result to the variable badCode):
- add 10 to startingValue
- multiply the result by 2
- format it
3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign
the final result to the variable goodCode
*/
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
function format(num) {
return `£${num}`
}
const startingValue = 2;
// Why can this code be seen as bad practice? Comment your answer.
let badCode = format(multiply(add(startingValue, 10), 2));
//This code can be seen as bad practice because multiple methods are used in one line which makes it hard to determine the results from each methods execution. It is confusing and hard to follow.
/* BETTER PRACTICE */
let sum = add(startingValue, 10);
let doubledSum = multiply(sum, 2);
let goodCode = format(doubledSum);
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
To run the tests for just this one file, type `npm test -- --testPathPattern 2-piping` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
*/
test("add function - case 1 works", () => {
expect(add(1, 3)).toEqual(4);
});
test("add function - case 2 works", () => {
expect(add(2.4, 5)).toEqual(7.4);
});
test("multiply function works", () => {
expect(multiply(2, 3)).toEqual(6);
});
test("format function works for whole number", () => {
expect(format(16)).toEqual("£16");
});
test("format function works for decimal number", () => {
expect(format(10.1)).toEqual("£10.1");
});
test("badCode variable correctly assigned", () => {
expect(badCode).toEqual("£24");
});
test("goodCode variable correctly assigned", () => {
expect(goodCode).toEqual("£24");
});