Answer to Question #205996 in HTML/JavaScript Web Application for Praveen

Question #205996

Product of Array Items

Given an array

integers, write a JS program to get the product of the integers in the given array.Input

  • The input will be a single line containing an array integers

Output

  • The output should be a single line string containing the product as shown in sample outputs

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




1
Expert's answer
2021-06-11T17:01:46-0400
<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>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS