Skip to content

Instantly share code, notes, and snippets.

@Mattteo1220
Created March 12, 2017 23:51
Show Gist options
  • Save Mattteo1220/da12551a00f2e6e3580531a49e96d0d4 to your computer and use it in GitHub Desktop.
Save Mattteo1220/da12551a00f2e6e3580531a49e96d0d4 to your computer and use it in GitHub Desktop.
max/min drill solution max/min drill solution // source https://jsbin.com/qixeqo
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="max/min drill solution">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>max/min drill solution</title>
</head>
<body>
x
<script id="jsbin-javascript">
function max(numbers) {
var currentMax = numbers[0];
for (i=0; i < numbers.length;i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
function min(numbers) {
var currentMin = numbers[0];
for (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.');
})();
</script>
<script id="jsbin-source-javascript" type="text/javascript">function max(numbers) {
var currentMax = numbers[0];
for (i=0; i < numbers.length;i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
function min(numbers) {
var currentMin = numbers[0];
for (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.');
})();
</script></body>
</html>
function max(numbers) {
var currentMax = numbers[0];
for (i=0; i < numbers.length;i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
function min(numbers) {
var currentMin = numbers[0];
for (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.');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment