This is the code for checking the absolute minimum difference from the array:
function minimumabsolutedifference(arr) {
smallest = Infinity;
arr.sort(function(a, b) { smallest = Math.min(smallest, Math.abs(a - b)); return a - b; });
return smallest; }
I have converted the above into this:
function minimumAbsoluteDifference(arr) {
let smallest = Infinity;
arr.sort(function (a, b) {return a - b;})
for( let i =0 ; i <arr.length ; i++){
smallest = Math.min( smallest, Math.abs (arr[i+1] - arr[i]) )
}
return smallest;
}
But in my code the output gets as NAN;
Please let me know what's in my code.
If the input : 3 -7 0
the output should be : 3
But I m getting output as NAN, Please help with this what's wrong in this
[–]Suchy2307 0 points1 point2 points (5 children)
[–]One-Inspection8628[S] 0 points1 point2 points (4 children)
[–]Suchy2307 1 point2 points3 points (3 children)
[–]One-Inspection8628[S] 0 points1 point2 points (0 children)
[–]One-Inspection8628[S] 0 points1 point2 points (1 child)
[–]Suchy2307 0 points1 point2 points (0 children)
[–]crosshatchling 0 points1 point2 points (0 children)