Build the Largest Number Finder - Build the Largest Number Finder

Tell us what’s happening:

Is this solution going fine? I don’t know if im doing it correctly and I pass the tests 1,2 and 3. But I want to know if there’s a way that I can know if im on the right path

Your code so far

function largestOfAll (arr){
 let array = []
 
  for (let i = 0; i < arr.length; i++){
     let max1 = arr[i];
    if (arr[i] > max1){
      max1 = arr[i]
    }
}
for (let j = 0; j <arr.length; j++){
  let max2= arr[j];
  if(arr[j] > max2){
    max2 = arr[j]
  }
}
 
return array
}
console.log(largestOfAll([[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/143.0.0.0 Safari/537.36

Challenge Information:

Build the Largest Number Finder - Build the Largest Number Finder

Hi @mio_min_mio

Your function will always return an empty array.

Happy coding

And for it to not return and empty array should I define it before the for function?

function largestOfAll (arr){
 let array = [0,0]
  for (let i = 0; i < array.length; i++){
     let max1 = array[i];
    if (array[i] > max1){
      max1 = array[i]
    }
}
for (let j = 0; j <array.length; j++){
  let max2= array[j];
  if(arr[j] > max2){
    max2 = array[j]
  }
}
 
return array
}

If Im doing this im closer to solving it?

The first for loop is iterating through the array defined at the start of the function.

What do you want to do with the array your function defines?

I want that it iterates and takes the largest number that is introduced in the array so that it completes what is asked in the excersice

function largestOfAll (arr){
 let array = [0,0]
  for (let i = 0; i < array.length; i++){
  for(let j = 0; j < array.length; j++){
     let max1 = array[i];
     let max2 = array[j];
    if (array[i] > max1){
      max1 = array[i]
    }
    if (array[j] > max2){
      max2 = array[j]
    }
return arr
  }
}
}

I think of something like this, but I don’t know how to translate it to a code solution

arr is a parameter, array is the array containing two zeros.

Are your loops accessing the array passed to the function?

So if I put it like this would it be more closer to the solution?

function largestOfAll (arr){
let array = [i,j]
  for (let i = 0; i < arr.length; i++){
  for(let j = 0; j < arr.length; j++){
     let max1 = arr[i];
     let max2 = arr[j];
    if (array[i] > max1){
      max1 = array[i]
    }
    if (array[j] > max2){
      max2 = array[j]
    }

  }
} 
return arr
}
console.log(largestOfAll([[4, 5, 1, 3], 1]))

Sorry for not understanding that well, im having a bit of trouble understanding some things with the arrays and the loops.

If I put it like this it tells me that I is not defined and I suspect that it would tell me the same for j, but why isn’t it defined if I already put that they are equal to zero and will be incrementing their counter until they find a major number, didn’t I?

ReferenceError: i is not defined

i and j are defined in the loops.

You are using them in the array variable, before they are initialised.

Ok, so I move them and then they are defined, but I don’t know how to progress from there

function largestOfAll (arr){
  for (let i = 0; i < arr.length; i++){
  for(let j = 0; j < arr.length; j++){
    let array = [i,j]
     let max1 = arr[i];
     let max2 = arr[j];
    if (array[i] > max1){
      max1 = array[i]
    }
    if (array[j] > max2){
      max2 = array[j]
    }

  }
} 
return arr
}
console.log(largestOfAll([[4, 5, 1, 3], 1]))

largestOfAll([[4, 6, 1, 3], [13, 32, 18, 26], [32, 45, 37, 39], [1025, 1001, 857, 1]])

Without writing any code, what array would you return for the values above?

[6], [32], [45], [1025]

Yes. Good. Now, when you loop over the array of arrays, what could you do to each array to make it easy to get at the largest value in that array?

To check their length and their characters? or to store them inside a variable?

Let’s roll back.

Is there an array method you could apply to [4, 6, 1, 3] to help you know that 6 is the largest number in this array?

the if (x > y) method could be the one?

There are several ways to approach this.

Just remember to log your variables and expressions as you code, so you can see if they are what you expect.

Try testing with this: console.log(largestOfAll([[4, 6, 1, 3], [13, 32, 18, 26], [32, 45, 37, 39], [1025, 1001, 857, 1]]))

Here’s a good resource. Take a look at instance methods in the lefthand side panel.

Array - JavaScript | MDN

You might also consider using a method in the built-in Math object.

Good luck and happy coding.

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