product of array items
given an array integers, write a JS program to get the product of the given integers in the array
sample input1
[1,2,3]
sample output1
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 */
}
INPUT:
[1,2,3]
OUTPUT:
1*2*3 = 6
INPUT:
[-1,-2,-3]
OUTPUT:
-1*-2*-3 = -6 SHOULD BE LIKE THIS
"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());
const productArrayItems = arr => arr.reduce((a, b) => a * b)
console.log(productArrayItems(integers)); // [1, 2, 3]
console.log(productArrayItems(integers)); // [-1,-2,-3]
}
Comments
Leave a comment