[英]Do while loop in JavaScript stops looping when condition is not met
I wrote a do while loop but it does not keep looping even though the condition is not met. 我编写了一个do while循环,但是即使不满足条件,它也不会一直循环。 There in an array called quotes and I am comparing the input number to the length of that array to make sure it only modifies an element already there.
在一个称为引号的数组中,我正在将输入数字与该数组的长度进行比较,以确保仅修改已经存在的元素。 Even though I add a higher number, it just adds it to that array element.
即使我增加了一个数字,它也只是将其添加到该数组元素中。
function modifyQuote(){
'use strict'
var modifyQuoteNum = 0
do{
var inputModify = prompt("what quote do you want to modify?");
modifyQuoteNum = (parseInt(inputModify) - 1);
}
while ((typeof modifyQuoteNum == 'number') &&
((modifyQuoteNum + 1) <= quotes.length));
Figured it out, got mixed up. 想通了,很困惑。
function modifyQuote(){
'use strict'
var inputModify = prompt("what quote do you want to modify?");
var modifyQuoteNum = (parseInt(inputModify) - 1);
while ((typeof modifyQuoteNum != 'number') ||
((modifyQuoteNum + 1) > quotes.length)){
var inputModify = prompt("what quote do you want to modify?");
var modifyQuoteNum = (parseInt(inputModify) - 1);
}
It looks like you got your condition the wrong way round. 看来您的病情有误。
it does not keep looping even though the condition is not met
它不保留,即使条件不满足循环
That's how while
loops work: they only keep looping while the condition is met. 这就是如何
while
循环工作:当条件满足时 ,他们只是不断循环。
The fix is trivial, so I'll let you figure out what it is as an exercise. 该修复程序很简单,因此我将让您弄清楚它是什么练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.