Loops - Exercise 5

Question 1: Build a Multiplication Grid

Return a rows-by-columns array of arrays where each cell is its one-based row number multiplied by its one-based column number. Both dimensions are non-negative integers.

Expected output: [[1,2,3],[2,4,6]] (print this with console.log)

javascript

function makeMultiplicationGrid(rows, columns) {
  // Write your solution here
}

console.log(makeMultiplicationGrid(2, 3));

Question 2: Sum All Matrix Values

Return the sum of the numbers in a two-dimensional array, including empty rows.

Expected output: 6 (print this with console.log)

javascript

function sumMatrixValues(matrix) {
  // Write your solution here
}

console.log(sumMatrixValues([[1,2],[3]]));

Question 3: Count Matrix Entries

Return the total number of entries across all rows of a two-dimensional array.

Expected output: 3 (print this with console.log)

javascript

function countMatrixEntries(matrix) {
  // Write your solution here
}

console.log(countMatrixEntries([[1,2],[],[3]]));

Question 4: Create Row Sums

Return an array containing the sum of each numeric matrix row, including zero for an empty row.

Expected output: [3,4,0] (print this with console.log)

javascript

function sumEachMatrixRow(matrix) {
  // Write your solution here
}

console.log(sumEachMatrixRow([[1,2],[4],[]]));

Question 5: Flatten One Matrix Level

Return a new array containing the entries of each row in order.

Expected output: [1,2,3] (print this with console.log)

javascript

function flattenMatrixRows(matrix) {
  // Write your solution here
}

console.log(flattenMatrixRows([[1,2],[],[3]]));

Question 6: Find a Value in a Matrix

Return true if any matrix entry strictly equals the supplied value.

Expected output: true (print this with console.log)

javascript

function matrixContainsValue(matrix, wanted) {
  // Write your solution here
}

console.log(matrixContainsValue([[1,2],[3]], 3));

Question 7: Create a Coordinate Grid

Return a rows-by-columns matrix of [row, column] pairs using zero-based indices.

Expected output: [[[0,0],[0,1]],[[1,0],[1,1]]] (print this with console.log)

javascript

function makeCoordinateGrid(rows, columns) {
  // Write your solution here
}

console.log(makeCoordinateGrid(2, 2));

Question 8: Count Matrix Matches

Return the number of matrix entries strictly equal to the supplied value.

Expected output: 3 (print this with console.log)

javascript

function countMatchingMatrixValues(matrix, wanted) {
  // Write your solution here
}

console.log(countMatchingMatrixValues([[1,2],[1,1]], 1));

Question 9: Repeat Each Word

Return an array containing each input word repeated count times consecutively. Count is non-negative.

Expected output: ["a","a","b","b"] (print this with console.log)

javascript

function repeatEachLoopWord(words, count) {
  // Write your solution here
}

console.log(repeatEachLoopWord(["a","b"], 2));

Question 10: Find the Longest Matrix Row

Return the largest row length in a two-dimensional array, or zero for an empty matrix.

Expected output: 3 (print this with console.log)

javascript

function longestMatrixRowLength(matrix) {
  // Write your solution here
}

console.log(longestMatrixRowLength([[1],[2,3,4],[]]));