# Homework 2 #5

Merged
merged 7 commits into from Dec 12, 2018
Commits
Filter file types
Failed to load files and symbols.
+709 −0

#### Just for now

Homework 2 files

norbs committed Nov 30, 2018
commit 7afb692cd7c78d537b1fbc161c72f505004ce0a2
@@ -0,0 +1,44 @@
// Relying on your newfound knowledge of loops, combine loops and if/else statements together and incrementally build the common fizzbuzz loop.

// - In the loop, every time a number is divisible by **3**, instead of logging the number itself, the word "fizz" should appear.
// - If the number is divisible by **5**, the word "buzz" should be logged.
// - If the number is divisible by both **3** and **5**, then the word "fizzbuzz" should be logged.

// ##### Step 1:
// Construct a for loop that iterates through, and `console.log()`'s out, numbers 1 - 100.

// ##### Step 2:
// Add an `if/else` statement that logs the string `"fizz"` if the value being iterated over is divisible by `3`; otherwise, log out the value.

// ##### Step 3:
// Add an `else if` clause that logs the string `"buzz"` if the value being iterated over is divisible by `5`.

// ##### Step 4:
// Add an additional `else if` clause that logs the string `"fizzbuzz"` if the value being iterated over is divisible by both `3` and `5`.

//##### BONUS:

// a. Make a copy of your existing code and comment out the original.
// b. In the new copy, instead of using console.log for the results, push the
// result of each loop to an array.
// c. After all iterations have, completed, use the forEach array method to log
// each array element value to the console.
// Label your console output with the original number and a colon before each
// array value. (Hint: How can you shorten your code using a second forEach
// function parameter?)

for(let i=1;i<=100;i++) {

#### vodnik Dec 12, 2018

Owner

I recommend spaces around operators for readability.

let fizz = i % 3 === 0;
let buzz = i % 5 === 0;
let output = i;

#### vodnik Dec 12, 2018

Owner

This is an elegant solution -- creating an output variable and then mutating it (or not) depending on the tests. Nice!

if(fizz && buzz) {
output = 'fizzbuzz';
} else if(fizz) {
output = 'fizz';
} else if(buzz) {
output = 'buzz';
}
console.log(output);
}
 @@ -0,0 +1,9 @@