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
72 lines (51 loc) · 1.86 KB
/
2-piping.js
File metadata and controls
72 lines (51 loc) · 1.86 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
/*
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(a) {
return `£${a}`
}
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 adds= add(startingValue,10);
let multiplies = multiply(adds , 2);
let goodCode = format(multiplies);
/* ======= 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
*/
const util = require('util');
function test(test_name, actual, expected) {
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your code returned: ${util.inspect(actual)}`;
}
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), 7.4)
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")