[JS Practice] Multiplication table

Title :

Multiplication table

Description:

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) {
	var arr = new Array(size);
	if(size > 0) {
		for(var i=1; i<=size; i++) {
			arr[i-1] = new Array(size);
			for(var j=1; j<=size; j++) {
				arr[i-1][j-1] = i*j;
			}
		}
		return arr;
	} else {
		return false;
	}
   };

 

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s