Created
August 12, 2018 10:15
-
-
Save DynamiteC/a78a11babd61d6423f65c0ac5116729b to your computer and use it in GitHub Desktop.
Thinkful_Array_Drills
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
function accessFirstItem(array) { | |
// your code goes here | |
return array[0]; | |
} | |
function accessThirdItem(array) { | |
// your code goes here | |
return array[2]; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
if (fn(input) === expected) { | |
console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`'); | |
return true; | |
} | |
else { | |
console.log( | |
'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input) | |
); | |
return false; | |
} | |
} | |
function runTests() { | |
var list = [1, 4, 9, 16, 25] | |
var item1 = 1 | |
var item2 = 9 | |
var testResults = [ | |
testFunctionWorks(accessFirstItem, list, item1), | |
testFunctionWorks(accessThirdItem, list, item2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function addToList(list, item) { | |
// your code goes here | |
list.push(item); | |
return list | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testAddToList() { | |
var input1 = ["red", "blue", "green"] | |
var input2 = "pink" | |
var expected = ["red", "blue", "green", "pink"] | |
var result = addToList(input1, input2); | |
if ( | |
result && result.length && expected.length === result.length && | |
expected.every(function(item) { | |
return result.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `addToList` works!'); | |
} else { | |
console.error('FAILURE: `addToList` is not working'); | |
} | |
} | |
testAddToList(); |
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
function firstFourItems(array) { | |
// your code goes here | |
return array.slice(0,4); | |
} | |
function lastThreeItems(array) { | |
// your code goes here | |
return array.slice(-3); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
var result = fn(input); | |
if ( | |
result && result.length === expected.length && | |
result.every(function(item) { | |
return expected.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `' + fn.name + '` works!') | |
return true; | |
} | |
else { | |
console.error('FAILURE: `' + fn.name + '` is not working') | |
return false; | |
} | |
} | |
function runTests() { | |
var list = ["red bull", "monster", "amp", "rockstar", "full throttle"]; | |
var result1 = ["red bull", "monster", "amp", "rockstar"]; | |
var result2 = ["amp", "rockstar", "full throttle"]; | |
var testResults = [ | |
testFunctionWorks(firstFourItems, list, result1), | |
testFunctionWorks(lastThreeItems, list, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function minusLastItem(array) { | |
// your code goes here | |
return array.slice(0,array.length-1); | |
} | |
function copyFirstHalf(array) { | |
// your code goes here | |
var len= array.length | |
if(len%2==0) | |
return array.slice(0,len/2); | |
else | |
return array.slice(0,parseInt(len/2)-1); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
var result = fn(input); | |
if ( | |
result && result.length === expected.length && | |
result.every(function(item) { | |
return expected.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `' + fn.name + '` works!'); | |
return true; | |
} else { | |
console.error('FAILURE: `' + fn.name + '` is not working'); | |
return false; | |
} | |
} | |
function runTests() { | |
var list = ["red bull", "monster", "amp", "rockstar", "full throttle", "kickstart"]; | |
var result1 = ["red bull", "monster", "amp", "rockstar", "full throttle"]; | |
var result2 = ["red bull", "monster", "amp"]; | |
var testResults = [ | |
testFunctionWorks(minusLastItem, list, result1), | |
testFunctionWorks(copyFirstHalf, list, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function shortWords(array) { | |
// your code goes here | |
return array.filter(function (v,i,s) { | |
return v.length < 5; | |
}); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
var result = fn(input); | |
if ( | |
result && result.length === expected.length && | |
result.every(function(item) { | |
return expected.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `' + fn.name + '` works!') | |
return true; | |
} | |
else { | |
console.error('FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input)) | |
return false; | |
} | |
} | |
function runTests() { | |
var input1 = ['cat', 'oblivion', 'dog', 'patriarchy', 'blue']; | |
var result1 = ['cat', 'dog', 'blue']; | |
var input2 = ['rainbow', 'the', 'big']; | |
var result2 = ['the', 'big']; | |
var testResults = [ | |
testFunctionWorks(shortWords, input1, result1), | |
testFunctionWorks(shortWords, input2, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function divisibleBy5(array) { | |
// your code here | |
return array.find(function (v,i,s){ | |
return v%5==0 | |
}); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
if (fn(input) === expected) { | |
console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`'); | |
return true; | |
} | |
else { | |
console.error( | |
'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input) | |
); | |
return false; | |
} | |
} | |
function runTests() { | |
var input1 = [13, 22, 4, 47, 15, 35, 82]; | |
var result1 = 15; | |
var input2 = [25, 20, 15, 10, 5]; | |
var result2 = 25; | |
var testResults = [ | |
testFunctionWorks(divisibleBy5, input1, result1), | |
testFunctionWorks(divisibleBy5, input2, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function greatestToLeast(array) { | |
// your code goes here | |
return array.sort(function (x,y){ | |
return y-x; | |
}); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
var result = fn(input); | |
if ( | |
result && | |
result.length === expected.length && | |
result.every(function(item, index) { | |
return (index === 0 || result[index] < result[index-1]); | |
}) && | |
result.every(function(item) { | |
return expected.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `' + fn.name + '` works!') | |
return true; | |
} | |
else { | |
console.error('FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input)) | |
return false; | |
} | |
} | |
function runTests() { | |
var input1 = [10, 3, 5, 22, 19]; | |
var result1 = [22, 19, 10, 5, 3]; | |
var input2 = [2, 4, 6, 8]; | |
var result2 = [8, 6, 4, 2]; | |
var testResults = [ | |
testFunctionWorks(greatestToLeast, input1, result1), | |
testFunctionWorks(greatestToLeast, input2, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function findLength(array) { | |
// your code goes here | |
return array.length; | |
} | |
function accessLastItem(array) { | |
// your code goes here | |
return array[array.length-1]; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
if (fn(input) === expected) { | |
console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`'); | |
return true; | |
} | |
else { | |
console.error( | |
'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input) | |
); | |
return false; | |
} | |
} | |
function runTests() { | |
var list = [1, 4, 9, 16, 25]; | |
var originalList = [1, 4, 9, 16, 25]; | |
var length = 5; | |
var lastItem = 25; | |
var testResults = [ | |
testFunctionWorks(findLength, list, length), | |
testFunctionWorks(accessLastItem, list, lastItem), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
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
function makeList(item1, item2, item3) { | |
// your code here | |
return [item1,item2,item3]; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// 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
function squares(array) { | |
// your code here | |
return array.map(function(x) { | |
return x**2; | |
}); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
var result = fn(input); | |
if ( | |
result && result.length === expected.length && | |
result.every(function(item) { | |
return expected.indexOf(item) > -1; | |
})) { | |
console.log('SUCCESS: `' + fn.name + '` works!') | |
return true; | |
} | |
else { | |
console.error('FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected + | |
' but was ' + fn(input)) | |
return false; | |
} | |
} | |
function runTests() { | |
var input1 = [1, 2, 3, 4, 5]; | |
var result1 = [1, 4, 9, 16, 25]; | |
var input2 = [2, 4, 6, 8]; | |
var result2 = [4, 16, 36, 64]; | |
var testResults = [ | |
testFunctionWorks(squares, input1, result1), | |
testFunctionWorks(squares, input2, result2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment