I’ve read through other solutions, check StackOverflow, and have looked at the hint, but my brain is hurting trying to understand why my solution doesn’t work and why the other solutions do.
Essentially I have 2 loops right after each other, but I’ve separated them out so that I can check as I go that I’m getting the values I want. I’m doing this with console.logs
and also with assignments.
The console messages seem kind of right, but I don’t get the right solution.
What I’ve noticed is people using multi-dimensional arrays, like arr[i][n]
. But I think I already have this by assigning arr[i]
to eachArr
, and then using eachArr[n]
. Why does this not work?
function largestOfFour(arr) {
let eachArr = 0;
let val = 0;
// set 2 values to 0
let newArr = [];
//go through an array
for (let i = 0; i < arr.length; i++) {
console.log("This arr: " + i);
// console.log(arr.length);
//get all the values for each sub array
eachArr = arr[i];
console.log("Items for arr " + i + " is " + eachArr);
// console.log("the array position is: " + pos);
for (let n = 0; n < 5; n++) {
console.log(eachArr[n]);
if (val < eachArr[n]) {
//if val is less than each arr value
val = eachArr[n];
//set value to value of arr
console.log("New Value is " + val);
newArr.push(val);
}
}
}
return newArr;
// console.log(newArr);
// return arr;
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
]);
UPDATE: I’m considering this solved on my end. I tried going through the solution, to figure out where I went wrong. And I haven’t been able to run the FCC basic solution through JS Bin without error. When I add some of the changes from the FCC solution to my code, it generates other errors. So, I’m moving on, or maybe I’ll ask on StackOverflow. Thanks if you tried to help.