JavaScript Challenge - 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) { // 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;

Hello here is my code :
function fizzbuzz(n) {
// Write your code here
let i = 1
const arr =
while(i<=n) {
arr.push(i)
i++
}
for(let j = 0; j <= arr.length; j++) {
if(arr[j] % 3 === 0 && arr[j] % 5 === 0) {
arr[j] = ‘FizzBuzz’
} else if(arr[j] % 5 === 0) {
arr[j] = ‘Buzz’
} else if(arr[j] % 3 === 0) {
arr[j] = ‘Fizz’
}
}
return arr

}

console.log(fizzbuzz(16));

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

v2 with range and map function

const _ = require('lodash');

function fizzbuzz(n) {
  return _.range(1, n + 1)
  .map(e => !(e%5) && !(e%3) ?
     'FizzBuzz' : !(e%3) ?
       'Fizz' : !(e%5) ?
         'Buzz' : e)
}

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

My humble solution.

function fizzbuzz(n) { let arr = [] for(let i = 1; i <= n; i++){ let str = "" if(i % 3 ===0) { str += "Fizz"}; if(i % 5 === 0) { str += "Buzz"}; str ? arr.push(str) : arr.push(i) } return arr } console.log(fizzbuzz(16)); // Leave this line for testing: module.exports = fizzbuzz;

Here is my code:

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;

Here’s my 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 % 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;

function fizzbuzz(n) { const 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;

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 :smiling_face_with_three_hearts:

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!!!

1 Like

Thank you I really appreciate that. I really like that operator :stuck_out_tongue_winking_eye:

1 Like
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;

Hello,

That’s my solution!

function fizzbuzz(n) { let array = []; for (n; n > 0; n--) { if (n % 3 == 0 && n % 5 == 0){ array.unshift("FizzBuzz"); } else if (n % 5 == 0) { array.unshift("Buzz"); } else if (n % 3 == 0) { array.unshift("Fizz"); } else { array.unshift(n); } } return array; }; console.log(fizzbuzz(16));

:metal: :metal:

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;

Fun

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;

My solution is pretty straight forward. Could be optimized to not use mod

function fizzbuzz(n) {
  // Write your code here
  let arr = [];
  for (let i = 1; i <= n; i++) {
    if (i < 3 && arr.push(i)) continue;
    const [mThree, mFive] = [(i % 3) === 0, (i % 5) === 0];
    let str = "";
    mThree && (str += "Fizz");
    mFive && (str += "Buzz");
    arr.push(str ? str : i);
  }
  return arr;
}

console.log(fizzbuzz(16));

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

Say the same thing in six months.

Okay, so 05/1/2023 I’ll be back to review it lol. I’ve been writing js for 3 years, this is straightforward.

1 Like

Whatever you say. See you in May.

In the meantime,

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

1 Like

You’ve got till May to refine the lot.