0

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].

10
  • 1
    In ES6 you can do array.map(arr => Math.max(...arr)) Commented Apr 25, 2019 at 13:25
  • 1
    Your function doesn't return anything, so the array your create dies when the function exits. Commented Apr 25, 2019 at 13:25
  • 1
    It's not clear what you're asking here. Your best bet here is to use your debugger, step through the code, and watch what happens. Commented Apr 25, 2019 at 13:27
  • 1
    @Oversought check out my answer to get some idea Commented Apr 25, 2019 at 13:38
  • 1
    @Oversought - Here's the documentation for Chrome. It's similar in other browsers. Commented Apr 25, 2019 at 13:59

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yea, I did miss that. Thank you. :)
Although your solution isn't working completely. I am not sure why, hopefully someone can explain.
Apparently the var maxNum = 0 should be put at the beginning of the first for loop. I don't know why, but that seems to be the consensus above.
@Oversought, yep I got it, thanks for pointing out. It is because if the max of the previous sub array is greater than all the elements of the next sub array it will push the previous max only. Hope you get me. I have updated the answer
dope thx, appreciate it, I posted my solution below that was made with everyone's help. Hopefully it is of use to people.
0

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]]));

8 Comments

Thank you for showing me how I can make my line of logic for coding out this algorithm work. I appreciate it, rather than just saying I am wrong completely and to do it a different way. Could you explain how moving where maxNum is defined changes it to the correct solution?
How would I get this to work if they used a negative number? I have some ideas but don't know hot to elucidate them easily. Obviously setting var maxNum = 0 is this issue with negative numbers here.
@Oversought, You have to reset maxNum(maxNum=0) for each subarray(if subarray contains positive numbers) to get maxNum of that subarray.
I'm so confused with this website. Looks like someone downvoted your solution. That's not right. It works, doesn't it? Just doesn't work if all numbers in subarray are negative, but I'm working on that atm.
@Oversought I have updated my answer and that will work with negative numbers too....Every youtube videos(having more than 1000 views) have dislike counts...I hope you got my point :)
|
0

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;
}

1 Comment

It may not be the most elegant, but it follows my initial line of logic and understanding, so that's good.
0

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.

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.