简体   繁体   English

如何使声明不断重复,直到它成为真的?

[英]how to make the statement to repeat itself until it gets true?

I want this statement to repeat itself until the condition is true, forgot the syntax to do so我希望这个语句重复自己直到条件为真,忘记了这样做的语法

let gradeAmount = Number(prompt('insert a number between 1 and 5'));
const grades = [];
if (gradeAmount >= 1 && gradeAmount <= 5) {
  for (let i = 0; i < gradeAmount; i++) {
    const grade = Number(prompt('insert the grade'));

    grades.push(grade);
  }
} else {
  alert('invalid number of grades, please insert again');
  gradeAmount = Number(prompt('insert a number between 1 and 5'));
}

A possible solution is to use do...while .一个可能的解决方案是使用do...while This code will keep display alert('invalid number of grades, please insert again') and window.prompt until it meets you condition.此代码将一直显示alert('invalid number of grades, please insert again')window.prompt直到满足您的条件。 Then it will window.prompt for user to enter the number.然后它会 window.prompt 让用户输入数字。

 let gradeAmount = Number(prompt('insert a number between 1 and 5')); const grades = []; do{ alert('invalid number of grades, please insert again'); gradeAmount = Number(prompt('insert a number between 1 and 5')); } while(gradeAmount <= 1 && gradeAmount >= 5) for (let i = 0; i < gradeAmount; i++) { const grade = Number(prompt('insert the grade')); grades.push(grade); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM