Answer to Question #312549 in HTML/JavaScript Web Application for CHINNI

Question #312549

Cumulative Sum

given an array integers, write a JS program to get the cumulative sum of the items in the array


sample input1

[1, 10, 100, 1000]

sample output1

[1, 11, 111, 1111]


"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 */

}



1
Expert's answer
2022-03-16T10:24:46-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());




 const cumulativeSum = arr => arr.map((sum => value => sum += value)(0))


 console.log(cumulativeSum(integers)); // [1, 10, 100, 1000]


}

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
APPROVED BY CLIENTS