Tell us what’s happening:
I’m doing this challenge again because I want to figure out how to use nested loops to figure this out. My logic is flawed, but I’m not sure how to navigate around it. I’m not looking on any other websites for clues or hints and trying to figure this out for myself.
That said, a gesture, a nudge would be appreciated.
Your code so far
function largestOfFour(arr) {
//initialize variables
var array = [];
var i;
var s;
//iterate through arrays
for(i=0;arr.length>i;i++){
for(s=0;s[i].length<4;s++){
//set length of subarray to be greater than subarrays in order to capture largest of subarrays
if(s[i].length>s){
//assign this to array
array = s[i].length;
}
}
}
return array;
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/
If s
is an just an incrementing integer, what do you expect s[i]
to be?
For example, the first time your inner loop runs it’s going to check if 0[0].length < 4
.
I’m not sure why this isn’t working.
The console says “Cannot read property of ‘0’ of undefined.”
I’m assuming they’re referring to var i, var s But I initialize these at the top. I’m not sure what I’m missing. Please help.
function largestOfFour(arr) {
//create container for largest arrays
var array = [];
//initialize variables
var i;
var s;
var len;
//loop through array and subarrays
for(i=0;arr.length>i;i++){
for(s=0;s<arr[s].length;s++){
//the number obtained by discerning the largest numer in each subarray through comparison with previous number in loop(which is contained in array[i])
if(arr[i][s]>array[i]) {
//the largest numbers in each array achieved through iterating through them and indicating the largest numbers in each array through comparing them to each other and assigned to empty container array and then returned below.
array[i] = arr[i][s];
}
}
}
return array;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Apologies for making such a long thread, but I’m stuck on one part of the code that I’m sure is a silly oversight but I’d like some extra eyes to look at it:
function largestOfFour(arr) {
var result = [];
for(var i=0;i<arr.length;i++){
var largestNum = arr[i][0]
for(var j=0;j<arr[i].length;j++){
if(largestNum < arr[i][j]){
result = arr[i][j];
}
}
console.log(result)
}
//return result;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
The issue is that the subarray is drawing from the final element of subarray[1]. Everything else loops through and settles on the largest element except for this loop. I checked my loops and I don’t see any reason why it would allow that to happen. Can anyone tell me what I’m doing wrong?