Created
November 22, 2016 15:24
-
-
Save johnfkneafsey/962aaacd29ef75d753e487bb9dfc1b62 to your computer and use it in GitHub Desktop.
Array Drills
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 max(numbers) { | |
var currentMax = numbers[0]; | |
for (var i=0; i <= numbers.length; i++) { | |
if (numbers[i] > currentMax) { | |
currentMax = numbers[i]; | |
} | |
} | |
return currentMax; | |
} | |
function min(numbers) { | |
var currentMin = numbers[0]; | |
for (var i=0; i <= numbers.length; i++) { | |
if (numbers[i] < currentMin) { | |
currentMin = numbers[i]; | |
} | |
} | |
return currentMin; | |
} | |
/* 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() { | |
// we'll use the variables in our test cases | |
var numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115]; | |
var realMin1 = numList1[3]; | |
var realMax1 = numList1[6]; | |
var numList2 = [0, 1, 2, 3, 4]; | |
var realMin2 = numList2[0]; | |
var realMax2 = numList2[4]; | |
var testResults = [ | |
testFunctionWorks(max, numList1, realMax1), | |
testFunctionWorks(max, numList2, realMax2), | |
testFunctionWorks(min, numList1, realMin1), | |
testFunctionWorks(min, numList2, realMin2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
})(); | |
function max(numbers) { | |
var currentMax = numbers[0]; | |
for (var i=0; i <= numbers.length; i++) { | |
if (numbers[i] > currentMax) { | |
currentMax = numbers[i]; | |
} | |
} | |
return currentMax; | |
} | |
function min(numbers) { | |
var currentMin = numbers[0]; | |
for (var i=0; i <= numbers.length; i++) { | |
if (numbers[i] < currentMin) { | |
currentMin = numbers[i]; | |
} | |
} | |
return currentMin; | |
} | |
/* 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() { | |
// we'll use the variables in our test cases | |
var numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115]; | |
var realMin1 = numList1[3]; | |
var realMax1 = numList1[6]; | |
var numList2 = [0, 1, 2, 3, 4]; | |
var realMin2 = numList2[0]; | |
var realMax2 = numList2[4]; | |
var testResults = [ | |
testFunctionWorks(max, numList1, realMax1), | |
testFunctionWorks(max, numList2, realMax2), | |
testFunctionWorks(min, numList1, realMin1), | |
testFunctionWorks(min, numList2, realMin2), | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
})(); | |
function fizzBuzz(countTo) { | |
var result = []; | |
for (var i=1; i <= countTo; i++) { | |
if (i % 15 === 0) { | |
result.push('fizzbuzz'); | |
} | |
else if (i % 5 === 0) { | |
result.push('buzz'); | |
} | |
else if (i % 3 === 0) { | |
result.push('fizz'); | |
} | |
else { | |
result.push(i); | |
} | |
} | |
return result; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
(function testFizzBuzz() { | |
// we'll use the variables in our test cases | |
var countTo = 16; | |
var expected = [ | |
1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', | |
'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16 | |
]; | |
var actual = fizzBuzz(countTo) || []; | |
if ( | |
expected.length === actual.length && | |
expected.every(function(item, index) { | |
return actual[index] === item;}) ) { | |
console.log('SUCCESS: fizzBuzz is working'); | |
} | |
else { | |
console.log('FAILURE: fizzBuzz is not working'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment