if (i % 3 === 0 && i % 5 === 0) {
newArray.push("FizzBuzz");
} else if (i % 3 === 0) {
newArray.push("Fizz");
} else if (i % 5 === 0) {
newArray.push("Buzz");
} else {
newArray.push(i);
}
}
return newArray;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
// Write your code here
const newArray = [ ];
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
newArray.push("FizzBuzz");
} else if (i % 3 === 0) {
newArray.push("Fizz");
} else if (i % 5 === 0) {
newArray.push("Buzz");
} else {
newArray.push(i);
}
}
return newArray;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
// An array for keeping track of the numbers and strings.
const list = [];
// A loop that goes through all the numbers for the parameter n.
for (i = 1; i <= n; i++) {
// Adds the string to list if it's a multiple of 5 or 3.
if (i % 5 === 0 && i % 3 === 0) {
list.push("FizzBuzz");
// Adds the string to list if it's a multiple of 5.
} else if (i % 5 === 0) {
list.push("Buzz");
// Adds the string to list if it's a multiple of 3.
} else if (i % 3 === 0) {
list.push("Fizz");
// Adds the number to the list.
} else {
list.push(i);
}
}
return list;
};
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
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 % 5 === 0) {
array.push(‘Buzz’)
} else if (i % 3 === 0) {
array.push(‘Fizz’)
} else {
array.push(i);
}
}
return array;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
Gotta jump on the bandwagon! Pretty sure a lot of more efficient code have been written here, so feel free to give me kind advices on how to improve :3 cheers
function fizzbuzz(n) {
// Write your code here
const arr = [];
for (let i = 1; i < n+1; i++) {
if (i % 3 == 0 && i % 5 != 0) {
arr.push('Fizz');
} else if (i % 5 == 0 && i % 3 != 0) {
arr.push('Buzz');
} else if (i % 5 == 0 && i % 3 == 0) {
arr.push('FizzBuzz');
} else {
arr.push(i);
}
}
return arr;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
I just stumbled across your post and I don’t think it’s too naive at all. I love the use of modulo. That alone is impressive because a lot of people struggle with that operator. Good work!!!
function fizzbuzz(n) {
let result = [];
for (let i = 1; i <= n; i++) {
let replace = '';
if (i % 3 === 0) {
replace += 'Fizz';
}
if (i % 5 === 0) {
replace += 'Buzz';
}
if (!!replace) {
result.push(replace);
} else {
result.push(i);
}
}
return result;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
// Write your code here
let arr = [];
//create a new array
//loop through the arrays
for(let i = 0; i < n; i++){
let num = i+1;
//create a variable called num and assign the value of i to it to aid for iteration/count;
if(num % 3 === 0 && num % 5 === 0){
num = 'FizzBuzz';
arr.push(num)
}
//First, get the multiples of both 3 and 5 and push them to the array.
else if(num % 3 === 0){
num = 'Fizz';
arr.push(num);
}
//Next, get the multiples of 3 and push them to the array
else if( num % 5 === 0){
num = 'Buzz';
arr.push(num);
}
//Last get the multiples of 5 and push them to the array
else{
arr.push(num);
}
//Finally, get all the remaining numbers and push them to the array.
}
return arr;
//return the arr.
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
let arr = []
for (let x = 1; x <= n; x++) {
arr.push(x)
}
return arr.map(y => {
let three = y % 3 === 0;
let five = y % 5 === 0;
return three && five ? 'FizzBuzz'
: three ? 'Fizz'
: five ? "Buzz"
: y
})
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
const fb = function (x){
const fb = new Array(x + 1).fill('');
const n = fb.length;
for (let i = 0; i < n; i++) {
let h = '';
h += i % 3 === 0 ? 'Fizz' : '';
h += i % 5 === 0 ? 'Buzz' : '';
fb[i] = h || `${i}`;
}
return fb;
}
pretty clean, I had considered going with fill but decided against it for performance. I mean in this challenge it wouldn’t make much difference so still a solid solution