Created
November 29, 2017 07:57
-
-
Save rob137/540a70a6d23d9cf379869a9d3de51e3d to your computer and use it in GitHub Desktop.
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
// https://repl.it/@robertaxelkirby/average-drill | |
function average(numbers) { | |
let total = 0 | |
for (let i = 0; i < numbers.length; i++) { | |
total += numbers[i]; | |
} | |
return total/numbers.length; | |
} | |
/* 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() { | |
const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
const correctAns1 = 5.5; | |
const numList2 = [0, -1, 1]; | |
const correctAns2 = 0; | |
const testResults = [ | |
testFunctionWorks(average, numList1, correctAns1), | |
testFunctionWorks(average, numList2, correctAns2), | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
})(); |
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
// https://repl.it/@robertaxelkirby/fizzbuzz-drill-js | |
function fizzBuzz(countTo) { | |
const outputArr = []; | |
for (var i = 1; i <= countTo; i++) { | |
if (i % 3 === 0 && i % 5 === 0) { | |
outputArr.push('fizzbuzz'); | |
} else if (i % 5 === 0) { | |
outputArr.push('buzz'); | |
} else if (i % 3 === 0) { | |
outputArr.push('fizz') | |
} else { | |
outputArr.push(i); | |
} | |
} | |
return outputArr; | |
} | |
/* 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 | |
const countTo = 16; | |
const expected = [ | |
1, | |
2, | |
'fizz', | |
4, | |
'buzz', | |
'fizz', | |
7, | |
8, | |
'fizz', | |
'buzz', | |
11, | |
'fizz', | |
13, | |
14, | |
'fizzbuzz', | |
16, | |
]; | |
const 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'); | |
} | |
})(); |
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
// https://repl.it/@robertaxelkirby/min-and-max-without-sort-drill | |
function max(numbers) { | |
if (numbers.length) { | |
let biggestSoFar = 0; | |
for (let i = 0; i < numbers.length; i++) { | |
if (biggestSoFar < numbers[i]) { | |
biggestSoFar = numbers[i]; | |
} | |
} | |
return biggestSoFar; | |
} else { | |
return undefined; | |
} | |
} | |
function min(numbers) { | |
if (numbers.length) { | |
let smallestSoFar = numbers[0]; | |
for (let i = 0; i < numbers.length; i++) { | |
if (numbers[i] < smallestSoFar) { | |
smallestSoFar = numbers[i] | |
} | |
} | |
return smallestSoFar; | |
} | |
return undefined; | |
} | |
/* 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 testEmpty(fn) { | |
if (fn([]) === null || fn([]) == undefined) { | |
console.log(`SUCCESS: ${fn.name} works on empty arrays`); | |
return true; | |
} else { | |
console.log( | |
`FAILURE: ${fn.name} should return undefined or null for empty arrays` | |
); | |
return false; | |
} | |
} | |
(function runTests() { | |
// we'll use the variables in our test cases | |
const numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115]; | |
const realMin1 = numList1[3]; | |
const realMax1 = numList1[6]; | |
const numList2 = [0, 1, 2, 3, 4]; | |
const realMin2 = numList2[0]; | |
const realMax2 = numList2[4]; | |
const testResults = [ | |
testFunctionWorks(max, numList1, realMax1), | |
testFunctionWorks(max, numList2, realMax2), | |
testFunctionWorks(min, numList1, realMin1), | |
testFunctionWorks(min, numList2, realMin2), | |
testEmpty(max), | |
testEmpty(min), | |
]; | |
const numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment