Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*************MAX MIN************
- function max(numbers) {
- // your code here
- var maxNum = numbers [0];
- for (i=1; i<numbers.length; i++){
- if (maxNum < numbers [i]){
- maxNum = numbers [i];
- }
- }
- return maxNum;
- }
- function min(numbers) {
- // your code here
- var minNum = numbers [0];
- for (i=1; i<numbers.length; i++){
- if (minNum > numbers [i]){
- minNum = numbers [i];
- }
- }
- 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.');
- })();
- //***************AVERAGE*******************
- function average(numbers) {
- // your code goes here
- var numSum = 0;
- for (i=0; i<numbers.length; i++) {
- numSum += numbers[i];
- }
- return numSum/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.')
- })();
- //****************FIZZBUZZ******************
- function fizzBuzz(countTo) {
- // your code here
- var finalArray = [];
- for (i=1; i<=countTo; i++) {
- if (i%3===0 && i%5===0){
- finalArray.push("fizzbuzz");
- }
- else if (i%3===0){
- finalArray.push("fizz");
- }
- else if (i%5===0){
- finalArray.push("buzz");
- }
- else {
- finalArray.push(i);
- }
- }
- return finalArray;
- }
- /* 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');
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement