0

I have been asked to make a prompt to enter your current weight and target weight. The loop should calculate how many weeks it will take to reach target weight at loss of 1.38 a week. My prompts work but the loop breaks the javascript. My code is below, can someone help me out? I am new to javscript by the way.

 var current_weight = prompt("Please enter your current weight",0.0);
 var target_weight = prompt("Please enter your target weight",0.0);
 var weeks = 0;
 if(current_weight > 0 target_weight > 0){
 if(current_weight > target_weight){
 while(current_weight <= target weight){
 var current_weight = current_weight – 1.38;
 weeks = weeks + 1;
 alert("it will take" + weeks + "weeks to hit target");
 }
 }
 else if(current_weight > 0 && target_weight > 1){
 alert("Current weight must be more than target weight");
 }
 else{ 
 alert("Input error")
 }
</script>

The loop breaks the javascript.

3
  • if current_weight > target_weight, why would the while (current_weight <= target_weight) ever evaluate to true? recheck your logic and adjust the conditions accordingly. Commented May 18, 2020 at 19:19
  • 1
    Hi interesting, so both conditions can't be true so the loop won't execute. if (current_weight > target_weight) {while( current_weight <= target weight) { ... Commented May 18, 2020 at 19:21
  • Is some logic operator missing in if(current_weight > 0 target_weight > 0){ ? Commented May 18, 2020 at 19:54

1 Answer 1

1

4 problems in your code:

  • missing && operator between the conditions in first if statement

  • prompt returns the user entered value as string. So you can't subtract 1.38 from current_weight as current_weight will be a string. You need to convert user entered values in to numbers

  • There's a logical error in your code. You have this if statement if(current_weight > target_weight) and if it evaluates to true, then while loop checks while(current_weight <= target weight) which is opposite to the condition of the wrapping if statement. Consequently, your loop will never run. You probably want loop condition to be current_weight > target_weight

  • alert should be outside the while loop

var current_weight = Number(prompt("Please enter your current weight", 0.0));
var target_weight = Number(prompt("Please enter your target weight", 0.0));
var weeks = 0;
if (current_weight > 0 && target_weight > 0) {
 if (current_weight > target_weight) {
 while (current_weight > target_weight) {
 current_weight = current_weight - 1.38;
 weeks = weeks + 1;
 }
 alert("it will take" + weeks + "weeks to hit target");
 } else if (current_weight > 0 && target_weight > 1) {
 alert("Current weight must be more than target weight");
 } else {
 alert("Input error");
 }
}

answered May 18, 2020 at 19:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.