Skip to content

Instantly share code, notes, and snippets.

@schamblee
Created January 19, 2018 22:01
Show Gist options
  • Save schamblee/c8952528323bdb2d0b81f18c937439fe to your computer and use it in GitHub Desktop.
Save schamblee/c8952528323bdb2d0b81f18c937439fe to your computer and use it in GitHub Desktop.
Array copying I
function firstFourItems(array) {
return array.slice(0, 4)
}
function lastThreeItems(array) {
return array.slice(-3)
}
Array copying II
function minusLastItem(array) {
return array.slice(0, array.length-1);
}
function copyFirstHalf(array) {
return array.slice(0, array.length/2)
}
Squares with map
function squares(array) {
return array.map(num => num ** 2);
}
Sort
function greatestToLeast(array) {
return array.sort((a, b) => b - a);
}
Filter
function shortWords(array) {
return array.filter(function (word) {
return word.length < 5;
});
}
function divisibleBy5(array) {
return array.find(num => num % 5 === 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment