Last active
June 20, 2016 11:55
-
-
Save alexcanessa/0719dcbd3f5143c739fc8cc5fe456b24 to your computer and use it in GitHub Desktop.
Questions (and solutions) for functional programming
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) Write a function sum() and a function multiply() that sums and multiplies (respectively) | |
all the numbers in an array of numbers. For example, sum([1,2,3,4]) should return 10, | |
and multiply([1,2,3,4]) should return 24. | |
*/ | |
/* | |
2) Write a function max() that takes an array of numbers and returns the largest of them. | |
*/ | |
/* | |
3) Write a function findLongestWord() that takes an array of words and returns the length of the longest one. | |
*/ | |
/* | |
4) Write a function onlyStrings() that takes an array of different types of values, | |
and returns an array of strings only. | |
*/ | |
/* | |
5) Write a function oneEven() that returns true if at least one of the numbers in an array is even. | |
*/ | |
/* | |
6) Write a function even() that returns true if and only if all the numbers in an array are even. | |
*/ | |
/* | |
7) Write a function alolexox() that doubles every consonant and place an occurrence of "o" in between. | |
For example, translate("this is fun") should return the string "tothohisos isos fofunon" | |
*/ | |
/* | |
8) Write a function kraken() that accepts an array, removes everything is not a string, | |
and returns the concatenation of all of the strings, preceded by 'DD - ' | |
e.g. [1, false, [1, 2], 'a', 'b', {a: 1}, 'c'] // DD - abc | |
*/ |
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) Write a function sum() and a function multiply() that sums and multiplies (respectively) | |
all the numbers in an array of numbers. For example, sum([1,2,3,4]) should return 10, | |
and multiply([1,2,3,4]) should return 24. | |
*/ | |
// 1.a | |
let sum = numbers => numbers.reduce((prev, curr) => prev + curr); | |
// 1.b | |
let multiply = numbers => numbers.reduce((prev, curr) => prev * curr); | |
/* | |
2) Write a function max() that takes an array of numbers and returns the largest of them. | |
*/ | |
function max(numbers) { | |
return numbers.reduce((prev, curr) => Math.max(prev, curr)); | |
}; | |
let max = numbers => Math.max(...numbers); | |
/* | |
3) Write a function findLongestWord() that takes an array of words and returns the length of the longest one. | |
*/ | |
let findLongestWord = words => words.reduce((prev, curr) => prev.length > curr.length ? prev : curr).length; | |
/* | |
4) Write a function onlyStrings() that takes an array of different types of values, and returns an array of strings only. | |
*/ | |
let onlyStrings = items => items.filter(item => typeof item === 'string'); | |
/* | |
5) Write a function oneEven() that returns true if at least one of the numbers in an array is even. | |
*/ | |
let oneEven = numbers => numbers.some(number => number % 2 === 0); | |
/* | |
6) Write a function even() that returns true if and only if all the numbers in an array are even. | |
*/ | |
let even = numbers => numbers.every(number => number % 2 === 0); | |
/* | |
7) Write a function alolexox() that doubles every consonant and place an occurrence of "o" in between. | |
For example, translate("this is fun") should return the string "tothohisos isos fofunon" | |
*/ | |
let alolexox = (phrase) => { | |
let escapes = ['a', 'e', 'i', 'o', 'u', ' ']; | |
return phrase | |
.split('') | |
.map(letter => escapes.indexOf(letter) === -1 ? letter + 'o' + letter : letter) | |
.join(''); | |
} | |
/* | |
8) Write a function kraken() that accepts an array, removes everything is not a string, | |
and returns the concatenation of all of the strings, preceded by 'DD - ' | |
e.g. [1, false, [1, 2], 'a', 'b', {a: 1}, 'c'] // DD - abc | |
*/ | |
let kraken = stuff => stuff.filter(item => typeof item === 'string').reduce((master, piece) => master + piece, 'DD - '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment