TransWikia.com

Loop inside the function that accepts the parameter

Stack Overflow Asked by Abi wardani on January 18, 2021

let printNumber = (num) => {
for (let i = 0; i <= num.length; i++){
  num += i;
}
return num
}
 console.log(printNumber(4));

the output should be from 0 to num like examples
output:
0
1
2
3
4

how should I fix my code?

One Answer

Simply change your code to

let printNumber = (num) => {
for (let i = 0; i < num+1; i++){
  console.log(i)
}
}
printNumber(4);

This will give you your desired output.. 1 you were returning numbers which was not required as you can console log from the function 2 the parameter was a number not a array that you need to use .length function 3 if this solved your problem click the tick mark button beside my answer

Correct answer by Alpha Wolf Gamer on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP