做練習,我想不通爲什麼我的while循環不逃避,它口口聲聲說的值是無效的,即使是隻輸入數字。有任何想法嗎?爲什麼我的while循環沒有逃脫? (JavaScript)的
// 31. Karvonen Heart Rate
var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid;
valid = false;
numCheckAge = /^\d+$/.test(age);
numCheckPulse = /^\d+$/.test(restingPulse);
while(valid === false) {
age = parseFloat(prompt("Enter your age:"));
restingPulse = parseFloat(prompt("Enter your resting heart rate:"));
if(numCheckAge === true && numCheckPulse === true) {
valid = true;
} else {
alert("Sorry. That's not a valid input, please enter numbers.");
}
}
for (var i = 0.55; i < 1; i+=0.05){
targetRate = (((220 - age) - restingPulse) * i) + restingPulse;
document.write("Intensity: " + Math.round((i * 100)) + "% | Rate: " + Math.round(targetRate) + "<br>");
}
您需要執行的循環內的正則表達式的'test's(age'和'restingPulse'已進入後')。 – jcaron
使用調試器會立即顯示'numCheckAge'和'numCheckPulse'設置只有一次,也不會更新以後... – jcaron
另外,你可以只使用'isNaN(年齡)',而不是正則表達式來檢查,如果輸入是一個號碼 –