做一个练习,我无法弄清楚为什么我的while循环没有逃脱,它一直说即使只输入数字,这些值也是无效的。有什么想法吗?
// 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>");
}
&#13;
答案 0 :(得分:1)
在测试正则表达式的位置,这些变量age
和restingPulse
为undefined
。您必须将用于测试的代码移动到循环内,在这些变量从用户输入接收到值之后:
while(!valid) {
age = parseFloat(prompt("Enter your age:"));
restingPulse = parseFloat(prompt("Enter your resting heart rate:"));
numCheckAge = /^\d+$/.test(age);
numCheckPulse = /^\d+$/.test(restingPulse);
if(numCheckAge && numCheckPulse) {
valid = true;
}
else {
alert("Sorry. That's not a valid input, please enter numbers.");
}
}
此外,您可以删除=== true
位,并直接使用变量的真值进行测试。
答案 1 :(得分:1)
我简化并灵活化了您的代码;)
检查出来:
function requireNumericPrompt(text) {
var value;
while(true) {
if(/^\d+$/.test(value = prompt(text))) {
return value;
}
alert("Sorry. Please enter valid (numeric) value.");
}
}
var age = parseInt(requireNumericPrompt("Enter your age:"));
var restingPulse = parseInt(requireNumericPrompt("Enter your resting heart rate:"));
document.write('Age: ' + age + '<br/>');
document.write('Resting pulse: ' + restingPulse + '<br/>');
for (var targetRate, 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>");
}
答案 2 :(得分:1)
var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid;
valid = false;
numCheckAge =function(a) {
return /^\d+$/.test(a);
}
numCheckPulse = function(a){
return /^\d+$/.test(restingPulse);
}
while(valid === false) {
age = parseFloat(prompt("Enter your age:"));
restingPulse = parseFloat(prompt("Enter your resting heart rate:"));
if(numCheckAge(age) && numCheckPulse(restingPulse)) {
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>");
}
&#13;
您需要将支票代码转换为函数而不是变量。
答案 3 :(得分:0)
你在正确的轨道上忘记了初始化变量。
var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid;
age = 0;
restingPulse = 0;
valid = false;
numCheckAge = /^\d+$/.test(age);
numCheckPulse = /^\d+$/.test(restingPulse);
while(valid !== true) {
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>");
}