Need Help - Basic Algorithm Scripting - Return Largest Numbers in Arrays

Need Help - Basic Algorithm Scripting - Return Largest Numbers in Arrays
0

#1

Hi, my code works fine with first array examples, but not with last array that has negative numbers. Could you help me to find a solution. I would like to find a work around with my current code, and not by using, forEach or .map or maht.max methods. Thank you in advance.
Here is my code:

function largestOfFour(arr) {
  
  let arrOfBigNum = [];

  // You can do this!
  for(let i = 0; i<arr.length; i++) {
    let prevArrItemVal = 0;
    let currentArrItemVal = 0;

    for(let j = 0; j<arr[i].length; j++) {
      currentArrItemVal = arr[i][j];

      if(currentArrItemVal > prevArrItemVal) {
        prevArrItemVal = currentArrItemVal;
      }
    }
    arrOfBigNum.push(prevArrItemVal);
  }
  return arrOfBigNum;
}

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

#2

I know that the problem comes when the iteration goes into the array [-72, -3, -17, -10].
The prevArrItemVal = 0 at this point and will always be bigger from the current item of the array, because all numbers are negatives.
So the result I take is [25, 48, 21, 0] instead of [25, 48, 21, -3]


#3

@AnastasiaEvgenia Can you check to see if the number is negative and then do something different if it is, like change the prevArrItemVal to the first negative number and go from there?


#4

Hi thank you for your reply, I tried to do something like that but didn’t work. I am still trying though to find a solution.


#5

@AnastasiaEvgenia Cool. I know for sure that it can work, so keep trying!


#6

Hi I managed to solve it finally like that

function largestOfFour(arr) {
  
  let arrOfBigNum = [];

  // You can do this!
  for(let i = 0; i<arr.length; i++) {
    let prevArrItemVal;
    let currentArrItemVal = 0;

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

      if(currentArrItemVal > prevArrItemVal) {
        prevArrItemVal = currentArrItemVal;
      }
    }
    arrOfBigNum.push(prevArrItemVal);
  }
  return arrOfBigNum;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

So simple but took me all afternoon. :scream::scream:


#7

I had the same problem. I realised that setting the variable I used to store the largest number back to 0 was preventing my code from working as it didn’t allow for negative values. My solution was…

function largestOfFour(arr) {

let newArr = [];

for (let i = 0; i < arr.length; i++) {

  let max = -Infinity;

    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > max) {
        max = arr[i][j];
      } 
    }
  console.log('Max = ' + max);
  newArr.push(max);
}
console.log('New Arr = ' + newArr);
return newArr;
}

I put console.log() in a couple of times to see what was being outputted.


Write Higher Order Arrow Functions Challenge too Difficult