Last active
July 19, 2020 08:07
-
-
Save exbotanical/166f3ef7130551e710ac19bc355e3b08 to your computer and use it in GitHub Desktop.
code golf cont'd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findInArray = (array, iterator) => { | |
for (let [i, v] of Object.entries(array)) { | |
if (iterator(v, Number(i))) { | |
return Number(i); | |
} | |
} | |
return -1; | |
}; | |
/* | |
Prompt | |
"We'll create a function that takes in two parameters: | |
- a sequence (length and types of items are irrelevant) | |
- a function (value, index) that will be called on members of the sequence and their index. | |
The function will return either true or false. | |
Your function will iterate through the members of the sequence in order until the provided function returns true; | |
at which point your function will return that item's index. | |
If the function given returns false for all members of the sequence, your function should return -1." | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const factorial = n => n < 0 ? null : `${Array.from({ length: n }, (_, i) => i + 1).reduce((x, y) => x * y)}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const solution = n => { | |
let a = 0, | |
b = 0, | |
c = 0; | |
for (let i = 1; i < n; i++) { | |
i % 3 === 0 ? i % 5 === 0 ? c++ : a++ : i % 5 === 0 ? b++ : ""; | |
} | |
return [a, b, c]; | |
}; | |
/* | |
Prompt | |
"Write a function that takes an integer and returns an array [A, B, C], | |
where A is the number of multiples of 3 (but not 5) below the given integer, | |
B is the number of multiples of 5 (but not 3) below the given integer | |
and C is the number of multiples of 3 and 5 below the given integer. | |
For example, solution(20) should return [5, 2, 1]" | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const maxMultiple = (divisor, bound) => { | |
for (let i = bound; i > 0; i--) { | |
if (i % divisor === 0) { | |
return i; | |
} | |
} | |
}; | |
// best to-date: O(1) constant time | |
// solution: by definition the remainder of a division is that which must be substracted | |
// from the given bound so as to yield a whole multiple | |
const maxMultiple = (divisor, bound) => bound - bound % divisor; | |
/* | |
Prompt | |
Given a Divisor and a Bound , Find the largest integer N , Such That , | |
Conditions : | |
- N is divisible by divisor | |
- N is less than or equal to bound | |
- N is greater than 0. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment