Get Largest Numbers in Array from Sub Arrays


#1

Here is the solution I have written for this solution

function largestOfFour(arr) {
      // You can do this!
      var len = arr.length;
      var newArr = [];
      var max = -1;
      
      for(var i= 0; i < len; i++){
     
      for(var j=0; j < arr[i].length; j++) { 
        if(max < arr[i][j]){
          max = arr[i][j];
      }
    }
   newArr.push(max);
  }
   return newArr;
}

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

now when I declare var outside the loop, the first function invocation works but the second one gives an error

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

but when I declare var inside the loop it works what is teh main concept and how does JS scoping work here.


#2

Scope of a variable is usually within a block i.e. curly braces. Can you mention which variable declaration gave the error? If you could post the error trace here, it would help with debugging the exact issue.


#3

Yes. Thanks.

When var max = -1 is outside loop, first invocation runs fine
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); // gives the desired output

But this does not work
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);// 27, 27, 39, 1001
should be 27, 5, 39, 1001


#4

when it is declared inside the loop, program runs fine and complete.


#5

The statement var max=-1; should be inside the first for loop. That is because the max variable has to be reset to -1 every time a new list is encountered. Scope in JS works within a body of curly braces and also when you explicitly declare a variable with var.

In your example, if you reverse the order of the list items it would not work if the variable declaration for max is outside the for loop. This is because the maximum value in the complete list is already present in the 1st list of the reversed list of lists.


#6

Thanks

Can you please elaborate with an example about this

In your example, if you reverse the order of the list items it would not work if the variable declaration for max is outside the for loop. This is because the maximum value in the complete list is already present in the 1st list of the reversed list of lists.

if var max is outside the loop, it already has value stored in it with first invocation.


#7

Lets make the call as largestOfFour([[1000, 1001, 857, 1], [32, 35, 37, 39], [13, 27, 18, 26], [4, 5, 1, 3]]).
if max is defined outside the 1st for loop, its values would be as below:
max=1001 after 1st iteration
max=1001 after 2nd iteration
max=1001 after 3rd iteration
max=1001 after 4th iteration

whereas if it is within the 1st for loop but just above the 2nd for loop, its values would be :
max=1001 after 1st iteration
max=39 after 2nd iteration
max=27 after 3rd iteration
max=5 after 4th iteration

This happens because the max variable is reset each time an inner list is checked whereas in the previous case that does not happen.


#8

Thanks noted

one quick question, I was testing a program to find out

Check if a string (first argument, str) ends with the given target string (second argument, target).

I did this it did not work
      if(str.substr(-1) === target) {
        return true;
    }else{
        return false;
    }
    }

But when I did this it worked. After checking on web
      if(str.substr(-target.length) === target) {
        return true;
    }else{
        return false;
    }
    }

if say target length is 20
so what you are saying is -20 === "string"
or can you explain


#9

For checking if a string ends with a target string, you should be using str.endsWith() instead of substr. Check out http://www.w3schools.com/jsref/jsref_endswith.asp for more details.


#10

Thanks Allen

Is this a robust way of doing?

Why do you think substr is not a good method to use.

Are you taking based on performance or other parameters


#11

This function was built for checking whether it ends with the target string. Substr was intended for more general splicing. If you wanted to use substr, you should pass in the starting index too.