What is the best way to find the index of the largest element in an array of floating point numbers?
[0.000004619778924223204, 0.8323721355744392, 0.9573732678543363, 1.2476616422122455e-14, 2.846605856163335e-8]
Once the index of the largest element is determined, the goal is to retrieve the corresponding value from another array.
For illustration purposes, let's refer to the second array as:
['a', 'b', 'c', 'd', 'e']
After running some code, the result was 'b'
instead of 'c'
.
An attempt was made to map the floats to their corresponding strings in the second array. Then the string at the index of the smallest float in the sorted array was retrieved.
// aInput contains the array of floats
var emObj = {};
for(var i=0; i<aInput.length; i++){
emObj[aInput[i]] = ['a', 'b', 'c', 'd', 'e'][i];
}
return emObj[aInput.sort()[0]];
Another approach involved iterating through the array of floats to identify the largest value, and then using that value to retrieve the corresponding string.
return ['a', 'b', 'c', 'd', 'e'][aInput.indexOf(largestFloat)];
Unfortunately, neither of these methods produced the desired outcome, consistently returning an incorrect string.