JavaScript Sum the contents of a JavaScript array compare contents array javascript,javascript array contents equal,javascript array

Beginning JavaScript learner here...

I'm working through Adrian Neumann's Simple Programming Problems and my question is about number 5 in the elementary exercises.

Write a program that asks the user for a number n and prints the sum of the numbers 1 to n... such that only multiples of 3 and 5 are considered.

Here is my code as it currently stands...

``````var myArray = [];
var mySum = 0;

var userNum = prompt("What is your number? "); {
for (var i = userNum; i > 0; i--) {
if (i % 5 === 0 || i % 3 === 0) {
mySum += myArray.push(i);
}
}
}
console.log(mySum);
``````

This produces the result `28` which is not correct. When I comment out the `mySum` statement and print the array for the input of `17` I get `[15, 12, 10, 9, 6, 3]` which looks correct.

I would be interested in tips on why the `mySum` statement doesn't provide the expected result. I would also be interested in any tips to make the code more efficient. Many thanks!

Edit
For anyone interested, here's the code I settled on for being best for my purpose and current level:

``````var mySum = 0;

var userNum = prompt("What is your number? ");
for (var i = userNum; i > 0; i--) {
if (i % 5 === 0 || i % 3 === 0) {
mySum += +i; // type coercion with unary operator
}
}
console.log(mySum);
``````

Thanks to everyone!

Since myArray.push(i) does not return the number you pushed but the current length of the array, your sum is not what you expect.

Instead use `mySum += i;`

or if you want to still use the array to later process/console.log the separate numbers:

`````` mySum += i;
myArray.push(i);
``````

or just `myArray.push(i);` and afterwards reduce the array:

``````sum = myArray.reduce(function(a, b) {
return a + b;
});
``````

Here I also cast the string you get when you prompt to a number and remove the wrapping `{ }` which are not necessary

``````var myArray = [];
var mySum = 0;

var userNum = prompt("What is your number? ");
for (var i = +userNum; i > 0; i--) {
if (i % 5 === 0 || i % 3 === 0) {
mySum += i;
myArray.push(i);
}
}
console.log(mySum,myArray);``````

Check out the docs for Array.push. It says: "Returns the new length property of the object upon which the method was called."

That means that every time you call `mySum += myArray.push(i)`, you are saying `mySum += myArray.length` (after having pushed `i`).

Instead you should just add `i` to `mySum`:

``````mySum += i
``````

``````mySum += myArray.push(i)
``````

In terms of better code, I agree with mplungjan - Array.reduce would be a good one to use here.

Edit: actually, since you're not starting with an array but a number, in this case a for loop is probably the quickest and cleanest way to get your result.

Consider the following optimized approach using "negative" `while` loop:

``````var userNum = prompt("What is your number? "),
mySum = 0;

while (userNum--) {
if (userNum && (userNum % 5 === 0 || userNum % 3 === 0)) {
mySum += userNum;
}
}
console.log(mySum);  // 60
``````

Summernote wraps <p><br></p> for insertNode()

I am trying to insert a simple img tag into the editor. jQuery('<img />').attr('src', '/uploads/images/' + res.data.filename); jQuery('.summernote').summernote('insertNode', \$image[0]); I ...

I am trying to insert a simple img tag into the editor. jQuery('<img />').attr('src', '/uploads/images/' + res.data.filename); jQuery('.summernote').summernote('insertNode', \$image[0]); I ...

EmberJS: toggleProperty set all properties of a Component to a default value

I'm trying to implement nested components in Ember 2.0.1, but I'm getting a strange behavior when use toggleProperty function inside of the action handler. The first component looks like: // ./...

I'm trying to implement nested components in Ember 2.0.1, but I'm getting a strange behavior when use toggleProperty function inside of the action handler. The first component looks like: // ./...

Preventing Marionette CompositeView render until fetch complete

I'm having a problem where render is being called autimatically in my Marionette CompositeView which is correct, the problem is that I'm fetching collection data in the initialize and want this to be ...

I'm having a problem where render is being called autimatically in my Marionette CompositeView which is correct, the problem is that I'm fetching collection data in the initialize and want this to be ...

CSS Scaled Div Centring

I am having an issue centring a <div id='divTwo'> inside another <div id='divOne'>. This is normal an easy thing to do, however in this instance i have transform: scale(); with transform-...

I am having an issue centring a <div id='divTwo'> inside another <div id='divOne'>. This is normal an easy thing to do, however in this instance i have transform: scale(); with transform-...

1