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:
- create a inner function, to return all the biggest number inside the sub-array. for example: [13,27,18,26] will give you 27.
- 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?