Last active
January 18, 2022 12:16
-
-
Save iancover/63d2d6bc0f7a8493a68741ebb3b48c12 to your computer and use it in GitHub Desktop.
Thinkful JS Arrays exercises
This file contains hidden or 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
// Array Ex 6 | |
function makeList(item1, item2, item3) { | |
return [item1,item2,item3]; | |
} | |
// tests | |
function testMakeList() { | |
var items = ["prime rib", "fried goat cheese salad", "fish tacos"]; | |
var result = makeList(items[0], items[1], items[2]); | |
if ( | |
result && result.length && items.length === result.length && | |
items.every(function(item) { | |
return result.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `makeList` works!'); | |
} else { | |
console.error('FAILURE: `makeList` is not working'); | |
} | |
} | |
testMakeList(); |
This file contains hidden or 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
// Arrays Ex 1 | |
function accessFirstItem(array) { | |
return array[0] | |
} | |
function accessThirdItem(array) { | |
return array[2] | |
} |
This file contains hidden or 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
// Arrays Ex 7 | |
function shortWords(array) { | |
return array.filter(function(str) {return str.length < 5}); | |
} |
This file contains hidden or 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
// Arrays Ex 8 | |
function divisibleBy5(array) { | |
return array.find(function(num) {return num%5 === 0;}) | |
} |
This file contains hidden or 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
// Array Ex 5 | |
function findLength(array) { | |
return array.length | |
} | |
function accessLastItem(array) { | |
return array[array.length-1] | |
} |
This file contains hidden or 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
// Arrays Ex 10 | |
function squares(array) { | |
return array.map(function(num) {return num*num}); | |
} | |
function squares(array) { | |
var result = array.map(function(num) { | |
return num**2; | |
}); | |
return result; | |
} |
This file contains hidden or 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
// Arrays Ex 2 | |
function addToList(list, item) { | |
list.push(item) | |
return list | |
} |
This file contains hidden or 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
// Arrays Ex 3 | |
function firstFourItems(array) { | |
return array.slice(0,4); | |
} | |
function lastThreeItems(array) { | |
return array.slice(-3); | |
} |
This file contains hidden or 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
// Array Ex 4 | |
function minusLastItem(array) { | |
return array.slice(0,-1); | |
} | |
function copyFirstHalf(array) { | |
if { | |
array.length % 2 === 0; | |
return array.slice(0,array.length % 2)); | |
else { | |
return array.slice() | |
} | |
} | |
// Correct solution: | |
function copyFirstHalf(array) { | |
return array.slice(0,array.length/2); | |
} |
This file contains hidden or 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
// Arrays Ex 9 | |
function greatestToLeast(array) { | |
return array.sort(function(a,b) {return b-a;}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment