Answer to Question #347359 in HTML/JavaScript Web Application for ram

Question #347359

Product of Array Items

given array of 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 integersOutput:The output should be a single line string containing the product as shown in sample outputs

Testcase1 :Sample Input 1

[1, 2, 3]

Sample Output 1

1 * 2 * 3 = 6

Testcase2 :Sample Input 2

[-1, -2, -3]

Sample Output 2

-1 * -2 * -3 = -6

note : it must satisfy any input given with different cases

"use strict";

process.stdin.resume();

process.stdin.setEncoding("utf-8");

let inputString = "";

let currentLine = 0;

process.stdin.on("data", (inputStdin) => {

 inputString += inputStdin;

});

process.stdin.on("end", (_) => {

 inputString = inputString

  .trim()

  .split("\n")

  .map((str) => str.trim());

 main(); });

function readLine() {

 return inputString[currentLine++]; }

function main() {

 let integers = JSON.parse(readLine());

/* Write your code here



1
Expert's answer
2022-06-02T10:27:04-0400
"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});


process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});

function readLine() {
return inputString[currentLine++];
}

function main() {
  let myArray = JSON.parse(readLine().replace(/'/g, '"'));
  if (myArray.length > 0) {
    
    let res = myArray[0].toString();
    let buf = myArray[0];
    for (let i = 1; i < myArray.length; i++) {
      res += " * " + myArray[i].toString();
      buf *= myArray[i];
    }
    console.log(res + " = " + buf);
  }
}

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