-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
292 lines (255 loc) · 6.25 KB
/
solution.js
File metadata and controls
292 lines (255 loc) · 6.25 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
function arrayDiff(a, b) {
b.forEach((itemDiff) => {
a = a.filter((item) => item !== itemDiff);
console.log(a);
});
return a;
}
console.log(arrayDiff([], [1, 2]));
function digitalRoot(n) {
let dir = ["n", "s", "n", "s", "n", "s", "n", "s", "n", "s"];
let newDir = dir.map((val, arr) => val.length);
console.log(newDir);
while (n >= 10) {
let result = 0;
n.toString()
.split("")
.forEach((val) => {
result += parseInt(val);
});
n = result;
}
console.log(n);
return n;
// ...
}
console.log(digitalRoot(313357));
function isValidWalk(walk) {
if (walk.length !== 10) {
return false;
}
let ns = 0;
let ew = 0;
walk.forEach((block) => {
if (block === "n") ns++;
if (block === "s") ns--;
if (block === "w") ew++;
if (block === "e") ew--;
});
return ns === 0 && ew === 0;
//insert brilliant code here
}
console.log(isValidWalk(["n", "s", "n", "s", "n", "s", "n", "s", "n", "s"]));
function bouncingBall(h, bounce, window) {
// your code here
const isBounce = 0 < bounce && bounce < 1;
const isH = h > 0;
const isWindow = window < h;
if (isBounce && isH && isWindow) {
let count = 1;
h = h * bounce;
while (h > window) {
h = h * bounce;
count += 2;
}
return count;
} else {
return -1;
}
}
console.log(bouncingBall(30.0, 0.66, 1.5));
function high(x) {
const AlphabetObj = new Map([
["a", 1],
["b", 2],
["c", 3],
["d", 4],
["e", 5],
["f", 6],
["g", 7],
["h", 8],
["i", 9],
["j", 10],
["k", 11],
["l", 12],
["m", 13],
["n", 14],
["o", 15],
["p", 16],
["q", 17],
["r", 18],
["s", 19],
["t", 20],
["u", 21],
["v", 22],
["w", 23],
["x", 24],
["y", 25],
["z", 26],
]);
x = x.split(" ");
let result = {};
let score = 0;
x.forEach((str) => {
console.log(str);
for (let char of str) {
score += AlphabetObj.get(char);
}
result[str] = score;
score = 0;
});
const maxWord = Object.keys(result).reduce((a, b) =>
result[a] >= result[b] ? a : b
);
return maxWord;
}
console.log(high("b aa"));
;
function alphabetPosition(text) {
const AlphabetObj = new Map([
["a", 1],
["b", 2],
["c", 3],
["d", 4],
["e", 5],
["f", 6],
["g", 7],
["h", 8],
["i", 9],
["j", 10],
["k", 11],
["l", 12],
["m", 13],
["n", 14],
["o", 15],
["p", 16],
["q", 17],
["r", 18],
["s", 19],
["t", 20],
["u", 21],
["v", 22],
["w", 23],
["x", 24],
["y", 25],
["z", 26],
]);
let textArr = text.replace(/\s+/g, "").toLowerCase().split("");
let result = textArr.map(
(char) => (char = AlphabetObj.get(char) ? AlphabetObj.get(char) : "")
);
result = result.filter((val) => val != "");
return result.join(" ");
}
console.log(alphabetPosition("The sunset sets at twelve o' clock."));
function findOdd(A) {
//happy coding!
let result;
//loop through the array and create a new array that saves unique value and number of times it occurs
let countMap = new Map();
A.forEach((val) => {
if (countMap.has(val)) {
countMap.set(val, countMap.get(val) + 1);
} else {
countMap.set(val, 1);
}
});
for (let [key, count] of countMap)
if (count % 2 !== 0) {
return key;
}
//const findOdd = (xs) => xs.reduce((a, b) => a ^ b)
//return the value that has odd count
return 0;
}
function findEvenIndex(arr) {
//Code goes here!
//sum array
// loop through array and sum, then check if (leftsum = totalsum-leftsum-arr[i])
// return the index that meet the check
const totalSum = arr.reduce((prev, curr) => prev + curr, 0);
let leftSum = 0;
for (let i = 0; i < arr.length; i++) {
if (leftSum == totalSum - leftSum - arr[i]) {
return i;
}
leftSum += arr[i];
}
return -1;
}
findEvenIndex([1, 2, 3, 4, 3, 2, 1]);
function incrementString(strng) {
// return incrementedString
let reg = /\d*$/;
let numStrng = strng.match(reg)[0];
if (numStrng) {
const padLength = numStrng.length;
let num = parseInt(numStrng) + 1;
let numRes = num.toString().padStart(padLength, 0);
return strng.slice(0, -padLength) + numRes;
}
return `${strng}1`;
}
console.log(incrementString("foo0042"));
console.log(incrementString("fo97obar79"));
// Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. For example:
//12 ==> 21
// 513 ==> 531
//2017 ==> 2071
//If the digits can't be rearranged to form a bigger number, return -1
function nextBigger(n) {
//your code here
//check if numbers are equal return -1
//slice the last digit if its not equal
//revers the arr,
}
function nextBigger(n) {
// Convert number to an array of digits
let digits = n.toString().split("");
// Step 1: Find the rightmost pair where the first digit is less than the next
for (let i = digits.length - 2; i >= 0; i--) {
if (digits[i] < digits[i + 1]) {
// Step 2: Find the smallest digit to the right of digits[i] that's larger than digits[i]
for (let j = digits.length - 1; j > i; j--) {
if (digits[j] > digits[i]) {
// Step 3: Swap digits[i] and digits[j]
[digits[i], digits[j]] = [digits[j], digits[i]];
// Step 4: Sort the remaining digits after i to get the smallest possible number
let leftPart = digits.slice(0, i + 1);
let rightPart = digits.slice(i + 1).sort();
// Combine and return the result as a number
return parseInt(leftPart.concat(rightPart).join(""), 10);
}
}
}
}
// If no bigger number can be formed, return -1
return -1;
}
//console.log(nextBigger(144))
function solution(list) {
//loop through each item
let result = [];
let i = 0;
while (i < list.length) {
let start = i;
while (i < list.length - 1 && list[i + 1] === list[i] + 1) {
i++;
}
if (i - start >= 2) {
result.push(`${list[start]}-${list[i]}`);
} else {
for (j = start; j <= i; j++) {
result.push(`${list[j]}`);
}
}
i++;
}
console.log(result);
return result.join(",");
}
console.log(
solution([
-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20,
])
);