1

I need to count numbers in a string. The numbers are separated by spaces. (1 2 3 4 10) I was trying to do this by charAt by that doesnt work if the number is not a single digit. I am relative new to JS and need some examples. I started with this but ran into a double digit #:

string1 = (1 1 1 4 10)
var total = parseFloat(0);
 for(var j=0; j<string1.length; j++) {
    total += parseFloat(string1.charAt(j)); 
}

Any help would be appreciated

9
  • 1
    string1 = (1 1 1 4 10) <--- that is not valid. Open up the developer console, you will see an error that points it out. "Uncaught SyntaxError: Unexpected number" Commented Sep 22, 2016 at 18:47
  • Wrap your string in quotes, then simply do string1.match(/\d+/g).length. Commented Sep 22, 2016 at 18:53
  • @Xufox: looks like he tries to sum the nums Commented Sep 22, 2016 at 18:56
  • Here's a solution that tallies up the numbers as you go. Commented Sep 22, 2016 at 19:03
  • @Jonasw In that case string1.match(/\d+/g).map(Number).reduce((a, b) => (a + b)). Commented Sep 22, 2016 at 19:38

4 Answers 4

1

I don't know why you need to do this way, but if this string comes from another font, you can deal with it, something like this:

var string1 = "(1 1 1 4 10)";
var aux = string1.replace("(","").replace(")","");
aux = aux.split(" ");
var total = parseFloat(0);
 for(var j=0; j<aux.length; j++) {
    total += parseFloat(aux[j]); 
}
console.log(total);

https://jsfiddle.net/bggLkvxd/1/

Sign up to request clarification or add additional context in comments.

Comments

0

Create an array:

var arr=[1,2,3]

and then do:

var count=0;
arr.forEach(function(number){
count+=number;
}

Or use a string:

var str="1 2 3";
var arr=str.split(" ");

var count=0;
arr.forEach(function(number){
count+=parseInt(number);
}

Count now contains the sum of all chars

Comments

0

// use method split to convert string to array and then add array items:

var string1 = "1 1 1 4 10", total = 0;
string1.split(" ").forEach(function(item) {
    total += +item;
});   

1 Comment

+"5" equal 5, '+' before string converts it to number, so we have 0 + 1.
0

Here's a simpler way. First, fix string1 so it's actually a string (by adding " "s), also properly declare it with "var string". Then you could do something like this.

var string1 = ("1 1 1 4 10")

function addString(input) {
var sum = 0;                        //get your empty variable ready
var array1 = input.split(" ");    //split your string into an array
 for(var i=0; i<array1.length; i++) { 
array1[i] = parseInt(array1[i]);    // each new array element is integer
sum += array1[i];                   // += is operator for solve/refactor
}
return sum;
}

addString(string1);

1 Comment

Why does your function contain a parameter input that is never used? Theres no need to wrap variables into brackets (return sum) and comments are marked with // ...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.