Loops - Exercise 5
Question 1: Build a Multiplication Grid Completed
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)
function makeMultiplicationGrid(rows, columns) {
// Write your solution here
}
console.log(makeMultiplicationGrid(2, 3));function makeMultiplicationGrid(rows, columns) {
const grid = [];
for (let row = 1; row <= rows; row++) { const line = []; for (let col = 1; col <= columns; col++) line.push(row * col); grid.push(line); }
return grid;
}
console.log(makeMultiplicationGrid(2, 3));Question 2: Sum All Matrix Values Completed
Return the sum of the numbers in a two-dimensional array, including empty rows.
Expected output: 6 (print this with console.log)
function sumMatrixValues(matrix) {
// Write your solution here
}
console.log(sumMatrixValues([[1,2],[3]]));function sumMatrixValues(matrix) {
let total = 0;
for (const row of matrix) for (const value of row) total += value;
return total;
}
console.log(sumMatrixValues([[1,2],[3]]));Question 3: Count Matrix Entries Completed
Return the total number of entries across all rows of a two-dimensional array.
Expected output: 3 (print this with console.log)
function countMatrixEntries(matrix) {
// Write your solution here
}
console.log(countMatrixEntries([[1,2],[],[3]]));function countMatrixEntries(matrix) {
let count = 0;
for (const row of matrix) for (const value of row) count++;
return count;
}
console.log(countMatrixEntries([[1,2],[],[3]]));Question 4: Create Row Sums Completed
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)
function sumEachMatrixRow(matrix) {
// Write your solution here
}
console.log(sumEachMatrixRow([[1,2],[4],[]]));function sumEachMatrixRow(matrix) {
const sums = [];
for (const row of matrix) { let total = 0; for (const value of row) total += value; sums.push(total); }
return sums;
}
console.log(sumEachMatrixRow([[1,2],[4],[]]));Question 5: Flatten One Matrix Level Completed
Return a new array containing the entries of each row in order.
Expected output: [1,2,3] (print this with console.log)
function flattenMatrixRows(matrix) {
// Write your solution here
}
console.log(flattenMatrixRows([[1,2],[],[3]]));function flattenMatrixRows(matrix) {
const result = [];
for (const row of matrix) for (const value of row) result.push(value);
return result;
}
console.log(flattenMatrixRows([[1,2],[],[3]]));Question 6: Find a Value in a Matrix Completed
Return true if any matrix entry strictly equals the supplied value.
Expected output: true (print this with console.log)
function matrixContainsValue(matrix, wanted) {
// Write your solution here
}
console.log(matrixContainsValue([[1,2],[3]], 3));function matrixContainsValue(matrix, wanted) {
for (const row of matrix) for (const value of row) if (value === wanted) return true;
return false;
}
console.log(matrixContainsValue([[1,2],[3]], 3));Question 7: Create a Coordinate Grid Completed
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)
function makeCoordinateGrid(rows, columns) {
// Write your solution here
}
console.log(makeCoordinateGrid(2, 2));function makeCoordinateGrid(rows, columns) {
const grid = [];
for (let row = 0; row < rows; row++) { const line = []; for (let col = 0; col < columns; col++) line.push([row, col]); grid.push(line); }
return grid;
}
console.log(makeCoordinateGrid(2, 2));Question 8: Count Matrix Matches Completed
Return the number of matrix entries strictly equal to the supplied value.
Expected output: 3 (print this with console.log)
function countMatchingMatrixValues(matrix, wanted) {
// Write your solution here
}
console.log(countMatchingMatrixValues([[1,2],[1,1]], 1));function countMatchingMatrixValues(matrix, wanted) {
let count = 0;
for (const row of matrix) for (const value of row) if (value === wanted) count++;
return count;
}
console.log(countMatchingMatrixValues([[1,2],[1,1]], 1));Question 9: Repeat Each Word Completed
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)
function repeatEachLoopWord(words, count) {
// Write your solution here
}
console.log(repeatEachLoopWord(["a","b"], 2));function repeatEachLoopWord(words, count) {
const result = [];
for (const word of words) for (let i = 0; i < count; i++) result.push(word);
return result;
}
console.log(repeatEachLoopWord(["a","b"], 2));Question 10: Find the Longest Matrix Row Completed
Return the largest row length in a two-dimensional array, or zero for an empty matrix.
Expected output: 3 (print this with console.log)
function longestMatrixRowLength(matrix) {
// Write your solution here
}
console.log(longestMatrixRowLength([[1],[2,3,4],[]]));function longestMatrixRowLength(matrix) {
let longest = 0;
for (const row of matrix) if (row.length > longest) longest = row.length;
return longest;
}
console.log(longestMatrixRowLength([[1],[2,3,4],[]]));