Help with return largest number in Array

Can someone help me with pushing the values into an Array? The values I have are correct and that part seems to work but when I try creating another variable to push the numbers in it doesnt seem to work…

Will I pass the tests once I figure out how to do that or will the order of the values in the returned array affect it?

  **Your code so far**

function largestOfFour(arr) {

let longestValue = [];

for (let i = 0; i < arr.length; i++){
  arr[i].sort(function(a, b){return b-a})
 console.log(longestValue = arr[i][0]);
 
 
 
}



return arr;
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

If you want to add numbers to an array, you have to use .push().
Right now you are creating an array to put the values in, but you overwrite it with the values, instead of adding them.
Also you need to return the array with the new values, not the original array.

I have already tried doing this:

function largestOfFour(arr) {

 

let longestValue = [];

let longestArray = [];

  for (let i = 0; i < arr.length; i++){

    arr[i].sort(function(a, b){return b-a})

   console.log(longestValue.push(arr[i][0]));

            

  }

but it doesnt seem to work, it only returns 1 2 3 4

and I have also tried this:

function largestOfFour(arr) {

 

let longestValue = [];

let longestArray = [];

  for (let i = 0; i < arr.length; i++){

    arr[i].sort(function(a, b){return b-a})

   longestValue = (arr[i][0]);

   return longestArray.push(longestValue)

            

  }

but it also doesnt seem to work

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

I don’t see where you are returning the array containing the largest values.

1 Like

return longestArray.push(longestValue)

is this not it?

That is a return statement inside of the loop. That will

  1. stop the entire function on the very first loop iteration

  2. only return the ‘return value’ of the push() method

So you don’t return an array.

2 Likes

return longestArray.push(longestValue)
The brackets there indicate that this is in itself a function - and that function does something AND THEN has a return-value the same way your function works.

By the way, same issue with your console.log() within the loop → this is also a function and within it you call another function and the console.log() will create an output in the console of the RETURN VALUE of the function.
That’s why you see single numbers there → because the .push() method* return the new length of the array.

*a method is a function that is directly built into an object and can only be called via the dot at the end of the object

2 Likes

so how would I only return the array after the final iteration?

If you want to return after the loop, then your return statement should be after the loop.

1 Like

But what I am trying to achieve is put the longestValue in the longestArray, I dont understand why the push() method is giving me a length and not accessing the arr[i][0] value instead? Is it only giving me the value of i? Through every iteration?

.push() is a function. Functions have a return value. The push() function/method changes the array and then returns the new number of elements in the array:

2 Likes

I dont know why i assumed if I used returned the array outside of the for loop that meant it would only return if the for loop was not executed… I think possibly from the if statement…

Just to clarify the return statement should be outside of the for loop in order to return the value after it has been executed? And if you wish to return the value initially then you must add a return to the for loop ?

The function immediately stops whenever the very first return statement is encountered.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.