0

Doing a while loop in JavaScript but it's displaying 120 when usernum < 50 but calculating when usernum is 60.

var userNum = 4; // Code will be tested with values: 4, 10 and 60
/* Your solution goes here */
do {
  userNum = 2 * userNum;
  console.log(userNum);
}
while (userNum < 50);

CORRECT Testing displayed output with userNum = 4 Yours 8 16 32 64

CORRECT Testing displayed output with userNum = 10 Yours 20 40 80

INCORRECT Testing displayed output with userNum = 60 Yours and expected differ. See highlights below. Yours 120 Expected Expected no output

1
  • while is not the same as do-while. Commented Feb 18, 2019 at 3:28

1 Answer 1

3

A do while loop runs the code once, and then tests the condition, if it is false, it stops the execution. You should use while or for loop.

while(userNum < 50){
userNum = 2 * userNum;
console.log(userNum);
}

while -> check condition, if true run code, else stop

for -> check condition, if true run code (basically shorter version of while)

do while -> run loop, check condition, if true run again or exit;

Sign up to request clarification or add additional context in comments.

1 Comment

You got it.. Nice

Your Answer

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.