'return-largest-numbers-in-arrays' : Little HELP please [SOLVED]

'return-largest-numbers-in-arrays' : Little HELP please [SOLVED]
0

#1

Guys wanna point out where i am making mistake !. All the conditions got matched except the last one i can’t figure out a way to find the largest number among negative numbers.please check my code.help me with this.

Here’s my code for this algo :

function largestOfFour(arr) {
  let newArr=[];
  for(let i=0;i<arr.length;i++){
    let flag=0;
    for(let j=0;j<arr[i].length;j++){
      if(arr[i][j]>flag){
        flag=arr[i][j];
      }
    }
    newArr.push(flag);
  }
  return newArr;
}

Link to the challenge:


#2

Look into this portion of the code here, specifically your flag and your IF condition, to why the fourth test is failing.

Try printing out the results of the last test case as well.

    let flag=0;
    for(let j=0;j<arr[i].length;j++){
      if(arr[i][j]>flag){
        flag=arr[i][j];
      }
    }

#3

I see ! So you are suggesting to log out the last condition as : else condition of the program. That might work but i can’t figure out the algo for that. i mean i can’t just console.log the rest of the part.I have to find the largest number among the negative ones. :thinking:


#4

Take a look at the values of the last test case again.

Try running through the loop in your head and validate if the IF conditions checks out against the flag.

Hope this helps.


#5

Thanks for the base ideas man.i figured out few algo :point_down:and this worked best for me. :slight_smile:

array = [-72, -3, -17, -10]
console.log(Math.max(...array));

So i basically went to my personal console and tried to work only on the negative array specifically and got this algo working. :blush:


#6

No problem.

I hope that you realized, with the flag being set to 0 initially, it will cause the IF statement to always result in a FALSE when it checks the array of negative numbers.

Good luck on the rest.


#7

Yeah i got it now :slight_smile: thanks again for the support.