Created
January 27, 2019 03:10
-
-
Save shanebarringer/b2c9cd8aca482110eea06b1e25760a8e to your computer and use it in GitHub Desktop.
js-katas-solutions for: https://github.com/shanebarringer/js-katas
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
// Write a function named convert() that takes string values and changes each value to number, and returns a single array | |
const convert = (...values) => { | |
return values.map(v => parseInt(v, 10)).filter(num => !isNaN(num)); | |
}; | |
module.exports = convert; |
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
/* Building on the previous convert function... | |
Write a function named convertAndAdd() that that takes multiple values and adds them together, returning a single value | |
For example: | |
convertAndAdd("5", 10, "20") => 35 | |
*/ | |
const convert = require("./00-convertToNumbers"); | |
const convertAndAdd = (...values) => { | |
return convert(...values).reduce((acc, curr) => acc + curr, 0); | |
}; | |
module.exports = convertAndAdd; |
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
// Write a function named combine() that takes two arguments and returns a single array | |
const combine = (first, ...rest) => | |
[first, ...rest].reduce((acc, curr) => acc.concat(curr), []); | |
module.exports = combine; |
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
// write a function that returns a single array without duplicates | |
const removeDuplicates = (...values) => { | |
return [...new Set(values.reduce((acc, curr) => acc.concat(curr), []))]; | |
}; | |
module.exports = removeDuplicates; |
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
// Find the last element of a list. | |
// Example: | |
// last( [1,2,3,4] ) // => 4 | |
// last( 1,2,3,4 ) // => 4 | |
const last = (...val) => { | |
const combinedArray = val.reduce((acc, curr) => acc.concat(curr), []); | |
return combinedArray[combinedArray.length - 1]; | |
}; | |
module.exports = last; |
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
/* Create a function named keysAndValues() that takes an object and returns returns a two-dimensional array containing an array of keys, and a separate array of values | |
For Example: | |
keysAndValues({firstName: "Foo", lastName: "Bar"}) => [["firstName", "lastName"], ["Foo", "Bar"]] | |
*/ | |
const keysAndValues = obj => { | |
const keys = Object.keys(obj); | |
const values = Object.values(obj); | |
return [keys, values]; | |
}; | |
module.exports = keysAndValues; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment