Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active July 29, 2016 18:08
Show Gist options
  • Select an option

  • Save nkabrown/7919a0d2f99fcc3e1af04680498a9126 to your computer and use it in GitHub Desktop.

Select an option

Save nkabrown/7919a0d2f99fcc3e1af04680498a9126 to your computer and use it in GitHub Desktop.
C4Q Access Code — Application Workshop

FizzBuzz

function fizzbuzz(times) {
  for (var i = 1; i <= times; i++) {
    // you must test for both first
    if (i % 5 === 0 && i % 3 === 0) {
      console.log('FizzBuzz');
    } else if (i % 5 === 0) {
      console.log('Buzz');
    } else if (i % 3 === 0) {
      console.log('Fizz');
    } else {
      console.log(i);
    }
  }
}

fizzbuzz(100);
@nkabrown

nkabrown commented Jun 29, 2016 •

Copy link
Copy Markdown
Author

Hi Darleen,

I'll do a little bit with the adjacent elements question and leave the difference between any two elements to you.

So this is the question:

Largest gap between consecutive elements in an array

Write a function that takes as input an array, and returns the largest difference between any two adjacent elements.

If we break that down we want to find the differences between all the adjacent elements in the array and save the largest difference to a variable that we will return from the function.

The pieces of code that we will need are a function, a variable to store the largest difference, a for loop that will iterate over the adjacent element pairs of the array, a return statement that will return the largest difference.

Note:
What happens if the first element is 4 and the second element is 11. 4 - 11 is -7. It's going to be hard to make a conditional test with both negative and positive numbers.

See this link for a method from the Math built-in object called abs() that will always return a positive number value: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs

var largestDiff = function(array) {
    var bigDiff = // what should we put here to get the difference between the first element of the array and the second element of the array

    // iterate over each element of the array minus one (why minus one?)
    for (var i = 0; i < array.length - 1; i++) {
      var difference = // what should we put here to get the difference between the current element value and next's value
      if (bigDiff < difference) {
        bigDiff = difference;
      }

      return bigDiff;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment