forked from portsoc/js201
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (96 loc) · 1.9 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* This is index.js
*
* Start by modifying the id, fn and sn functions to return
* information about you, then open index.html to check what
* else you have to do, adding functions to the end of this
* file as necessary.
*
* NB: all code you write this year should use strict mode, so
* we've enabled that by default with the first line of code.
*/
'use strict';
function id() {
return 'up2194485';
}
function fn() {
return 'Thomas';
}
function sn() {
return 'Robinson';
}
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function checkObject(obj) {
obj.checked = true;
}
function checkObjectInside(obj) {
if ('data' in obj) {
obj.data.checked = true;
}
}
function arraySet(arr, i, n) {
if (arr[i]) arr[i] = n;
}
function addAll(arr) {
let sum = 0;
arr.forEach(num => sum += num);
return sum;
}
function larger(a, b) {
return (a > b) ? a : b;
}
function largest(arr) {
let max; // we can't set this to 0, since we might have all negative #s
arr.forEach(num => {
if (num > max || !max) max = num;
});
return max ? max : null;
}
function compare(a, b) {
if (a.length !== b.length) return false;
for (const i in a) {
if (a[i] !== b[i]) return false;
}
return true;
}
function addToAll(arr, n) {
for (const i in arr) {
arr[i] += n;
}
return arr;
}
let remembered;
function rememberThis(keepsake) {
remembered = keepsake;
}
function nArray(n) {
const arr = [];
for (let i = 0; i < n; i++) {
arr[i] = i + 1;
}
return arr;
}
function addAllOpt(arr) {
if (!arr || arr.length === 0) return 0;
return addAll(arr);
}
function divisors(arr, div) {
const divisable = [];
arr.forEach(n => {
if (n % div === 0) divisable.push(n);
});
return divisable;
}
function multiples(n, m) {
const arr = [];
// similar to nArray!
for (let i = 0; i < n; i++) {
arr[i] = (i + 1) * m;
}
return arr;
}