arrays - Simple javascript program -
i'm getting started on learning javascript, , i'm creating simple program takes largest numbers array, , put them in new array, returned in end.
the function called largestof(), , example,
largestof([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001].
what have far this, , don't know how fix it, or if has way utilizing brackets.
function largestof(arr) { var narr = []; (var = 0; < arr.length; i++) { n = arr[i].length; max = 0; for(var j = 0; j < n; j ++) { if (arr[i][j] > max) { max = arr[i][j]; narr.push(max); } } } return narr; }
what trying here pretty straightforward. i'm running through every block in array, picking max, , putting max in own array (narr) other max's.
i want know how fix have while still doing way.
thank you
function largestof(arr) { var narr = []; (var = 0; < arr.length; i++) { var n = arr[i].length; var max = 0; (var j = 0; j < n; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } narr.push(max); // push max outside of inner loop } return narr; }
Comments
Post a Comment