Hey it’s my second time going through these exercises and I knew I had to nest a for loop but without looking at the solution too much I thought I’d just post this.
Seems my biggest issue is with how to run through a nested array with a nested for loop. I saw the answer has a different variable name etc. Can you explain?
Update: Thought it may be better to post the parts of answer I don’t understand in the code:
function largestOfFour(arr) {
var results = [];
for (var i = 0; i < arr.length; i++) {
var largestNumber = arr[i][0]; // why do we keep this 0 here?
for (var ii = 1; ii < arr[i].length; ii++) { // why do we start at 1 not 0?
if (arr[i][ii] > largestNumber) {
largestNumber = arr[i][ii];
}
}
results[i] = largestNumber; //
}
return results;
}
Your code so far
function largestOfFour(arr) {
var largestArr = [];// initialize a variable to hold the largest value
for (var i = 0; i < arr.length; i++) {
largestArr = arr[i].length;
for (var i = 0; i < arr[i].length; i++) // this is the part I don't understand
{
if (arr[i].length > largestArr) {
largestArr = arr[i];
}
}
return largestArr; // return the largest value
}
console.log(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_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.
Well, the goal is to find the “biggest” in the set.
When looping the first element (the element “0’”) is of course the biggest one as we have yet to look at the others and see if that case still holds true.
But is a safe assumption even if it’s the only element in the set.
So it make sense to actually start comparing the first element (0) with the second (1), hence why starting from 1 as index.
I thought the first for loop was about checking the index of outer array (starting at 0), then the second was checking the index of inner array (starting at 0)
[i] would be [4, 5, 1, 3]… and then 0 would be 4… then [13, 27, 18, 26] and 0 would be 13 right?
arr[i][ii] would be [4, 5, 1, 3] then 1 would be 5?
So 4,13,32,1000 is the first loop… Sorry is there a place to find the expanded explanation of this?
For this problem, I like thinking about how I would do this by hand.
Let’s look at a single array.
[5, 2, 8, 42]
I would start at the beginning.
[5, ?, ?, ?]
biggest = 5
Then I’d look at the next number
[5, 2, ?, ?]
biggest = 5 // still
And the next
[5, 2, 8, ?]
biggest = 8 // changed!
And the last
[5, 2, 8, 42]
biggest = 42 // changed!
Your code replicates how I would do this by hand.
// The biggest is the first I see
let largestNumber = arr[0];
// Check (loop over) the rest of the numbers
for (let i = 1; i < arr.length; i++) {
// Update the biggest seen so far if I find a bigger
if (arr[i] > largestNumber) {
largestNumber = arr[i];
}
}
The problem makes this more complex by adding nested arrays, which is where the outer loop comes in.
Yeah so is because the loop is within a for loop it would not move on to the next subarray until it goes through each index of the sub array?
So
for (var i = 0; i < arr.length; i++) { // goes through each subarray
var largestNumber = arr[i][0]; // stores 4 as largestNumber
for (var ii = 1; ii < arr[i].length; ii++) {
if (arr[i][ii] > largestNumber) { // if arr[0][1] aka 5 > 4
largestNumber = arr[i][ii]; // largestNumber = 5
}
}
results[i] = largestNumber; //
}
I think the loop part within a loop is my biggest issue. How it’s looping is probably confusing me