Created
July 10, 2018 01:35
-
-
Save shravanipl/00d1588da90dd5b70cea1fa55457eaac to your computer and use it in GitHub Desktop.
arrays
This file contains 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) { | |
const short = array.filter( x => x.length<5); | |
return short; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
const 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() { | |
const input1 = ['cat', 'oblivion', 'dog', 'patriarchy', 'blue', 'house']; | |
const result1 = ['cat', 'dog', 'blue']; | |
const input2 = ['rainbow', 'the', 'big', 'broom']; | |
const result2 = ['the', 'big']; | |
const testResults = [ | |
testFunctionWorks(shortWords, input1, result1), | |
testFunctionWorks(shortWords, input2, result2), | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
This file contains 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) { | |
const div = array.find(x=> x%5=== 0); | |
return div; | |
} | |
/* 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() { | |
const input1 = [13, 22, 4, 47, 15, 35, 82]; | |
const result1 = 15; | |
const input2 = [25, 20, 15, 10, 5]; | |
const result2 = 25; | |
const testResults = [ | |
testFunctionWorks(divisibleBy5, input1, result1), | |
testFunctionWorks(divisibleBy5, input2, result2), | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
This file contains 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) { | |
return array.slice(0,array.length-1); | |
} | |
function copyFirstHalf(array) { | |
return array.slice(0,array.length/2); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
const 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() { | |
const list = [ | |
'red bull', | |
'monster', | |
'amp', | |
'rockstar', | |
'full throttle', | |
'kickstart', | |
]; | |
const result1 = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle']; | |
const result2 = ['red bull', 'monster', 'amp']; | |
const list2 = ['lions', 'tigers', 'bears']; | |
const result3 = ['lions']; | |
const testResults = [ | |
testFunctionWorks(minusLastItem, list, result1), | |
testFunctionWorks(copyFirstHalf, list, result2), | |
testFunctionWorks(copyFirstHalf, list2, result3) | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
This file contains 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) { | |
return array.sort( (x,y) => y-x); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
const 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() { | |
const input1 = [10, 3, 5, 22, 19]; | |
const result1 = [22, 19, 10, 5, 3]; | |
const input2 = [2, 4, 6, 8]; | |
const result2 = [8, 6, 4, 2]; | |
const testResults = [ | |
testFunctionWorks(greatestToLeast, input1, result1), | |
testFunctionWorks(greatestToLeast, input2, result2), | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
This file contains 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) { | |
const square= array.map(x=>x*x); | |
return square; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testFunctionWorks(fn, input, expected) { | |
const 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() { | |
const input1 = [1, 2, 3, 4, 5]; | |
const result1 = [1, 4, 9, 16, 25]; | |
const input2 = [2, 4, 6, 8]; | |
const result2 = [4, 16, 36, 64]; | |
const testResults = [ | |
testFunctionWorks(squares, input1, result1), | |
testFunctionWorks(squares, input2, result2), | |
]; | |
const 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