 # Basic Algorithm Scripting: Return Largest Numbers in Arrays - Passed but Magical

Basic Algorithm Scripting: Return Largest Numbers in Arrays - Passed but Magical
0

#1

Is this answer acceptable in the real world?
How can I overcome this habit of hardcoding? (staticIndex)

This answer works only for 4 sub arrays

function largestOfFour(arr) {
// You can do this!
var staticIndex = [0, 1, 2, 3];
var ans = [];
for (var i = 0; i < arr.length; i++) {
var max = arr[staticIndex[i]];
for (var j = 1; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
ans.push(max);
}
return ans;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

I thought of using higher order functions but I didn’t know how

#2

The line:

`````` var max = arr[staticIndex[i]];
``````

would be the same as:

`````` var max = arr[i];
``````

so you would not even need the staticIndex array.

#3

Nope.

Before you start writing your solution ask yourself:
“What information do I have?”
“What information do I need?”
“How can I get the information that I need from the information that I have?”

Do you need to know the number of sub-arrays? If so, how can you get the number of sub-arrays if you are given the whole array?