-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperate-array.html
78 lines (70 loc) · 1.97 KB
/
operate-array.html
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
77
78
<!DOCTYPE html>
<html>
<head>
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
</head>
<body>
<div id="example"></div>
<script type="text/javascript">
const addCounter = (list) => {
// list.push(0); //TypeError: Can't add property 0, object is not extensible
// return list;
// return list.concat([0]);
return [...list, 0];
};
const removeCounter = (list, index) => {
// list.splice(index, 1);
// return list;
// return list
// .slice(0,index)
// .concat(list.slice(index+1));
return [
...list.slice(0,index),
...list.slice(index + 1)
];
};
const incrementCounter = (list, index) => {
// list[index]++;
// return list;
// return list
// .slice(0,index)
// .concat([list[index] + 1])
// .concat(list.slice(index + 1));
return [
...list.slice(0,index),
list[index]+1,
...list.slice(index+1)
];
};
const testAddCounter = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const testRemoveCounter = () => {
const listBefore = [0, 10, 20];
const listAfter = [0, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 1)
).toEqual(listAfter);
};
const testIncrementCounter = () => {
const listBefore = [0, 10, 20];
const listAfter = [0, 11, 20];
deepFreeze(listBefore);
expect(
incrementCounter(listBefore, 1)
).toEqual(listAfter);
}
testAddCounter();
testRemoveCounter();
testIncrementCounter();
console.log("tests passed!");
</script>
</body>
</html>