I tried answering a simple question in code kata where the question goes like this:
Your goal is to return multiplication table for number that is always an integer from 1 to 10.
For example, a multiplication table (string) for number == 5
looks like below:
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
here is the code I did but I used console.log
function multiTable(number) {
// good luck
let arr = [1,2,3,4,5,6,7,8,9,10];
for(i = 0; i < arr.length; i++) {
let answer = arr[i] * number;
console.log(arr[i] + '*' + number + '=' + answer);
}
}
When I try it in the console window using dev tools it runs okay, but when I change console.log
to return
it only returns the last element multiplied to the function parameter i.e. if number == 5
, it only returns 10*5=50
and not the whole iteration. Why is this happening?