Product of Array Items
Given an array
Sample Input 1
[1, 2, 3]
Sample Output 1
1 * 2 * 3 = 6
Sample Input 2
[-1, -2, -3]
Sample Output 2
-1 * -2 * -3 = -6
<script>
function product(ar,n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = result * ar[i];
return result;
}
// Sample 1
let ar = [ 1, 2, 3];
let n = ar.length;
document.write(parseInt(product(ar, n)));
// Sample 2
let ar = [ -1, -2, -3];
let n = ar.length;
document.write(parseInt(product(ar, n)));
</script>
Comments
Leave a comment