Answer to Question #318695 in HTML/JavaScript Web Application for siva

Question #318695

Product of array Items

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

input

the input will be containing single line integers.

output

the output should be single line containing the product as shown in sample outputs.


input1

[1,2,3]

output1

[1 * 2 * 3 = 6]

input2

[-1, -2, -3]

output2

[-1*-2*-3 = -6]


Note: The Output should be displayed as [1 * 2 * 3 = 6]


"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());


 /* Please do not modify anything above this line */


 /* Write your code here and log the output */

}



1
Expert's answer
2022-03-26T14:01:45-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 integers = JSON.parse(readLine());


 /* Please do not modify anything above this line */


 /* Write your code here and log the output */
 let buff = 1
 let res = `[`
 for (let i=0; i<integers.length; i++) {
   buff *= integers[i];
   if (i>0) {
     res += `*${integers[i]}`}
   else {
     res += `${integers[i]}`}
 }
 res += ` = ${buff}]`;
 console.log(res)
}

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