Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

388
Visualizações
How does the Fizzbuzz algorithm work in JavaScript?

I want to ask as to why when I turn if (i % 3 === 0 && i % 5 === 0) {console.log("Fizz")}; into else if(i % 3 === 0 && i % 5 === 0) {console.log("Fizz")}; vice versa, I only get Fizz and Buzz on console.log and no FizzBuzz unlike when I used if. I expect the same result with else if thus I think they should be the same?

Below is the full script for reference.

let answer = 100;

for (let i = 1; i <= answer; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz");
  } else if (i % 5 === 0) {
    console.log("Buzz");
  } else if (i % 3 === 0) {
    console.log("Fizz");
  } else {
    console.log(i);
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

forget them oldskool solutions.

IMHO the most important Array Method to learn is Map:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Which "walks over" the Array and returns a new value for that Array index

let arr = Array(100)
  .fill((x, div, label) => x % div ? "" : label) //store Function in every index
  .map((func, idx) =>
    func(++idx, 3, "Fizz") + func(idx, 5, "Buzz") || idx
  );
document.body.append(arr.join(", "));

fill takes a single Object, it is not executed 100 times!
Since JavaScript Functions are Objects this code declares a function once for every index

Note the ++idx because we want to start at 1, not 0

In JavaScript ""+"" is a Falsy value, thus it returns the idx value for non-FizzBuzz numbers

More Array Methods explained: https://array-methods.github.io

about 4 years ago · Juan Pablo Isaza Relatório

0

When you use else if the previous if condition has to be false for the else if condition to be verified (and its content possibly executed).

Otherwise it's only a chain of indepent ifs.

about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need to consider the "both case" separately, as your code does. In pseudocode:

if (i is divisible by 3) {
    print "Fizz"
}
if (i is divisible by 5) {
    print "Buzz"
}

If you run through this on paper, you can see that it will print "FizzBuzz" by executing both conditions. So it is not wrong to have a special case for both, but it is easier (and clearer) to do it without that.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda