1

function timesTable(num1) {
 var counter = 0;
 while (counter < num1) {
 counter +=1
 return (counter + ' * ' + num1 + ' = ' + counter * num1);
 
 }
}
console.log(timesTable(9));

the while loop is not iterating all it does is show 1 * 9 = 0 counter only increases once and doesn't show next line?? what do I do to fix this

asked Jun 24, 2021 at 3:09
1
  • 3
    you are returning from while loop. Commented Jun 24, 2021 at 3:11

3 Answers 3

2

The issue here is that you are returning inside the loop, meaning that the counter variable increments once and then the statement is returned. To fix this, put the return statement outside the while loop

answered Jun 24, 2021 at 3:16
Sign up to request clarification or add additional context in comments.

1 Comment

That’s a good answer, adding a short code snippet would help the questioner.
2

You should build a string to return at the end (outside the loop) instead of returning inside the loop.

function timesTable(num1) {
 var counter = 0, res = "";
 while (counter < num1) {
 counter += 1
 res += counter + ' * ' + num1 + ' = ' + counter * num1 + '\n';
 }
 return res;
}
console.log(timesTable(9));

answered Jun 24, 2021 at 3:16

Comments

1

Try putting return statement outside the while loop.

answered Jun 24, 2021 at 3:29

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.