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) {
- var maxNumber = numbers[0];
- for ( i=0; i <= numbers.length; i++){
- if (numbers[i] > maxNumber)
- maxNumber = numbers[i];
- };
- return maxNumber;
- function min(numbers) {
- return result = numbers.reduce(function(a, b){
- return a < b ? a : b
- });
- }
- /* 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 maxNumber = numbers[0];
- for ( i=0; i <= numbers.length; i++){
- if (numbers[i] > maxNumber)
- maxNumber = numbers[i];
- };
- return maxNumber;
- function min(numbers) {
- return result = numbers.reduce(function(a, b){
- return a < b ? a : b
- });
- }
- /* 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