# JavaScript Challenge - FizzBuzz

``````function fizzbuzz(n) {
let res= [];
//a function to find miltiple of 3
const isMultiple3 = (n) =>{
return n%3==0 || false
}
//a function to find multiple of 5
const isMultiple5 = (n) =>{
return n%5==0 || false
}
//iterate over each number to n value,
for(let i = 1; i<=n; i++){
//if number is multiple of 3 and 5 push FizzBuzz else push Fizz
if (isMultiple3(i)){
(isMultiple5(i) && res.push ('FizzBuzz')) || res.push('Fizz')
}
//if number miltiple of 5 only push Buzz
else if (isMultiple5(i)){
res.push('Buzz')
}
// if number is none push number
else{
res.push(i)
}
}
return res
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
``````

Here is my code for the fizzBuzz Chalenge, I feel like i could have made it more concise and avoid so many if else statement, but I also like the readability, what’s your view on that?

Looks like I went a slightly different route than some here but we mostly all boiled it down to the same logic. This was fun and helped me understand a few really great methods MUCH better.

function fizzbuzz(n) { const rangeArray = Array.from({ length: n }, (value, index) => index + 1); rangeArray.map((value, index) => { if (value % 3 === 0 && value % 5 === 0) { rangeArray[index] = "FizzBuzz"; } else if (value % 3 === 0) { rangeArray[index] = "Fizz"; } else if (value % 5 === 0) { rangeArray[index] = "Buzz"; } }) return rangeArray; } console.log(fizzbuzz(16)); // Leave this line for testing: module.exports = fizzbuzz;

Here is my solution, it was the first way to implement that came to mind… seems overly simple and basic, I’m sure it could be refactored a lot. Would this be considered an okay solution in an interview ? Keen to hear any constructive feedback

``````function fizzbuzz(n) {
let fizzBuzzArray = [];

for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
fizzBuzzArray.push('FizzBuzz');
}
else if (i % 3 === 0) {
fizzBuzzArray.push('Fizz');
}
else if (i % 5 === 0) {
fizzBuzzArray.push('Buzz');
} else {
fizzBuzzArray.push(i);
}
}
return fizzBuzzArray;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
Copy your code and post it to the forums.
``````

I came up with a very similar solution!

``````function fizzbuzz(n) {
let array = [];
for (let i = 1; i <= n; i++) {
if(i % 3 === 0 && i % 5 === 0) {
array.push('FizzBuzz');
} else if (i % 3 === 0) {
array.push('Fizz');
} else if (i % 5 === 0){
array.push('Buzz');
} else {
array.push(i);
}
}
return array;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
``````
1 Like

We see this a lot. It is not wrong, per se, but it excludes the fact that if both numbers are divisible then so is their product.

``````3 * 5 === 15  =>  LCM

if (i % 15 === 0)
``````

uses less logic to perform the same check.

Read up on Lowest Common Multiple to get a better grasp if this concept is new to you. We may not need to know mathematics to learn programming but it can offer us some timesaving tricks.

2 Likes
``````function fizzbuzz(n) {
const newArr = [];
for(let i = 1; i < n + 1; i++) {
if(i % 3 === 0 && i % 5 === 0){
newArr.push('FizzBuzz')
}else if(i % 3 === 0){
newArr.push('Fizz')
}else if(i % 5 === 0) {
newArr.push('Buzz')
}else {
newArr.push(i)
}
}
return newArr
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
``````

pretty basic iteration of a classic problem

Here is my code !

``````function fizzbuzz(n) {
return [...Array(n).keys()].map(x => ++x).map(x => {
if (x % 3 === 0 && x % 5 === 0) return "FizzBuzz";
if (x % 3 === 0) return "Fizz";
if (x % 5 === 0) return "Buzz";
return x;
})
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
``````