forked from portsoc/js201
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (143 loc) · 3.19 KB
/
index.js
File metadata and controls
166 lines (143 loc) · 3.19 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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 "UP2060197";
// e.g. return "UP654321";
}
function fn() {
return "mork";
}
function sn() {
return "c";
}
function example() {
// replace this example with
// your first function then
// add more below as necessary.
}
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
// const obj = {
// checked: false
// }
function checkObject(obj) {
obj.checked = true;
}
function checkObjectInside(obj) {
if (obj.data) {
obj.data.checked = true;
//0 would equal false here... (not the best solution), could use != null
}
}
/*
Create an arraySet function that accepts three parameters `arr`, `i` and `n`.
The first one is an array and the second one an index.
The function should place the value of the third parameter into the array at an index specified by the second parameter,
if (and only if) such an index is already in the array.
Note that your function does not need to return a value because the array is modified directly. Rerun
*/
function arraySet(arr, i, n) {
if (arr[i]) {
arr[i] = n;
}
}
function addAll(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
function larger(a, b) {
return Math.max(a, b);
// turnary operator???? = (a,b) => a > b ? a : b;
//what
}
/* function largest(arr) {
//return Math.max(...arr);
//why does this not work?
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
return max;
}
//ig this doesnt work either :DDDDDD
}
*/
function largest(arr) {
return arr.length > 0 ? Math.max(...arr) : null;
}
//Create a function called compare that accepts two arrays of numbers (`a` and `b`) and compares the contents. It should return true if the arrays are identical and false otherwise.
function compare(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
function addToAll(arr, n) {
for (let i = 0; i < arr.length; i++) {
arr.splice(i, 1, arr[i] + n);
}
return arr;
}
let remembered = null;
function rememberThis(keepsake) {
remembered = keepsake;
}
function nArray(n) {
let array = [];
let count = 1;
while (array.length < n) {
array.push(count++);
}
return array;
}
function addAllOpt(arr) {
if (!arr || arr.length === 0) {
return 0;
}
let count = 0;
for (let i = 0; i < arr.length; i++) {
count += arr[i];
}
return count;
}
function divisors(arr, div) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % div === 0) {
result.push(arr[i]);
}
}
return result;
}
function multiples(n, m){
let count = 1;
let result = [];
for (let i = 0; i < n; i++){
result.push(count*m);
count++;
}
return result;
}