Created
August 12, 2018 10:25
-
-
Save DynamiteC/74787520a21fdfda10d563aa7effff76 to your computer and use it in GitHub Desktop.
Thinkful_Array_Loop_Drill
This file contains hidden or 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
// noprotect | |
// ^^ `noprotect` is here to prevent a bug with jsbin and for loops. | |
function average(numbers) { | |
// your code goes here | |
var sum = 0; | |
for(var i in numbers) | |
{ | |
sum += numbers[i]; | |
} | |
return sum/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() { | |
var numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
var correctAns1 = 5.5; | |
var numList2 = [0, -1, 1]; | |
var correctAns2 = 0; | |
var testResults = [ | |
testFunctionWorks(average, numList1, correctAns1), | |
testFunctionWorks(average, numList2, correctAns2) | |
]; | |
var numPassing = testResults.filter(function(result){ return result; }).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.') | |
})(); |
This file contains hidden or 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
// noprotect | |
// ^^ `noprotect` is here to prevent a bug with jsbin and for loops. | |
function fizzBuzz(countTo) { | |
// your code here | |
var arr = []; | |
for(var x = 1; x <= countTo; x++) | |
{ | |
if(x%3==0 && x%5==0) | |
arr.push('fizzbuzz'); | |
else if(x%3==0) | |
arr.push('fizz'); | |
else if(x%5==0) | |
arr.push('buzz'); | |
else | |
arr.push(x); | |
} | |
return arr; | |
} | |
/* 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 | |
var countTo = 16; | |
var expected = [ | |
1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', | |
'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16 | |
]; | |
var 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 hidden or 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
// noprotect | |
// ^^ `noprotect` is here to prevent a bug with jsbin and for loops. | |
function max(numbers) { | |
// your code here | |
var max_sum = Number.MIN_SAFE_INTEGER; | |
for(var x = 0; x < numbers.length; x++) | |
{ | |
max_sum = Math.max(numbers[x],max_sum); | |
} | |
return max_sum; | |
} | |
function min(numbers) { | |
// your code here | |
var min_sum = Number.MAX_SAFE_INTEGER; | |
for(var x = 0; x < numbers.length; x++) | |
{ | |
min_sum = Math.min(numbers[x],min_sum); | |
} | |
return min_sum; | |
} | |
/* 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