Hello! Can somebody help me with this FizzBuzz challenge?

I would like to use input value instead of prompt. Here is my code:

// Prompt
let answer = parseInt(prompt("Please enter your number for FizzBuzz: "));

for (let i = 1; i <= answer; i++){

    if(i % 3 === 0 && i % 5 === 0) {

        document.getElementById("demo").textContent = "FizzBuzz";

    } else if (i % 3 === 0) {

        document.getElementById("demo").textContent = "Fizz";

    } else if (i % 5 === 0) {

        document.getElementById("demo").textContent = "Buzz"

    } else {

        document.getElementById("demo").textContent = i;

    }

 }

// input
document.getElementById("button").onclick = function() {

 var answer = document.getElementById("demo_1").value;

 

 console.log("your number is", answer);

}

Thanks in advance :slightly_smiling_face:

Thanks for the editing :slightly_smiling_face:

document.getElementById("button").onclick = function() {
 let answer = document.getElementById("demo_1").value;
 
 for (let i = 1; i <= answer; i++){

    if(i % 3 === 0 && i % 5 === 0) {
       
       console.log("FizzBuzz");
    
    } else if (i % 3 === 0) {

       console.log("Fizz");

    } else if (i % 5 === 0) {

        console.log("Buzz")  ;

    } else {
      
        console.log(i) ;
    }

 }
 
}

Hi friends!
I get this code to the console. Any advices how to get it to the DOM? Thanks in advance. :slightly_smiling_face:

I would like to display this code on the DOM(browser) .I already did it on the console.
Thanks so much. Here is the code below:

document.getElementById("button").onclick = function() {
 let answer = document.getElementById("demo_1").value;
 
 for (let i = 1; i <= answer; i++){

    if(i % 3 === 0 && i % 5 === 0) {
       
       console.log("FizzBuzz");
    
    } else if (i % 3 === 0) {

       console.log("Fizz");

    } else if (i % 5 === 0) {

        console.log("Buzz")  ;

    } else {
      
        console.log(i) ;
    }

 }
 
}

Hi @mike2020

  1. You can create another element with an id in the HTML eg. <p id='output'></p>
  2. Create an empty array before the for loop
  3. Push the values in that array instead of logging them to the console or you could do both
  4. Target the element created in step 1 and change the innerHTML to that array.
    Note: perform this step after the loop is completed.

It works…Thanks so much :slightly_smiling_face:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.