Solution should work, I think (Return Largest Numbers in Arrays)

Tell us what’s happening:
Why doesn’t this solution work?
My answer
fcc
Your code so far


function largestOfFour(arr) {
  let temp = [...arr];
  let finalArr = [];
  for(let i=0; i<arr.length; i++){
    for(let j=0; j<arr[i].length; j++){
      if(arr[i][j]>temp[i][0]){
        finalArr[i] = arr[i][j];
      }
    }
  }
  console.log(finalArr);
  return finalArr;
}

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/75.0.3770.100 Safari/537.36.

Link to the challenge:

I think it’s because of the way you’re constructing the temporary variables to track the largest number. Your first line, in effect, creates a copy of arr - which is a two-dimensional array. Whereas your ultimate desired answer is supposed to be a one-dimensional array. This leads you to get confused with assignments into the temp variable - for example, in line:
temp[i] = arr[i][j];
what you’re getting out of array is a single number and you put it into temp[i] - but before this operation, temp[i] used to contain an array of numbers. This then makes temp[i][0] stop working.

I would suggest you create temp as a single-dimension array instead, maybe initialise it with zeroes, and then update every time when you encounter a number larger than what’s in the array.

UPDATE: I see you changed the code in question slightly - makes this answer not fit your current code.

Thanks for your input, please see my updated query (I figured out this after I posted this question).

In your second attempt, you’re not comparing the variable in final with encountered number, but always with the first element in the subarray (temp[i][0]) - that’s what makes you return e.g. 26 instead of 27. Just ditch the temp array and compare directly what you have stored in the final result and update as you go, and you’ll get there :slight_smile:
PS: It might help you to add console.log ("Comparing " + arr[i][j] + " to " + temp[i][0]) next to your if statement to see what’s happening.

1 Like

Thank-you so much for your help :slight_smile:

My solution, although I admit that I peeked into the get hint section but I am amazed that I didn’t figure out this before, I wasn’t updating the value in temp[i][o] in if section and it was staring right at my face, gotta be writing code with full concentration next time

function largestOfFour(arr) {
  let temp = [...arr];
  let finalArr = [];
  for(let i=0; i<arr.length; i++){
    for(let j=0; j<arr[i].length; j++){
      if(arr[i][j]>temp[i][0]){
        temp[i][0] = arr[i][j];
      }
    }
    finalArr[i] = temp[i][0];
  }
  console.log(finalArr);
  return finalArr;
}

@maverick_2k19 It is really not necessary to post your final solution. In fact, we would prefer you not to avoid spoiling the challenge for others.

Thanks.

I added the spoiler tags in it, thanks.

In the future, just let us know you solved it and not post (even if blurred).

Thanks

The intent behind posting this solution was to facilitate the campers who were stuck in this problem. In future I will keep that in mind, thanks.

The solutions are in the Guide which can be accessed by clicking the “Get a Hint” button the challenge. Thanks for understanding.