DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#30 - Multiplication table CodeWars Kata (6 kyu)

Instructions

Your task, is to create NxN multiplication table, of size provided in parameter.

for example, when given size is 3:

1 2 3
2 4 6
3 6 9

for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]]


My solution:

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
}

Enter fullscreen mode Exit fullscreen mode

Explanation

First I declarated the variable "r" with an empty array, wich will contain the last result.

After that I used a for loop to iterate the array, and for every iteration I did a "x" variable with an empty array and another for loop, inside of this loop I will iterate through the size value, and in every iteration I will push to x the result of the multiplication of i by j, that way for example if I'm in the first value of the array in the first for loop, I will always be equal to 1 in the second loop, but j will be changing in every iteration, so I can get [1*1,1*2,1*3] in the x array, and at the end I just returned r


What do you think about this solution? πŸ‘‡πŸ€”

My Github
My twitter
Solve this Kata

Top comments (0)