Skip to main content
edited body
Source Link
GrumpyCrouton
  • 466
  • 2
  • 4
  • 16

Expanding on Michael's answer, you could also create an object with all the words you want, with the key bringbeing the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.

optionsdivisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};

for(var i = 1; i <= 100; i++) {
    var output = "";

    for (var x in optionsdivisions) {
        if(i % x == 0) output += options[x];divisions[x]; 
    }

    console.log(output == "" ? i : output);
}

Expanding on Michael's answer, you could also create an object with all the words you want, with the key bring the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.

options = {3: "Fizz", 5: "Buzz", 7: "Jazz"};

for(var i = 1; i <= 100; i++) {
    var output = "";

    for (var x in options) {
        if(i % x == 0) output += options[x]; 
    }

    console.log(output == "" ? i : output);
}

Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.

divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};

for(var i = 1; i <= 100; i++) {
    var output = "";

    for (var x in divisions) {
        if(i % x == 0) output += divisions[x]; 
    }

    console.log(output == "" ? i : output);
}

Source Link
GrumpyCrouton
  • 466
  • 2
  • 4
  • 16

Expanding on Michael's answer, you could also create an object with all the words you want, with the key bring the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.

options = {3: "Fizz", 5: "Buzz", 7: "Jazz"};

for(var i = 1; i <= 100; i++) {
    var output = "";

    for (var x in options) {
        if(i % x == 0) output += options[x]; 
    }

    console.log(output == "" ? i : output);
}