This one would be done using calculating the product of all numbers in an array and then dividing the current element value. Since it could also be done using log and exp without using any divide operation.

If you know other way of solving this problem of O(n). Please discuss in comments section.

var product = input => {

let sum = input.reduce((acc, cval) => {

return acc + Math.log(cval);

}, 0);

let result = input.map(x => {

return Math.round(Math.exp(sum - Math.log(x)));

});

console.log(result);

};

product([1, 2, 3, 4, 5]);

If you know other way of solving this problem of O(n). Please discuss in comments section.