Number alerts in Javascript and HTML

1.1k Views Asked by At

How to decide number of alerts popping up, depending on the input from user?

4

There are 4 best solutions below

0
On

let number = prompt("Write a number");
for (var i = 0; i< parseInt(number); i++){
  alert('hey '+i+1)
}

0
On

I'd only like to add that you need to add the Number function in the alert prompt or else it will consider it like a string.

let number = prompt("Write a number");
for (let i = 0; i< parseInt(number); i++){
   alert('hey ' + Number(i + 1));
}
0
On

I'm not sure why the user would want to see multiple alerts, but try:

let number = prompt("Write a number");
for (let i = 1; i <= number; i++) {
  alert(i);
}

You would still need to check that number is a valid number before running the for(..) loop. Also note that some browsers allow users the choice of hiding multiple alerts.

0
On

This snippet will trigger X alerts from a prompt input

let number = prompt("Write a number");
for (var i = 0; i< parseInt(number); i++){
  alert('hey '+i+1)
}