So here what happening I'm trying to make a function that return the number i enter in the function parameter * the number of loop iteration i expect
// 1 * 5 = 5
// 2 * 5 = 10
// 3 * 5 = 15
// 4 * 5 = 20
// 5 * 5 = 25
// 6 * 5 = 30
// 7 * 5 = 35
// 8 * 5 = 40
// 9 * 5 = 45
// 10 * 5 = 50
but i only get // 1 * 5 = 5
i know that return stop the loop from iterating but i can't figure out how to make the loop continue
function multiTable(number) {
//loop from 1 to 10
for(let i = 1; i <= 10; i++){
//create a string with the calculation
let sum = (`${i} * ${number} = ${number * i}\n`);
//return the sum
return sum
//this is where the error happen it only return the first iteration (1 * 1 = 1)
// i expect it to return 1 * 1 = 1, 1 * 2 = 2 all the way up to 10
}
}
multiTable(5);
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment