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
-
3you are returning from while loop.Amit Verma– Amit Verma2021年06月24日 03:11:46 +00:00Commented Jun 24, 2021 at 3:11
3 Answers 3
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
Sign up to request clarification or add additional context in comments.
1 Comment
17xande
That’s a good answer, adding a short code snippet would help the questioner.
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
Unmitigated
91.5k12 gold badges103 silver badges109 bronze badges
Comments
Try putting return statement outside the while loop.
Comments
lang-js