AKL012
AKL012

Reputation: 399

while loop does not break out

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>");
    }

Upvotes: 2

Views: 80

Answers (4)

Sarbartha Rana
Sarbartha Rana

Reputation: 31

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>");
}

Upvotes: 0

num8er
num8er

Reputation: 19372

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>");
}

Upvotes: 1

Huan Jiang
Huan Jiang

Reputation: 257

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.

Upvotes: 1

cs95
cs95

Reputation: 402323

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.

Upvotes: 1

Related Questions