[05.09.22] Coding test

Juyeon.it·2022년 5월 9일
0

Coding test

목록 보기
28/32

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 answer

multiplicationTable = function(size) {
  let result = [];
  
  for (let i = 1; i <= size; i++) {
    let pushedArr = [];
    for (let m = 1; m <= size; m++) {
      pushedArr.push(m * i);
    }
    result.push(pushedArr);
  }
  return result;
}

Other solutions

multiplicationTable = function(size) {
  var result = [];

  for (var i = 0; i < size; i++) {
    result[i] = [];
    for(var j = 0; j < size; j++) {
      result[i][j] = (i + 1) * (j + 1);
    }
  }
  
  return result
}
multiplicationTable = function(size) {

  return Array.apply(null, new Array(size)).map(function(val, i) {
    return Array.apply(null, new Array(size)).map(function(val, j) {
      return (i+1) * (j+1);
    });
  });
}

Wrap up

apply function

func.apply(thisArg, [argsArray])

Parameters

thisArg

The value of this provided for the call to func.

argsArray(optional)

An array-like object, specifying the arguments with which func should be called, or null or undefined if no arguments should be provided to the function.

Return value

The result of calling the function with the specified this value and arguments.

Examples

const array = ['a', 'b'];
const elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); // ["a", "b", 0, 1, 2]
// min/max number in an array
const numbers = [5, 6, 2, 3, 7];

// using Math.min/Math.max apply
let max = Math.max.apply(null, numbers);
// This about equal to Math.max(numbers[0], ...)
// or Math.max(5, 6, ...)

let min = Math.min.apply(null, numbers);

source: mdn web docs

0개의 댓글

관련 채용 정보