JavaScript How can I find the sum of each individual element in an array of long numbers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

You can use `map` and `reduce` array methods:

``````['123-434-4455', '123-495-8475', '484-728-3456'].map(function(str) {
return str.split('').reduce(function(a,b) {
return a + (+b || 0);
}, 0);
}); // [ 35, 48, 51 ]
``````

Some notes:

• `split('')` splits a string into an array of characters
• `+b` coerces `b` into a number, e.g. `"5"` to `5` and `"-"` to `NaN`.
• `+b || 0` will return `0` if `+b` is falsy (`0` or `NaN`), and `+b` otherwise

This code will do it:

``````var phoneNumbers = ["123-434-4455", "123-495-8475", "484-728-3456"];

for (var i=0; i<phoneNumbers.length; i++) {//For every phone number
var total=0; //create a total variable
var indNumbers = phoneNumbers[i].replace(/-/g, ""); //remove all dashes
for (var j=0; j<indNumbers.length; j++) {//loop for all digits
}
console.log(total);//do stuff with it here
}
``````

All pretty standard javascript stuff. Only confusing bit I might have used is the `.replace` method - my parameter is `/-/g` which might look scary, but it's the equivalent of passing in `"-"` except it tells the function to replace ALL instances of the dash. If you do pass `"-"`, it only replaces the first instance.

If you're a beginner, the things in this code you'll want to learn about are `.replace`, `parseFloat` `for` loops, and accessing strings using square bracket notation.

``````var phoneNumbers = ["123-434-4455", "123-495-8475", "484-728-3456"];

function crossSum(arr, pos) {
var sum = 0;
var a = arr[pos].split("-").join("").split("");
a.forEach(function(e, i) {
sum += parseInt(a[i]);
})
return sum;
}

document.write(crossSum(phoneNumbers, 0));``````

Handling 404, 500 and Exceptions in Node.js and Express

I have a node.js + Express + express-handlebars app. I want to redirect the users to 404 page when they go to a page that does not exists and redirect them to a 500 when there is an internal server ...

I have a node.js + Express + express-handlebars app. I want to redirect the users to 404 page when they go to a page that does not exists and redirect them to a 500 when there is an internal server ...

1. handling exceptions node.js
2. handle all exceptions node
3. handling uncaught exceptions in node js

Trouble with innerHTML using Javascript

I am making a form with some fields that a user has to fill out. Once he/she fills it out, hits save, the form disappears and a new div appears with what the user filled out. Unfortunately, I cannot ...

I am making a form with some fields that a user has to fill out. Once he/she fills it out, hits save, the form disappears and a new div appears with what the user filled out. Unfortunately, I cannot ...

How to return Success data after update in firebase

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

1. return success data ajax jquery
2. return success data
3. return success data ajax
4. javascript return success data
5. ajax success return data json
6. jquery success return data
7. datatable ajax success return data
8. ajax post success return data

Benefits of dynamically creating jQueryUI Dialogs

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

1