Generation

generate functionSat, 11 Mar 2023

Given a (m x n) 2d array count the number of 5's in it.

Script function count5s(arr) { var count = 0; // Loop through the array for (var i = 0; i < arr.length; i++) { // Check each row for (var j = 0; j < arr[i].length; j++) { // Check each value if (arr[i][j] === 5) { count++; } } } return count; } count5s([[1, 2, 5], [5, 5, 5], [5, 5, 5], [9, 5, 9]]);

Questions about programming?Chat with your personal AI assistant