Created
June 27, 2021 04:03
-
-
Save antsmartian/46cb902b53c231583ec7a6e71f6903f8 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
1. In JavaScript, define a function `makeCounter` which takes one optional argument defining the intial value, `start`, with a default value of 0. The function should return an object containing keys that define 3 methods: - `value` returns the current value of the counter - `increment` increments the value of the counter by 1 and returns the new value - `decrement` decrements the value of the counter by 1 and returns the new value The returned object should not allow direct modification or retrieval of the value. | |
Example usage: | |
var counter = makeCounter(); | |
console.log(counter.value()); // 0 | |
var counter2 = makeCounter(4); | |
console.log(counter2.value()); // 4 | |
console.log(counter2.increment()); // 5 | |
console.log(counter2.value()); // 5 | |
counter2.decrement(); | |
counter2.decrement(); | |
console.log(counter2.decrement()); // 2 | |
*/ | |
function makeCounter(initialValue) { | |
let value = initialValue || 0 | |
return { | |
value : () => value, | |
increment: () => {value += 1; return value}, | |
decrement: () => { value -= 1; return value} | |
} | |
} | |
/* | |
2.In JavaScript or Python, define a function `foo` which accepts a Python list or JavaScript array of integers and returns a new list where elements with an even index are incremented by 1 and elements with an odd index are decremented by 1. Assume 0 is an even number. | |
e.g. foo([10, 10, 10, 10, 10]) => [11, 9, 11, 9, 11] | |
*/ | |
function foo(array) { | |
return array.map((data, index) => { | |
if (index % 2 === 0) | |
return data + 1 | |
else | |
return data - 1 | |
}) | |
} | |
/* | |
3. In JavaScript, define a function `minArgs` which accepts any number of numerical arguments and returns the minimum value. Next, show how you would use the function you just created (without modification) to find the minimum of values in an array. | |
Example: | |
minArgs(1, -6, 78, 12, 45.5, -6.9); // -6.9 | |
*/ | |
function minArgs(...rest) { | |
return Math.min(...rest) | |
} | |
/* | |
4. In JavaScript, define a function `group` which accepts an array, `collection`, as the first parameter and a function, `grouper`, as the second parameter. `group` should return an object composed of keys generated by running each element of `collection` through `grouper`. The value of each key should be an array of elements responsible for generating that key. | |
For example: | |
group([6.5, 4.2, 6.3], Math.floor); | |
// { '4': [4.2], '6': [6.5, 6.3] } | |
*/ | |
function group(array, fn) { | |
let groupBy = {} | |
array.forEach((data) => { | |
let fnResult = fn(data); | |
if (!groupBy[fnResult]) { | |
groupBy[fnResult] = [] | |
groupBy[fnResult].push(data) | |
} else { | |
groupBy[fnResult].push(data) | |
} | |
}) | |
return groupBy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment