Creating a multiplication table of a size provided in a parameter.
I did a code wars problem.
Here are the instructions:
Your task is to create an N×N multiplication table, of the size provided in the parameter.
For example, when the given size
is 3:
1 2 3
2 4 6
3 6 9
For the given example, the return value should be:
[[1,2,3],[2,4,6],[3,6,9]]
Here is the solution:
First, you have to declare an array that will hold the value for the created multiplication table.
let r = []
You then have to use a loop that ensures only the value up to the specified parameter is taken and each of the integers extracted from 1 up to the value of the parameter is multiplied to create the multiplication table.
for(let i = 1; i<=size; i++){
}
Finally, just take the values from the generated after multiplying each digit and push them to the array we created earlier.
for(let j = 1; j<=size; j++){
x.push(i*j)
}
Note: we have two arrays, one to hold each value of each digit being multiplied and one to hold the whole combined values/ multiplication table.
Here is what the final result should look like.
multiplicationTable = function(size) {
let r = []
for(let i = 1; i<=size; i++){
let x = []
for(let j = 1; j<=size; j++){
x.push(i*j)
}
r.push(x)
}
return r
}