← Back to challenges

Matrix Multiplication (Part 2)

JavaScriptHardalgorithmsarraysloopsmath

Instructions

Create a function that multiplies two matrices (n x m) and (p x q) and returns:

  • "invalid" if the matrices are not multiplicable (i.e. if m is not equal to p).
  • The multiplication matrix (n x q) otherwise.

Examples

matrixMultiply([[1, 2]], [[3], [4]]) ➞ [[11]]

matrixMultiply([[0, 0], [0, 1]], [[1, 2], [3, 4], [5, 6]]) ➞ "invalid"

matrixMultiply([[4, 2], [3, 1]], [[5, 6], [3, 8]]) ➞ [[26, 40], [18, 26]]

Notes

This challenge is a generalized version of Matrix Multiplication.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.