Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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) {
- for (var i=0;i<numbers.length;i++) { //loop through array
- var maxNum = 0; //create point of comparison and placeholder
- if (maxNum < numbers[i]) { //compare current num to maxNum
- maxNum = numbers[i];// if larger, replace maxNum
- }
- }
- return maxNum;
- }
- function min(numbers) {
- for (var i=0;i<numbers.length;i++) { //loop through array
- var minNum = 0; //create point of comparison and placeholder
- if (minNum > numbers[i]) { //compare current num to minNum
- minNum = numbers[i];// if smaller, replace minNum
- }
- }
- 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.');
- })();
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">function max(numbers) {
- for (var i=0;i<numbers.length;i++) { //loop through array
- var maxNum = 0; //create point of comparison and placeholder
- if (maxNum < numbers[i]) { //compare current num to maxNum
- maxNum = numbers[i];// if larger, replace maxNum
- }
- }
- return maxNum;
- }
- function min(numbers) {
- for (var i=0;i<numbers.length;i++) { //loop through array
- var minNum = 0; //create point of comparison and placeholder
- if (minNum > numbers[i]) { //compare current num to minNum
- minNum = numbers[i];// if smaller, replace minNum
- }
- }
- 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.');
- })();
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement