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);-
-
Save nkabrown/7919a0d2f99fcc3e1af04680498a9126 to your computer and use it in GitHub Desktop.
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;
}
}
Hey Nathan,
I know you busy and about to leave town... My name is Darleen, I'm in the class preparing for the test to get into the class...
I come from a technology background only dealing with the hardware stuff. I want to get this so bad, its like eating me up inside when i can't solve the code. My problem is how to read the question and then develop a start... With this code below.. I know its going to be an array with for loop and maybe an if statement, but I dont know how to start it off.
Please help me with that part...
/ Example input: [4, 8, 11, 5, 9, 12]
// Returns: 6 (difference between 5 and 11)
// Example input [10, 7, 11, 8, 4, 9, 10]
// Returns: 5 (difference between 4 and 9)