LargestOfFour doesn't work vhen trying to do array.push(MaxNum))

function largestOfFour(arr) {
  let maxNum = 0;
  let maxNum1=0;
  const smallArray =[];
  const bigArray=[];
  for (let i=0 ; i<arr.length; i++){
     let maxNum = arr[i][0];
    for (let j=0 ; j<arr[i].length; j++){
      maxNum1=arr[i][j]
        if(arr[i][j] > maxNum){
        maxNum1 = arr[i][j];
        arr[i]++;
        smallArray.push(maxNum1);
        console.log(smallArray);
      }
    //bigArray[i] = maxNum; // This line works but would like to know if it is possible to resolve in pushing values in the arrays
    
    }
       
    bigArray.push(smallArray);
  }

  return bigArray;
}

let result = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
console.log(result);

Please post a link to the challenge.

Thanks for your help, here is the link :
Basic Algorithm Scripting: Return Largest Numbers in Arrays | freeCodeCamp.org

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Can you rephrase your question? I am not sure I understand it.

Sure!
Sorry for the late reply, I didn’t notice the question.
So it was basically to know if we could have the same result by pushing the values in bigArray.
Thank you for your help @hbar1st

the push method adds to the end of the array. So long as that works with your algorithm, then it should do the same as bigArray[i] = maxNum;

That is, if you are adding maxNum in each iteration of the loop from i=0 to the end then yes push should work exactly the same.

Yeah, thanks a lot @hbar1st , I could figure it out later, but I literally need to come back on the exercise, step by step.

1 Like
function largestOfFour(arr) {
  let largestArray = [];
  

  for(let array in arr){
    console.log(arr[array]);
    largestArray.push(Math.max(...arr[array]))
  }

  return largestArray;
}

let x = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
console.log(x)[Preformatted text](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays)

I think this is a better code @hbar1st … Thanks for your help

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

Okay, thanks…
So we can’t give solutions here right ?

yes pls avoid posting solutions unless it is to discuss something related to them.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.