Doing an exercise and I can't figure out why my while loop isn't escaping, it keeps saying that the values aren't valid even when only digits are entered. Any ideas?
// 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>"); }
At the point where where you test the regular expressions, those variables age
and restingPulse
are undefined
. You'll have to move the code for testing to within the loop, after those variables have received values from user input:
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.");
}
}
Also, you can drop the === true
bits, and use the truth value of the variable directly for testing.
I've simplified and flexified Your code ;)
Check this out:
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>"); }
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>"); }
you need to convert your check code to a function instead a variable.
You were on the right track you just forgot to initialize the variable.
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>"); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.