Skip to content

Instantly share code, notes, and snippets.

@jsmayo
Created February 20, 2018 08:38
Show Gist options
  • Save jsmayo/2f23a2d2a84c1a7c96a40c1bc991f10f to your computer and use it in GitHub Desktop.
Save jsmayo/2f23a2d2a84c1a7c96a40c1bc991f10f to your computer and use it in GitHub Desktop.
ForcefulDarkkhakiPostscript created by jsmayo - https://repl.it/@jsmayo/ForcefulDarkkhakiPostscript
/*
Write a function called doubleValues which accepts an array and returns a new array with all the values in the array passed to the function doubled
Examples:
doubleValues([1,2,3]) // [2,4,6]
doubleValues([1,-2,-3]) // [2,-4,-6]
*/
function doubleValues(arr){
return arr.map(function(val, i, arr) {
return val * 2;
});
}
console.log(doubleValues([1,2,3]));
/*
Write a function called valTimesIndex which accepts an array and returns a new array with each value multiplied by the index it is currently at in the array.
Examples:
valTimesIndex([1,2,3]) // [0,2,6]
valTimesIndex([1,-2,-3]) // [0,-2,-6]
*/
function valTimesIndex(arr){
return arr.map(function(val, i, arr) {
return val * i;
});
}
console.log(valTimesIndex([1,2,3]));
/*
Write a function called extractKey which accepts an array of objects and some key and returns a new array with the value of that key in each object.
Examples:
extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractKey(arr, key){
return arr.map(function(val, i, arr){
return val[key];
});
}
console.log(extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name'));
/*
Write a function called extractFullName which accepts an array of objects and returns a new array with the value of the key with a name of "first" and the value of a key with the name of "last" in each object, concatenated together with a space.
Examples:
extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]) // ['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele']
*/
function extractFullName(arr){
return arr.map(function(val, i, arr) {
return val.first + ' ' + val.last;
});
}
console.log(extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment