Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JS Return Largest Numbers in Array

So here is my code challenge everyone. I think I am really close to solving this problem, and I would like to figure it out on my own without anyone posting the answer unless I ask.

When my code runs it stores the two greatest numbers from each array and adds them to newArray. I want it to only store the greatest value from each array and add it to newArray. I kind of understand why it is doing this, but I don't know how to fix it. I appreciate any advice on this.

Challenge: Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays. Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

function largestOfFour(arr) {
  var arrayValue = 0;
  var newArray = [];
  var oldArray = arr;

  for (i = 0; i < oldArray.length; i ++) {
    for (j = 0; j < oldArray[i].length; j ++) {
      if (oldArray[i][j] > arrayValue) {
        arrayValue = oldArray[i][j];
        newArray.push(arrayValue);
      }
    }
  }
  return newArray;
}

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

Hi Trevor, I suggest you to look into Math.max() function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

I managed to solve your issue using above function and ES6 with much less code just now, let me know if you want to see it!

Yeah post your code that would be great

function largestOfFour(arr) {
    let newArr = [];
    arr.forEach((a) => {
      newArr.push(Math.max.apply(null, a));
    });
    return newArr;
  }

lmk if you have any questions

Your code is a bit advanced for me. I don't know what let is, or forEach, or =>

Sure,

This is ES6, the updated syntax of JS (i think that's what it is in a nutshell), there are few courses on T3H about it, I strongly suggest you look into them.

Good luck with coding!

2 Answers

Ah okay. You should scope arrayValue so that its inside the loop (because your not going to use it outside of the loop) and it gets reset after each loop. Also you should put var next to i and j so that you don't accidentally use it outside and get an unintended bug.

function largestOfFour(arr) {

  var newArray = [];
  var oldArray = arr;

  for (var i = 0; i < oldArray.length; i ++) {
   var arrayValue = 0;
    for (var j = 0; j < oldArray[i].length; j ++) {
      if (oldArray[i][j] > arrayValue) {
        arrayValue = oldArray[i][j];
      }
    }
        newArray.push(arrayValue);
  }
  return newArray;
}

Why did moving the variable inside of the loop change the result like that?

Technically you could have done it your way and just do arrayValue = 0 inside but this is much cleaner code .This is the concept of scope. Variables declared (using var) inside of a "scope" can't be access outside of the scope. Usually you can tell the scope by these brackets { }.

function justAnyFunction(){
     var a = 3;
}
console.log(a);
//you get something like undefined because you're outside the brackets and a was declared inside.

if(4 > 3){
    var b = 3;
}
console.log(b);
//you get something like undefined because you're outside the brackets and b was declared inside.

var c; 
if(4 > 3){
    c = 3;
}
console.log(c);
//you get 3 because you're outside the brackets and c was declared outside the brackets too.

As for forloops, everything declared inside the forloop gets destroyed each time one iteration finishes so a new arrayValue gets created and set to 0. In yours the arrayValue does not get its value reset after going through each array because its declared in a higher scope (outside the brackets).

You can push the new Value at the end outside of the inner loop so that you only push a max value once.

  for (i = 0; i < oldArray.length; i ++) {
    for (j = 0; j < oldArray[i].length; j ++) {
      if (oldArray[i][j] > arrayValue) {
        arrayValue = oldArray[i][j];
      }
    }
        newArray.push(arrayValue);     
  }

That helps but it still doesn't work. It doesn't pass when you switch the first and second sub arrays like this.

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

and returns this

[27, 27, 39, 1001]