function largestOfFour(arr) {
var largestNumber = [0,0,0,0];
for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {
largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
}
}
}
return largestNumber;
}
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);
what is wrong in my code ?
I could not understand why I can’t return [25, 48, 21, -3] in my code.
I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Hi,
Because -3 is not greater than 0.
In that test case the zero you prepacked your array with was the largest number tested.
2 Likes
The question is repeated.
Tell us what’s happening:
I could not return [25, 48, 21, -3]. What is my wrong ?
Your code so far
function largestOfFour(arr) {
var largestNumber = [0, 0, 0, 0];
for (var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
for (var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
if (arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {
largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
}
}
}
return large…
2 Likes