Skip to content

Instantly share code, notes, and snippets.

Created June 9, 2017 20:19
Show Gist options
  • Save anonymous/e11556db441fbc2d3ab26dc691ec16c6 to your computer and use it in GitHub Desktop.
Save anonymous/e11556db441fbc2d3ab26dc691ec16c6 to your computer and use it in GitHub Desktop.
minMaxdrillJS created by dtstanley - https://repl.it/G9KJ/199
function max(numbers) {
var maxNum = 0;
for (var i =1; i<= numbers.length; i++){
if (maxNum <= numbers[i]){maxNum = numbers[i];
}
}
return maxNum;
}
function min(numbers) {
var minNum = numbers[0];
for (var i =1; i<= numbers.length; i++){
if (minNum >= numbers[i]){minNum = numbers[i];
}
}
return minNum;
}
/* 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.');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment