-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS_codeWars_AUG24.js
More file actions
531 lines (388 loc) · 15.1 KB
/
Copy pathJS_codeWars_AUG24.js
File metadata and controls
531 lines (388 loc) · 15.1 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
///////////////////////////////codeWars 01AUG2024///////////////////////////////
// TASK
// Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
// Examples:
// solution('abc', 'bc') // returns true
// solution('abc', 'd') // returns false
// PARAMETERS
// Receives str and ending in string format.
// will also be in string format
// will never be an empty string
// RETURNS
// return true if ending matches the end of string
// else return false
// EXAMPLES
// solution('abc', 'bc') // returns true
// solution('abc', 'd') // returns false
// PSEUDOCODE
// get length of ending
// splice str from end to the size of ending
// compare spliced str and endind, if equal then return true, else return false
// SOLUTION
// const solution = (str, ending) => (ending === str.slice(-ending.length) || ending === '' ? true : false)
// console.log(solution('abc', 'bc'))
// BEST SOLUTION
///////////////////////////////codeWars 02AUG2024///////////////////////////////
// TASK
// Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
// It should remove all values from list a, which are present in list b keeping their order.
// arrayDiff([1,2],[1]) == [2]
// If a value is present in b, all of its occurrences must be removed from the other:
// arrayDiff([1,2,2,2,3],[2]) == [1,3]
// PARAMETERS
// receiving two arrays (a and b)
// arrays will have integers in them, no decimals or other types of numbers
// array will not be empty and always have numbers in them
// RETURNS
// return one array
// returned array must have only values that are not repeated from list b in list a
// basically, anything in b must be removed from a, and order of numbers must be preserved and not necessarily is put in sorted order when given
// EXAMPLES
//arrayDiff([1,2,2,2,3],[2]) == [1,3]
// PSEUDOCODE
// get array a and array b
// foreach element in a, compare to element in b and remove if equal
// SOLUTION
// function arrayDiff(a, b) {
// for (let i = 0; i < a.length; i++) {
// console.log(a[i])
// if (b.length === 0) {
// return a
// }
// for (let j = 0; j < b.length; j++) {
// if (a[i] === b[j]) {
// a(a[i])
// }
// }
// }
// return a
// }
// function arrayDiff(a, b) {
// return a.filter(element => !b.includes(element))
// }
// console.log(arrayDiff([1, 2, 3], [1, 2]))
// BEST SOLUTION
///////////////////////////////codeWars 03AUG2024///////////////////////////////
// TASK
// Write a function to split a string and convert it into an array of words.
// Examples (Input ==> Output):
// "Robin Singh" ==> ["Robin", "Singh"]
// "I love arrays they are my favorite" ==> ["I", "love", "arrays", "they", "are", "my", "favorite"]
// EXAMPLES
// "Robin Singh" ==> ["Robin", "Singh"]
// REPEAT
// I'm writing a function that will take a string and convert the stirng into an array of words
// The string words will be separated by a space
// I will return an array of strings, to include numbers as strings if the argument has numbers in the string
// APPROACH
// Take the input string and use .split method, separating members to be split by a space
// CODE
// const stringToArray = string => string.split(' ')
// // TEST
// console.log(stringToArray('I love arrays they are my favorite'))
// OPTIMIZE
// No other optimizations for this problem
// BEST SOLUTION
///////////////////////////////codeWars 05AUG2024///////////////////////////////
// TASK
// Now you have to write a function that takes an argument and returns the square of it.
// REPEAT
// The task is to write a function that will take an argument and return the square of that argument.
// I assume the argument will always be in number form (ie. not in string form and require conversion)
// I assume that the argument will never be null
// EXAMPLES
// 4 => 16
// 10 => 100
// APPROACH
// simply function with arrow function format, take in number and return number * number
// CODE
// const square = number => number * number
// TEST
// console.log(square(4))
// console.log(square(10))
// OPTIMIZE
// Possible to also use Math.sqrt function
// BEST SOLUTION
///////////////////////////////codeWars 06AUG2024///////////////////////////////
// TASK
// You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.
// Examples
// [7, 1] => [1, 7]
// [5, 8, 6, 3, 4] => [3, 8, 6, 5, 4]
// [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
// REPEAT
// I will be given an array of numbers. This array will have only numbers, both even and odd. I need to sort the odd numbers in ascending order, that is lowest to highest,
// while leaving the even numbers at their original positions.
// EXAMPLES
// [7, 1] => [1, 7]
// [5, 8, 6, 3, 4] => [3, 8, 6, 5, 4]
// [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
// APPROACH
//
// CODE
// function sortArray(array) {
// let oddNumbers = array.filter(num => num % 2 !== 0)
// oddNumbers = oddNumbers.sort((a, b) => a - b)
// for (let i = 0; i < array.length; i++) {
// if (array[i] % 2 !== 0) {
// array[i] = oddNumbers.shift()
// }
// }
// return array
// }
// // TEST
// console.log(sortArray([7, 1]))
// console.log(sortArray([3, 8, 6, 5, 4]))
// console.log(sortArray([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]))
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 07AUG2024///////////////////////////////
// TASK
// Write a function that checks if a given string (case insensitive) is a palindrome.
// A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards, such as madam or racecar.
// REPEAT
// I will be given a word and I have to check if the word is a palindrome (same backwards and forwards)
// The word will be in string or number form and the function should be case insensitive
// EXAMPLES
// racecar --> true
// cambelak --> false
// radar --> true
// APPROACH
// break given argument into array, reverse array, check to see if arrays equal each other
// CODE
// function isPalindrome(x) {
// const xReverse = x.split('').reverse().join('')
// console.log(xReverse)
// if (x.toLowerCase() === xReverse.toLowerCase()) {
// return true
// } else {
// return false
// }
// }
// // TEST
// console.log(isPalindrome('radar'))
// console.log(isPalindrome('racecar'))
// console.log(isPalindrome('elbow'))
// console.log(isPalindrome('quokka'))
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 08AUG2024///////////////////////////////
// TASK
// You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
// Implement the function which takes an array containing the names of people that like an item. It must return the display text as shown in the examples:
// [] --> "no one likes this"
// ["Peter"] --> "Peter likes this"
// ["Jacob", "Alex"] --> "Jacob and Alex like this"
// ["Max", "John", "Mark"] --> "Max, John and Mark like this"
// ["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
// Note: For 4 or more names, the number in "and 2 others" simply increases.
// REPEAT
// I am taking an array of strings (names) and displaying the text given in the statements
// EXAMPLES
// [] --> "no one likes this"
// ["Peter"] --> "Peter likes this"
// ["Jacob", "Alex"] --> "Jacob and Alex like this"
// ["Max", "John", "Mark"] --> "Max, John and Mark like this"
// ["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
// Note: For 4 or more names, the number in "and 2 others" simply increases.
// APPROACH
// use array lengths to fuel conditionals
// if array length is 0, then retun "no one likes this"
// if array length is between 1 and three, use for loop and concat?
// CODE
// function likes(names) {
// if (names.length === 0) {
// return 'no one likes this'
// } else if (names.length === 1) {
// return `${names[0]} likes this`
// } else if (names.length === 2) {
// return `${names[0]} and ${names[1]} like this`
// } else if (names.length === 3) {
// return `${names[0]}, ${names[1]}, and ${names[2]} like this`
// } else if (names.length > 3) {
// return `${names[0]}, ${names[1]}, and ${names.length - 2} others like this`
// }
// }
// // TEST
// console.log(likes(['Max', 'John', 'Mark']))
// console.log(likes(['Alex', 'Jacob', 'Mark', 'Max']))
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 09AUG2024///////////////////////////////
// TASK
// Return the number (count) of vowels in the given string.
// We will consider a, e, i, o, u as vowels for this Kata (but not y).
// The input string will only consist of lower case letters and/or spaces.
// REPEAT
// I will be given a string and have to return the number of vowels. The string will only have lower case letters and/or spaces.
// EXAMPLES
// apple --> 2
// bread --> 2
// monitor --> 3
// APPROACH
// loop through string and if conditional --> vowel --> count ++, return count
// CODE
// function getCount(str) {
// let count = 0
// for (let i = 0; i < str.length; i++) {
// if (str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u') {
// count++
// }
// }
// return count
// }
// TEST
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 10AUG2024///////////////////////////////
// TASK
// Write Number in Expanded Form
// You will be given a number and you will need to return it as a string in Expanded Form. For example:
// expandedForm(12); // Should return '10 + 2'
// expandedForm(42); // Should return '40 + 2'
// expandedForm(70304); // Should return '70000 + 300 + 4'
// NOTE: All numbers will be whole numbers greater than 0.
// REPEAT
// I will be given a number and I need to return it in expanded form. That means returning each "tenth" of the number.
// EXAMPLES
// expandedForm(12); // Should return '10 + 2'
// expandedForm(42); // Should return '40 + 2'
// expandedForm(70304); // Should return '70000 + 300 + 4'
// APPROACH
// take num, use modulus to get remainder and that becomes the first part of the expanded form
// then reassign the regular number by 10 and Math.floor to make sure it isn't rounded incorrectly because of the decimal
// continue this in your loop for the length of the number in string form
// return all the saved portions
// CODE
// function expandedForm(num) {
// let arr = []
// let numLength = num.toString().length
// for (let i = 0; i < numLength; i++) {
// pushVal = num - (num % 10)
// arr.push(pushVal)
// // arr.push(num % 10)
// num = num - (num - (num % 10))
// console.log(num)
// }
// return arr.join(' + ')
// }
// TEST
// console.log(expandedForm(12))
// console.log(expandedForm(42))
// console.log(expandedForm(70304))
// OPTIMIZE
// BEST SOLUTION
// const expandedForm = n =>
// n
// .toString()
// .split('')
// .reverse()
// .map((a, i) => a * Math.pow(10, i))
// .filter(a => a > 0)
// .reverse()
// .join(' + ')
///////////////////////////////codeWars 12AUG2024///////////////////////////////
// TASK
// Your task is to find the first element of an array that is not consecutive.
// By not consecutive we mean not exactly 1 larger than the previous element of the array.
// E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.
// If the whole array is consecutive then return null.
// The array will always have at least 2 elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!
// 1 Can you write a solution that will return null for both [] and [ x ] though? (This is an empty array and one with a single number and is not tested for, but you can write your own example test. )
// REPEAT
// EXAMPLES
// APPROACH
// CODE
// function firstNonConsecutive(arr) {
// for (let i = 0; i < arr.length - 1; i++) {
// if (arr[i] !== arr[i + 1] - 1) {
// return arr[i + 1]
// }
// }
// return null
// }
// // TEST
// console.log(firstNonConsecutive([1, 2, 3, 4, 6, 7, 8]))
// console.log(firstNonConsecutive([1, 2, 3, 4, 5, 6, 7, 8]))
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 13AUG2024///////////////////////////////
// TASK
// Write a function named sumDigits which takes a number as input and returns the sum of the absolute value of each of the number's decimal digits.
// For example: (Input --> Output)
// 10 --> 1
// 99 --> 18
// -32 --> 5
// Let's assume that all numbers in the input will be integer values.
// REPEAT
// I am taking a number as an input, and returning the sum of the absolute value of each of its decimal place numbers.
// EXAMPLES
// 10 --> 1
// 99 --> 18
// -32 --> 5
// APPROACH
// Assuming that all numbers in the input with be integer values, I will split the number into an array, the use a reduce function to find the sum
// of its absolute value decimal places.
// CODE
// function sumDigits(number) {
// if (number === 0) {
// return 0
// }
// let numberArr = Math.abs(number).toString().split('')
// return numberArr.reduce((acc, currVal) => Number(acc) + Number(currVal))
// }
// TEST
// console.log(sumDigits(10))
// console.log(sumDigits(99))
// console.log(sumDigits(-32))
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 16AUG2024///////////////////////////////
// TASK
// Create a combat function that takes the player's current health and the amount of damage recieved, and returns the player's new health. Health can't be less than 0.
// REPEAT
// I am getting a player's current health and the amount of damage received, then returning the new player's health.
// EXAMPLES
// health 100, damage 5, return 95
// health 20, damage 30, return 0 (edge case)
// APPROACH
// subtraction with conditional for edge case
// CODE
// function combat(health, damage) {
// if (health >= damage) {
// return health - damage
// } else if (damage > health) {
// return 0
// }
// }
// TEST
// OPTIMIZE
// BEST SOLUTION
///////////////////////////////codeWars 30AUG2024///////////////////////////////
// TASK
// Complete the function that takes two integers (a, b, where a < b) and return an array of all integers between the input parameters, including them.
// For example:
// a = 1
// b = 4
// --> [1, 2, 3, 4]
// REPEAT
// I will get two integers, a and b, where a is less than b, and I need to return an array of integers between a and b, including a and b
// EXAMPLES
// a = 1
// b = 4
// --> [1, 2, 3, 4]
// APPROACH
// take a and b, use for loop to add items to an array with a and b as the constraints
// CODE
function between(a, b) {
let arr = []
for (let i = a; i <= b; i++) {
arr.push(i)
}
return arr
}
// TEST
console.log(between(1, 4))
// OPTIMIZE
// Time complexity is linear time because of the for loop
// BEST SOLUTION