Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. const largestOfFour = (arr) => {
  2. let newArray = []; /* declare a a varible assign an empty array */
  3. for (let i = 0; i < arr.length; i++) { /* first loop through the first level array to determine its length*/
  4. let firstNum = arr[i][0]; /*because we have a Multidimensional Array declare a
  5. variable and capture [0] element of each inner array */
  6. for (let j = 0; j < arr[i].length; j++) { /*loop through the inner array's length */
  7. if (arr[i][j] > firstNum) { /* use an if statement to determime if each element of our inner array
  8. is greater than the first element assigned to our firstNum variable */
  9. firstNum = arr[i][j]; /* if it greater replace the previous element with a new greater number */
  10. }
  11. }
  12. newArray[i] = firstNum; /* assign our new elements to create a newArray of greater numbers */
  13. }
  14. return newArray; /*return a new array with greater values */
  15. }
  16.  
  17. 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