Skip to content

Instantly share code, notes, and snippets.

@nikhilmufc7
Created December 15, 2016 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilmufc7/d63fac48c66f1a78d6d8940c653c120e to your computer and use it in GitHub Desktop.
Save nikhilmufc7/d63fac48c66f1a78d6d8940c653c120e to your computer and use it in GitHub Desktop.
function average(numbers) {
var total = numbers[0];
for (var i=1; i < numbers.length; i++){
total += numbers[i];
}
return total/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.')
})();
function fizzBuzz(countTo) {
var result = [];
for (var i=1; i <= countTo; i++) {
if (i % 15 === 0) {
result.push('fizzbuzz');
}
else if (i % 5 === 0) {
result.push('buzz');
}
else if (i % 3 === 0) {
result.push('fizz');
}
else {
result.push(i);
}
}
return result;
}
/* 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');
}
})();
function max(numbers) {
var currentMax = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
function min(numbers) {
var currentMin = numbers[0];
for (var 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