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
3 Answers
Reset to default 8DEMO
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));
- Find the length of the string
- then in a temp Variable store the value pow(10,length-1)
- if you apply module function (left%temp) you will ge the Last significant digit
- you can use this digit to add
- 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)