← Back to challenges

Infection of the Ones

JavaScriptHardarrayslanguage_fundamentals

Instructions

Write a function that replaces every row and column that contains at least one 1 into a row/column that is filled entirely with 1s.

Examples

onesInfection([
  [0, 0, 1],
  [0, 0, 0],
  [0, 0, 0]
]) ➞ [
  [1, 1, 1],
  [0, 0, 1],
  [0, 0, 1]
]

onesInfection([
  [1, 0, 1, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 0]
]) ➞ [
  [1, 1, 1, 1],
  [1, 1, 1, 1],
  [1, 1, 1, 0]
]

onesInfection([
  [0, 1, 0, 1],
  [0, 0, 0, 0],
  [0, 1, 0, 0]
]) ➞ [
  [1, 1, 1, 1],
  [0, 1, 0, 1],
  [1, 1, 1, 1]
]

Notes

  • You must mutate the original array.
  • Input matrices will have at least row and one column.
  • Bonus: Solve this without using any higher-order functions.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.