最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sum of a string of one-digit numbers in javascript? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to write a script that adds the left side of a string and validates it against the right side.

For example:

var left = "12345"
var right = "34567"

I need to do some sort of sum function that adds 1+2+3+4+5 and checks if it equals 3+4+5+6+7.

I just don't have a clue how to do it.

I think I need to use a for loop to iterate through the numbers such as for (var i = 0, length = left.length; i < length; i++)

But I'm not sure how to add each number from there.

EDIT the var is actually being pulled in from a field. so var left = document.blah.blah

I'm trying to write a script that adds the left side of a string and validates it against the right side.

For example:

var left = "12345"
var right = "34567"

I need to do some sort of sum function that adds 1+2+3+4+5 and checks if it equals 3+4+5+6+7.

I just don't have a clue how to do it.

I think I need to use a for loop to iterate through the numbers such as for (var i = 0, length = left.length; i < length; i++)

But I'm not sure how to add each number from there.

EDIT the var is actually being pulled in from a field. so var left = document.blah.blah

Share Improve this question edited Apr 7, 2012 at 6:48 ninjagecko 91.1k24 gold badges142 silver badges152 bronze badges asked Apr 7, 2012 at 6:13 SorryEhSorryEh 9282 gold badges19 silver badges48 bronze badges 5
  • You need a delimiter, and split the string up in an array, then run through the arrays and add up all values into a temporary number, then pare the two. You need a delimiter because not all numbers will always be 1 digit. – Faris M Commented Apr 7, 2012 at 6:15
  • You don't need a delimiter. left.split("") => [ '1', '2', '3', '4', '5' ] – The Who Commented Apr 7, 2012 at 6:16
  • @Faris a delimeter is not needed. left.split('') – nathanjosiah Commented Apr 7, 2012 at 6:16
  • You don't need a delimiter of the values are only 1 digit. He did not specify if that is the case, in which you should play it safe and assume they could be > 9. – Faris M Commented Apr 7, 2012 at 6:19
  • @nathanjosiah i've tried going online and looking up the right way to do this and found only information on iteration where the var increases incrementally as long as a condition exists - nothing about adding the parts together.I haven't "tried" anything because there's nothing to try since I dont even know where to start. – SorryEh Commented Apr 7, 2012 at 6:19
Add a ment  | 

3 Answers 3

Reset to default 8

DEMO

var left = "12345"
var right = "12345"

function add(string) {
    string = string.split('');                 //split into individual characters
    var sum = 0;                               //have a storage ready
    for (var i = 0; i < string.length; i++) {  //iterate through
        sum += parseInt(string[i],10);         //convert from string to int
    }
    return sum;                                //return when done
}

alert(add(left) === add(right));​
  1. Find the length of the string
  2. then in a temp Variable store the value pow(10,length-1)
  3. if you apply module function (left%temp) you will ge the Last significant digit
  4. you can use this digit to add
  5. repeat the process till the length of the string left is 0 6 Repeat all the steps above for the right as well and then pare the values

Note: convert the string to int using parseInt function

var sum = function(a,b){return a+b}

function stringSum(s) {
    var int = function(x){return parseInt(x,10)}
    return s.split('').map(int).reduce(sum);
}

stringSum(a) == stringSum(b)
发布评论

评论列表(0)

  1. 暂无评论