Want user to input number of times a for loop runs in javascript


I have a for loop that prints the numbers 1-20 (fizzbuzz challenge) I want to add a prompt that allows the user to input the number of times that for loops repeats. Below is my code for the for loop. Basically want the 1-20 to print x number of times based on user input. I’ve only been coding for a few weeks so have absolutely no idea where to even start.

var myArray = [] 
for (var i = 1; i <= 20; i++) 
{
    if (i % 5 === 0 && i % 3 === 0) { 
        console.log'FizzBuzz')
    } else if (i % 3 === 0) { 
        console.log("Fizz");
    } else if (i % 5 === 0) { 
        console.log("Buzz");
    } else {              
        console.log(i);
    }
myArray.push(i);        
}

Source: JavaSript – Stack Overflow

August 25, 2021
Category : News
Tags: for-loop | javascript | user-input

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.