Tell us what’s happening:
Dear all,
I’m wondering why the following code won’t work. Instead, the code next will work.
-
Why can’t I put
let largestNum = arr[i][0];
after
for (let j=0; j<arr[i].length; j++){
-
Where should I put
return ans;
. It seems like it matters thatreturn ans;
should only be added before certain curly bracket, but I’m often confused after which one.
Thanks for helpingYour code so far
function largestOfFour(arr) {
let ans =[];
for(let i= 0; i< arr.length; i++){
for (let j=0; j<arr[i].length; j++){
let largestNum = arr[i][0];
if (arr[i][j]> largestNum){
largestNum= arr[i][j];
}
} ans[i] = largestNum
}
return ans;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
function largestOfFour(arr) {
let ans =[];
for(let i= 0; i< arr.length; i++){
let largestNum = arr[i][0];
for (let j=0; j<arr[i].length; j++){
if (arr[i][j]> largestNum){
largestNum= arr[i][j];
}
} ans[i] = largestNum
}
return ans;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15
Challenge: Return Largest Numbers in Arrays
Link to the challenge: