I need to create a function which finds the two numbers with the largest difference between them and returns their indices as an array of two elements - [ lowest, biggest ]
.
Notice: the bigger number must be to the right from lowest. If we have no difference between the numbers, the function must return []
.
function getProfit(arr) {
let result = [];
let sortedArr = arr.sort();
let min = sortedArr.indexOf(Math.min(...sortedArr));
let max = sortedArr.indexOf(Math.max(...sortedArr));
if(max > min) {
result.push(min);
result.push(max);
return result;
} else {
return [];
}
}
console.log(getProfit([13, 6, 3, 4, 10, 2, 3], [2, 4]));