-
Notifications
You must be signed in to change notification settings - Fork 0
/
pure_impure_demo.js
76 lines (46 loc) · 1.01 KB
/
pure_impure_demo.js
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
// Constant array
const defaultCart = [];
function addItem(cart, item){
cart.push(item)
return cart;
}
// Constant array
const defaultCart = [];
// updating the array
function addItem(cart, item){
cart[cart.length] = item;
return cart;
}
// Not altering the array but creating a copy
const defaultCart = [];
function addItem(cart, item){
userCart = [...cart];
userCart.push(item);
return userCart;
}
function addItem(arr, item){
return [...arr, item];
}
// What if the function does not return anything?
function hello(name){
helloName = 'hello ' + name;
}
function toUpperCase(arr){
copyArr = [...arr];
for(i = 0; i < copyArr.length; i++){
copyArr[i] = copyArr[i].toUpperCase();
}
return copyArr;
}
function toUpperCase(arr){
return arr.map((element) => element.toUpperCase() );
}
// What about Random?
function randomWithId(name){
return Math.random() + name;
}
// I/O Operation
function giveNumber(){
console.log(number);
return number;
}