I have developed an algorithm designed to identify the highest value within each subarray and then push that value onto a separate array called 'final'.
As part of this process, I aim to assign the variable 'value' the lowest possible number so that negative numbers can be considered greater than 'value'.
function largestOfFour(arr) {
var final=[];
arr.map(sub => {
let value = 0; // Starting point
sub.map(num => {
if(num>value){value=num};
})
final.push(value)
})
return final;
}
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));
In the given example, the last subarray produces a result of 0 since none of its numbers surpass the initial value assigned to 'value', which is set at 0.
My goal is for the output to reflect '-3', as it represents the highest number within the subarray.