0

I'm trying to solve a simple warm up problem in an online coding environment.

The problem is to find the cumulative sum of an array which will be entered through stdin. Here's the provided code.

process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
 input_stdin += data;
});
process.stdin.on('end', function () {
 input_stdin_array = input_stdin.split("\n");
 main(); 
});
function readLine() {
 return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
 var n = parseInt(readLine());
 arr = readLine().split(' ');
 arr = arr.map(Number);
}

Here's the code I've added.

var count = 0;
 for(i= 0; i<n; i++){
 arr[i]+= count;
 }
 return count;

It's not providing any output on the stdout.

My question is two-pronged.

One, what am I doing wrong here?

Two, can someone help me understand the provided code.

Particularly this line of code doesn't make sense, `arr = arr.map(Number);

`

asked Jun 13, 2016 at 18:37
5
  • What? You've written the code and asking us to explain what you've written?.. Commented Jun 13, 2016 at 18:38
  • 2
    What is going 'wrong'? Commented Jun 13, 2016 at 18:40
  • @nicael maybe you should go back and read the question twice before downvoting. There's a block of provided code which I cant seem to understand. I know what I've written. Commented Jun 13, 2016 at 18:46
  • All I can glean from the provided code is that they are intializing their own input field to work. The reason the code above the ignore line is to be ignored, is probably because it uses their own functions. Commented Jun 13, 2016 at 18:46
  • @user2864740 it's not providing any output on the stdout. Which is why I'm trying to understand the provided code. Commented Jun 13, 2016 at 18:47

3 Answers 3

1

I see what you've done wrong here.

arr[i]+= count;

With this, you're adding count to arr[i].

What you're trying to do, which seems to be counting the sum of the array, will look like this:

count += arr[i];

It adds arr[i] to count.

If I answered the wrong thing, I apologise.

answered Jun 13, 2016 at 18:44
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, it seems, like the answer above stated, your challenger wanted the total console.log()ed.
0

I guess it's a Hackerrank challenge? I think what's wrong is that you're incrementing the value of your array element/item rather than the count. It's great that you're learning to code, I'd say keep it up

var count = 0;
var n = arr.length
for(i= 0; i<n; i++){
 count += arr[i];
}
return count;

You should also look at the reduce operation, for cleaner code. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

answered Jun 13, 2016 at 18:45

3 Comments

did you add this portion to the main function? and i think the you ought to console.log(count) not return.
Thanks it finally worked on doing console.log. Can you please help me understand what does this line mean arr = arr.map(Number);
a map operation on an iterable object means a new array will be created with the results of calling a provided function on every element in the array array. Number is a js method/class. So basically what it was doing in your case is it's re-assigning the result of calling Number on each element in the array to the arr variable. The map and reduce functions help your reduce to boilerplate code in your application. Calling the map is the same as doing: for(var i = 0; i < arr.length; i++){ arr[i] = Number(arr[i])}. The map is shorter and concise.
0

To pass HR's stdout requirement you need either

console.log(count);

or

process.stdout.write(count);
mabdrabo
1,07021 silver badges35 bronze badges
answered Dec 20, 2016 at 22:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.