Return largest number array challenge!

Return largest number array challenge!
0

#1

Here is my code!

function largestOfFour(arr) {
  // You can do this!
  var retArray=[];
  var max=0;
  
  for(i=0;i<arr.length;i++)
    {
      for(p=0;p<arr[i].length;p++)
        {
          if(arr[i][p]>max)
            {
              max=arr[i][p];
              
              retArray=arr[i];
              
            }
          
        }
    }
  

  return retArray;
}

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [2, 35, 3, 3], [10, 11, 7, 1]]);

It is showing correct answer in the console,but not passing the testcases.


#2

While I am looking at your code, I would appreciate it if you could edit your post to make it easier to read.

Highlight everything that is code, then look at the little toolbar above the place where you write your message. Click the button that looks like </>, then save.

EDIT: In addition, it would be very helpful if you could comment your code so I can see what you are thinking at each step.

EDIT 2: It seems to me like you are misunderstanding the question. You don’t need to return the sub-array with the biggest element in it. You need to return an array containing the biggest element of EACH of the arrays.


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#4

What the problem ask for is to return the array that is make up from the largest number of it’s own array. Your retArray must have 4 number that are selected from each sub arrays. This mean your retArray must equal to [27, 5, 35, 11] for your given test case.

In order to do that, you must first search the largest number within sub array and push it to your retArray.

What you are doing right now is searching the largest number from all given number and returning the whole array that contains the largest number. Your retNumber is holding [2, 35, 3, 3].

P.S >> I’m a non-english speaker and I’m sorry if I make mistake and I can’t say cleanly. Have fun and good luck!


#5

Thanks will keep this in mind before posting a question next time:)


#6

Thanks:), sorry i misunderstood the question!


#7

Thanks for the advise!


#8
function largestOfFour(arr) {
  var array = arr;
  var finalArray = [];
  for (var i = 0; i < array.length; i++) {  
    var biggestCurrentNumber = 0;
    for (var n = 0; n < array[i].length; n++) {

      if (array[i][n] > biggestCurrentNumber) {

        biggestCurrentNumber = array[i][n];
      }
    }
    finalArray.push(biggestCurrentNumber);

  }
  return finalArray;
}

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [2, 35, 3, 3], [10, 11, 7, 1]]);


#9

You are correct. You need to reset the biggest number for each new array.


#10

This is exactly what I needed-- my largest number var was up top and not resetting. As soon as I moved it down… :raised_hands: Thanks for posting this :slight_smile:


#11
function biggestNum(arr) {
  var max = arr[0];
  arr.forEach(function (el) {
    if (el > max) max = el;
  });
  return max;
}

function biggestCollection(arrs) {
  return arrs.map(function (arr) {
    return biggestNum(arr);
  });
}

#12

function largestOfFour(arr) { for(var i=0; i<arr.length; i++){ arr[i] = arr[i].reduce(function(large,current){ return current > large ? current : large; },-1); } return arr; }

and another one
function largestOfFour(arr) { return arr.map(x=>x.reduce(function(large,current){ return current > large ? current : large; },-1)); }[details=Summary]This text will be hidden[/details]


#13

You could just sort and then take the latest. It´s sad because the sort in JS by default is alphabetical so you need to pass a sortFunction to the method but yeah… In general terms this solution is the best in other languages since normally the sort under the hood uses a really good algorithm

function largestOfFour(arr){
return arr.sort((a,b) => a - b )[arr.length -1];
}


#14

I used sort exactly in this way and ignored the comment about having “only four elements in each array for simplicity” by taking the last element after sorted. It seems to be passing any test and even matches the expected output under the instructions, but only passes for returning an array.

var newArr = [];
var n; var x;

function largestOfFour(arr) {
  for (n = 0; n < arr.length; n++) {
    var rep = []; var i;
    rep = arr[n].sort(function(a,b) { return a - b;});
    i = rep.pop();
    newArr.push(i);
  
  }
  
  return newArr;
}

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

I was about to sort the final array as well after noticing I was getting the largest of each array, but realized that doesn’t seem necessary in the solution, so does anyone know what’s wrong here?


#15

Sort should be super quick in this case @jdmerinor - if the array is all integers, most JS engines will switch to using a very fast C integer sort afaik

Also

function largestOfFour(arr){
  const largestNumInArr = (nums) => Math.max(...nums);
  return arr.map(largestNumInArr);
}

#16

Hello everyone, I’m getting stuck with this one. I’ve read in google other ways to solve this, and looked at the codes here but I wrote my own which passes all the test cases when I log it to the console in Sublime but don’t understand why it won’t pass on the website. Is it because I’m not using comparison operators?

Here’s my code:
// new array to be returned
var newArray = [];

function largestOfFour(arr) {
// for loop to iterate through the array
for (i = 0; i < arr.length; i++) {
// using max and apply functions to extract largest number and storing them
var largest = Math.max.apply(Math, arr[i]);
// pushing into an array to be returned
newArray.push(largest);
}
return newArray
}

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


#17

this was answered here


#18

Thank you. Dumb mistake on my part.


#19

Hi,

My code is not passing however it shows the correct answer.
Please help me out.

var array = [];


function largestOfFour(arr) {
  for (var i = 0; i < arr.length; i++){
    var lgN = arr[i].sort(
      function(a, b) {
        return a - b;
      }).slice(-1);
    
    array.push(lgN[0]);
  }
  return array;
}

Thank you


#20

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2