Oversought
Oversought

Reputation: 67

Return Max number from each sub-array of array into new array

I am doing a freecodecamp algorithm scripting problem, and I've gone as far as my understanding will allow. The challenge at hand is to take an array full of 4 sub-arrays of numbers, search through it with javascript, and return a new array with the max value of each sub-array. I've seen their solution and understand it decently, but I've been working on my own solution using nested for loops and a ternary operator. I'll display their solution first, then my faulty solution, where it is saying that the function with arguments is undefined.

Below is the code:

Their Solution:

function largestOfFour(arr) {
  var results = [];
  for (var n = 0; n < arr.length; n++) {
    var largestNumber = arr[n][0];
    for (var sb = 1; sb < arr[n].length; sb++) {
      if (arr[n][sb] > largestNumber) {
        largestNumber = arr[n][sb];
      }
    }

    results[n] = largestNumber;
  }

  return results;
}

The solution that I am working on (currently doesn't work):

function largestOfFour(arr) {
  var maxNum = 0;
  var results = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      arr[i][j] > maxNum ? results.push(arr[i][j]) : delete arr[i][j]; 
    }
  }
}

For example,

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

should display an array of [5, 27, 39, 1001].

Upvotes: 0

Views: 69

Answers (4)

Niraj Kaushal
Niraj Kaushal

Reputation: 1502

Note: You can solve this problem with two lines of code in ES6

You can see this working example to see where you are doing wrong,

function largestOfFour(arr) {
 //var maxNum = 0;
  var results = [];
  for (let i = 0; i < arr.length; i++) {
    var maxNum = null;
    for (let j = 0; j < arr[i].length; j++) { 
      //You can use ternary operator here 
      if (maxNum) {
        if (arr[i][j] > maxNum) {
          maxNum = arr[i][j];
        }
      }
      else {
        maxNum = arr[i][j];
      }
      
    }
    results.push(maxNum);
  }
  
  return results;
}

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

Upvotes: 0

Oversought
Oversought

Reputation: 67

I went back through the Basic Algorithm Scripting section to get a more thorough re-examination of the problems, as to learn the material better. The second time I re-wrote this, I ended up using a very similar if not exact-same approach to a solution given in this thread. Some things might be a little different, so I thought I would post my second solution. Again, this can be solved in two lines of code using ES6, but that's not as fun. :)

SOLUTION:

function largestOfFour(arr) {
  var finalArr = [];

  for (let i = 0; i < arr.length; i++) {
    var maxCounter = -Infinity;
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > maxCounter) {
        maxCounter = arr[i][j];
      }
    }
    finalArr.push(maxCounter);
  }

  return finalArr;
}

You can see here that -Infinity is used as a 'number' instead of undefined, as in my first posted solution. I think that cleared up some unnecessary space of code as well, and I like how it works logically more too.

Upvotes: 0

Ashutosh Hans
Ashutosh Hans

Reputation: 9

function largestOfFour(arr) {
  //var maxNum = 0; updated
  var results = [];
  for (let i = 0; i < arr.length; i++) {
    var maxNum = -Infinity; //updated
    for (let j = 0; j < arr[i].length; j++) {
      if(arr[i][j] > maxNum){
        maxNum = arr[i][j];
      }
    }
    results.push(maxNum);
  }
 return results;
}
largestOfFour([[2,4,6],[45,56,78]]); //[6, 78]

hey, you can modify it this way. You might have missed updating the maxNum and after innerLoop ends, we can push the maximum in the results array.

Upvotes: 1

Oversought
Oversought

Reputation: 67

HERE IS MY SOLUTION WITH HELP OF PEOPLE IN THREAD:

function largestOfFour(arr) {
  var results = [];
  for (let i = 0; i < arr.length; i++) {
    var maxNum = undefined;
    for (let j = 0; j < arr[i].length; j++) {
      if (maxNum) {
        arr[i][j] > maxNum ? maxNum = arr[i][j] : delete arr[i][j];
      } else {
        maxNum = arr[i][j];
      }
    }
    results.push(maxNum);
  }
  return results;
}

Upvotes: 0

Related Questions