[Solved] How to use a loop to loop to the amount of times as a variable [closed]


The main problem with your attempt is here in the for loop.

for (age = 0; age <= 19; age++)

That will overwrite the age that the user entered, and always loop until it reaches 19. You want to create a new loop variable, then loop until the age the user entered.

let name = prompt("What is your name?");
let age = prompt("What is your age?");

for (i = 1; i <= age; i++) {
  document.write("Your name is " + name + " and your age is " + age + "</br>");
}

The other issue is that you don’t do anything about users named “Raymond” using your program. You can do that with an if..else statement around the loop.

let name = prompt("What is your name?");
let age = prompt("What is your age?");

if (name === "Raymond") {
    document.write("Get out of here, Raymond! You know what you did!");
}
else {
  for (i = 1; i <= age; i++) {
      document.write("Your name is " + name + " and your age is " + age + "</br>");
  }
}

1

solved How to use a loop to loop to the amount of times as a variable [closed]