Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 825 Bytes

matrix-creation.md

File metadata and controls

44 lines (31 loc) · 825 Bytes

Matrix creation 7 Kyu

LINK TO THE KATA - FUNDAMENTALS ARRAYS MATRIX LINEAR ALGEBRA MATHEMATICS LANGUAGE FEATURES

Description

Create an identity matrix of the specified size( >= 0).

Some examples:

(1)  =>  [[1]]

(2) => [ [1,0],
         [0,1] ]

       [ [1,0,0,0,0],
         [0,1,0,0,0],
(5) =>   [0,0,1,0,0],
         [0,0,0,1,0],
         [0,0,0,0,1] ]

Solution

const getMatrix = number => {
  const result = []

  for (let i = 0; i < number; i++) {
    const subArray = Array(number).fill(0)
    subArray[i] = 1

    result.push(subArray)
  }

  return result
}