Build the Largest Number Finder - Build the Largest Number Finder

Tell us what’s happening:

I’ve been trying for days to figure out the logic to complete this lab. I think I’m getting close, but I’m stuck. Could someone give me some guidance or a hint? Thanks

Your code so far

function largestOfAll(arr) {
  arr = [...arr];
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j in (arr[i])) {
      if (j > Math.max(arr[i])) {
      newArr.push(j);
      return ([newArr]); 
    }
  }
  return arr;
}
}

Your browser information:

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

Challenge Information:

Build the Largest Number Finder - Build the Largest Number Finder

Hi @jfo0707

Looks like you are returning the original array.

Happy coding

Thank you. Yes, I can’t figure out how to correct it. I tried returning newArr, but it’s not working.

How is it not working?

Does it return the original array?

What does it return?

It returns undefined.

What is your updated code?

What debugging have you tried with this new code?

is for_in the best type of loop? console.log(j) to check the values you are getting.

The Math.max() static method returns the largest of the numbers given as input parameters

does this use of Math.max() help you find the largest number in the current array?

No, I’m confused as to when to call what. I keep trying different configurations but I feel like I’m just going in circles. Should I be able to solve this with a simple for loop?

it is possible to solve this using only loops and if-statements, but there are always multiple ways to solve something

Depends on the for loop you choose. Did you console.log(j) to see if this loop is giving you the values in the array?

This is one of the experiments I’m trying to dig down to an array of the largest numbers from each array.

isn’t j an index, and arr[i] an array? how would this comparison work?

Yeah, I see your point. Thanks. I’ll see what else I can do to get the largest number in each array. I was thinking that if I can sort each array, then then pop the last element into a new array, it might work.

Yes. That approach could work with just one loop.

1 Like
const arr = [1, 2, 3];
const max = Math.max(...arr);

This is one of the examples from the Math.max() link I sent you earlier. It shows another approach you could use.

1 Like

I’ve gotten as far as being able to isolate one array, but it’s not an array of the largest numbers.

\`function largestOfAll(arr) {

 let newArr;

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

      const max = Math.max(arr);

      arr = arr.toSorted();

      newArr = arr.pop();

      return newArr.toReversed();

  }

      return \[newArr\];

}

console.log(largestOfAll(\[\[4, 9, 1, 3\], \[13, 35, 18, 26\], \[32, 35, 97, 39\], \[1000000, 1001, 857, 1\]\]));\`
`// running tests
4. largestOfAll([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001].
5. largestOfAll([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
6. largestOfAll([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
// tests completed
// console output
[ 3, 1, 9, 4 ]`

this looks suspiciously similar to the first array of the input

what happens when return runs?

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

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

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

Silly question here - what exactly is your plan here, without any code terminology?

You shouldn’t be looking for syntax to save your- the syntax is there is meet your plan

Yeah sorry. I thought I was supposed to just surround the post with a pair of backticks like interpolating.