This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path2-piping.js
More file actions
66 lines (53 loc) · 1.8 KB
/
2-piping.js
File metadata and controls
66 lines (53 loc) · 1.8 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
/*
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(x, y) {
let sum = (x * 10 + y * 10) / 10;
return sum;
}
console.log(add(2.4, 5.3));
function multiply(x, y) {
let multiple = x * y;
return multiple;
}
function format(number) {
return `£${number}`;
}
const startingValue = 2;
// Why can this code be seen as bad practice? Comment your answer.
let badCode = format(multiply(add(startingValue, 10), 2));
/* BETTER PRACTICE */
let goodCode = add(startingValue, 10);
goodCode = multiply(goodCode, 2);
goodCode = format(goodCode);
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
To run these tests type `node 2-piping.js` into your terminal
*/
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}
test("add function - case 1 works", add(1, 3) === 4);
test("add function - case 2 works", add(2.4, 5.3) === 7.7);
test("multiply function works", multiply(2, 3) === 6);
test("format function works", format(16) === "£16");
test("badCode variable correctly assigned", badCode === "£24");
test("goodCode variable correctly assigned", goodCode === "£24");