Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Well, this works and pass the challenge…but it’s not universal in all scenarios imo.
Is there a way to reference the first value of each sub-array instead of comparing it to negative inf? xD

Your code so far

function largestOfFour(arr) {
let subArr = [-Infinity, -Infinity, -Infinity, -Infinity];
for (let i = 0; i < arr.length; i++)
for (let j = 0; j < arr[i].length; j++) {
if (subArr[i] < arr[i][j])
  subArr[i] = arr[i][j];
}
return subArr ;
}


console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

I wouldn’t use -Infinity.
I would just assume initially the first number I see is the largest. Then loop till I find a bigger one to replace it (storing in a variable as I go the largest one so far)

you mean like reference the first value of every subArray index and then loop to replace them with higher value when found?
how do you do that?

I prefer that you tried to do it first?
Or maybe ask me a more specific question?

Edit:

If you are asking how to get the first number of the array, then sorry for misunderstanding you. (I though you meant how to write the whole program)

Use bracket notation with 0 as the index to get the first value.
For eg:

arr[0][0]

[0][0]That would only give 1st element of the 1st subArray.
If I want 1st element of 1st, 2nd, 3rd, 4th subArray…

Ah…be right back.

I’m trying to put arr[i][0] as the reference but don’t know where to put.
It keeps giving me i is undefined.
So so far mine it’s like this:

function largestOfFour(arr) {
let refArr1 = arr[0];
let refArr2 = [];
for (let i = 0; i < arr.length; i++)
for (let j = 0; j < arr.length; j++) { 
  if (arr[i][j] > refArr1[i]) {
    refArr1[i] = arr[i][j];
  } 
}
return refArr1;
}


console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));

How about after the first for loop? (As I is defined there? And because it represents the loop of arrays?)

ps. Your variable names are not great.
If the first variable is supposed to be the largest num, then just call it that largestNum or just l even.
If the second is meant to be the return array then just call it that too retArr or just r.

I got it!

function largestOfFour(arr) {
let refArr1 = arr[0]; //[17, 23, 25, 12], [...]
for (let i = 0; i < arr.length; i++) {
 let firstNum = arr[i][0];
for (let j = 0; j < arr.length; j++) { 
  if (arr[i][j] > firstNum) {
    firstNum = arr[i][j];
  } 
}
refArr1[i] = firstNum
}
return refArr1;
}


console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));

Took forever!

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