1

I'm trying to return the largest number in each of the sub-arrays - but this is returning the first two values of each sub-array. Seems pretty simple and yet I can't find where I go wrong.

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

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

This code returns the following: [ 4, 5, 13, 27, 32, 35, 37, 39, 1000, 1001 ]. Where am I going wrong? I don't want to use .sort()

3
  • 4
    What is the value of maxVal when i becomes 1? What should it be? Commented Jun 23, 2021 at 18:04
  • 1
    Also, when are you adding maxVal into newArr? When should you save the biggest value in the array into your result? Commented Jun 23, 2021 at 18:07
  • @ScottHunter should increment by 1? i think I've figured this out.... Commented Jun 23, 2021 at 18:07

3 Answers 3

2

The problem is, you push a new value whenever a new max value is found and not deleting the previously pushed value from the current set, you need to store the maxVal until the end of the inner loop and push the value afterwards, so you only push one value for each array, which is actually the biggest one

Your function should look similar to this:

function largestOfFour (arr) {
  let maxVal = 0;
  let newArr = [];

  for (i = 0; i < arr.length; i++) {
    // reset maxVal, for new set of numbers
    maxVal = 0;
    for (j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > maxVal) {
        // new largest number found
        maxVal = arr[i][j];
      }
    }
    // push highest number found in set
    newArr.push(maxVal);
  }
  return newArr
}
Sign up to request clarification or add additional context in comments.

Comments

2

I don't know whether this is allowed, but it will get you the result:

const arr=[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

console.log(arr.map(a=>Math.max(...a)))

Comments

0

I prefer @CarstenMassmann's answer; however, just so you can understand how your code was in error, but very close, see this edit to the original attempt.

That inner conditional doesn't find the max of the sub-array, if finds the interim max. Don't push to your results inside the condition, finish iterating the sub-array and then push the best interim max...

function largestOfFour (arr) {
  let maxVal = 0
  let newArr = []
  for (i = 0; i < arr.length; i++) {
    for (j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > maxVal) {
        maxVal = arr[i][j]
        // don't push here. maxVal is just the max *for now*
      }
    }
    newArr.push(maxVal) // push here
  }
  return newArr
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.