Skip to content

Instantly share code, notes, and snippets.

@shanebarringer
Created January 27, 2019 03:10
Show Gist options
  • Save shanebarringer/b2c9cd8aca482110eea06b1e25760a8e to your computer and use it in GitHub Desktop.
Save shanebarringer/b2c9cd8aca482110eea06b1e25760a8e to your computer and use it in GitHub Desktop.
// 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;
/* 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;
// 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;
// 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;
// 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;
/* 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