Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
From FizzBuzz to an array

I cannot seem to figure this out whatsoever, I can use console.log(i) and get the return but cant figure out how to return as an array?

Takes a number, max and returns an array that contains every number from 0 to max (not inclusive) that is divisible by either 3 or 5, but not both

    function fizzBuzz(max) {
  let arr = [];
  
    for(let i = 0; i < max; i++){
        if(i % 3 === 0 && i % 7 !== 0){
          arr = arr.concat(i);
          
        } else if(i % 3 !== 0 && i % 7 === 0){
          arr = arr.concat(i);
   }
  return arr;
};
fizzBuzz(20);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using Array.prototype.push (which is used to add an element to an array) rather than Array.prototype.concat (which is used to merge an array with another array) is much simpler here. You can also use the XOR operator (^) instead of writing two if statements:

function fizzBuzz(max) {
  let arr = [];

  for (let i = 0; i < max; i++) {
    if (i % 3 === 0 ^ i % 5 === 0) {
      arr.push(i);
    }
  }
  return arr;
}

console.log(fizzBuzz(20));

about 4 years ago · Juan Pablo Isaza Report

0

Few things wrong in your code, you should use array.push instead of concat and you were missing a } at the end.

This code should do what you need :) produces an array between 0 and max inclusive and produces an array of numbers that are divisible by 3 or 5 but not both

function fizzBuzz(max) {
  let arr = [];  
  for(let i = 0; i <= max; i++){
    if((!(i % 3) && !(i % 5))) continue;
    if(!(i % 3) || !(i % 5)){
      arr.push(i);
    }         
  }
  return arr;
};
console.log(fizzBuzz(20));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!