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);
`
-
What? You've written the code and asking us to explain what you've written?..nicael– nicael2016年06月13日 18:38:49 +00:00Commented Jun 13, 2016 at 18:38
-
2What is going 'wrong'?user2864740– user28647402016年06月13日 18:40:54 +00:00Commented 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.user6461129– user64611292016年06月13日 18:46:32 +00:00Commented 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.MineAndCraft12– MineAndCraft122016年06月13日 18:46:57 +00:00Commented 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.user6461129– user64611292016年06月13日 18:47:15 +00:00Commented Jun 13, 2016 at 18:47
3 Answers 3
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.
1 Comment
console.log()ed.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
3 Comments
console.log(count) not return.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.To pass HR's stdout requirement you need either
console.log(count);
or
process.stdout.write(count);