Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const largestOfFour = (arr) => {
- let newArray = []; /* declare a a varible assign an empty array */
- for (let i = 0; i < arr.length; i++) { /* first loop through the first level array to determine its length*/
- let firstNum = arr[i][0]; /*because we have a Multidimensional Array declare a
- variable and capture [0] element of each inner array */
- for (let j = 0; j < arr[i].length; j++) { /*loop through the inner array's length */
- if (arr[i][j] > firstNum) { /* use an if statement to determime if each element of our inner array
- is greater than the first element assigned to our firstNum variable */
- firstNum = arr[i][j]; /* if it greater replace the previous element with a new greater number */
- }
- }
- newArray[i] = firstNum; /* assign our new elements to create a newArray of greater numbers */
- }
- return newArray; /*return a new array with greater values */
- }
- largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Add Comment
Please, Sign In to add comment