Wednesday, 13 January 2016

Kata Day 48: Multiplication Tables


Problem Description: Create a function that accepts dimensions, of Rows x Columns, as parameters in order to create a multiplication table sized according to the given dimensions. **The return value of the function must be an array,and the numbers must be Fixnums, NOT strings.

Example:

multiplication_table(3,3)

1 2 3
2 4 6
3 6 9

-->[[1,2,3],[2,4,6],[3,6,9]]

Each value on the table should be equal to the value of multiplying the number in its first row times the number in its first column.

Level:6kyu

Link To Kata: Multiplication Tables

In this kata we accept as an input the number of rows and columns as our input and we have to return a multiplication table according to the given dimensions.

We declare an empty array as ans. We loop from 1 till the end of the given input row.For every iteration we declare an empty sub-array. Then we loop from 1 to the given input column and multiply the row and column and push the product to sub-array. After complete iteration of columns we push the sub-array to ans array and continue the iteration with next row.

In the end we return the ans array as the required multiplication table.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 48

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment