Return Largest Numbers in Arrays (syntax error?)

In this code challenge we need to return the biggest number in sub arrays, then combine them into an array.

for example:

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

So, here’s what I am planning to do:

  1. create a inner function, to return all the biggest number inside the sub-array. for example: [13,27,18,26] will give you 27.
  2. Create an outer function, to combine all those 4 numbers into one array and output it.

And here’s what the inner function looks like:

function extract(arr){
  var base = 0;

  for(let i = 0; i< arr.length; i++){
    while(arr[i]>base){
      base = arr[i];
    }
  }

  return base;
}

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

It works.

and here’s the outer function (unfinished) :

function a(arr){
  for(var j=0;j<arr.length;j++){
    function extract(arr[j]){ //this is where the error comes from
      var base = 0;
      for(let i = 0;i<arr[j].length;i++){
        while(arr[j][i]>base){
          base = arr[j][i];
        }
      }
      console.log(base);
    }
  }
}

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

And it gives me an error there, at the 3rd line.
How do I change its syntax so that it wont give me an error?

were you trying to “return” the function?
Right now you are just defining it without assigning to anything (no variable, no return)

I know that it returns undefined.
I will change that part later.

But the error I get says at 3rd line I cannot use arr[j] .
Then how am I gonna use code to represent the inner arrays?

arr[j] is not a valid parameter name

I’m not clear if you are trying to return a function definition or define and use an anonymous function.

If you want to pass arr[j] to your anonymous function, then assign it to some variable (after you change the param to valid variable name) and then pass arr[j] to that new variable function.

Not sure if I’m helping, but I hope I’m giving you an idea…

edit , here’s an example using your function:

let myfunc = function extract(arr){
var base = 0;

for(let i = 0; i< arr.length; i++){
while(arr[i]>base){
base = arr[i];
}
}

return base;
};

myfunc(arr[j]);

1 Like

I see… so one of the problem I have here is that I declared a function but did not invoke it.
I will try to figure out the rest myself…

Thanks!