-1

I was wondering if it were possible with jQuery to add together numbers in a string...

The strings can only be added to themselves as shown in the examples below. If one or more of the string appears, it should be combined.

All the possible strings: click here

Example with possible strings:

+0.53 attack damage
+0.53 attack damage
= 1.06 attack damage
+8.75 mana
+8.75 mana
= 17.5 mana

Each of these can be combined up to 9 times.

asked Aug 18, 2014 at 12:01
8
  • will the structure be always same? Commented Aug 18, 2014 at 12:02
  • look at stackoverflow.com/questions/3955345/… Commented Aug 18, 2014 at 12:03
  • @AnoopJoshi - no the string would be different almost every time Commented Aug 18, 2014 at 12:04
  • what is source of strings, are they being concatenated with numbers at server? Commented Aug 18, 2014 at 12:05
  • @terribleProgrammer - this is an example of always having the same thing before the number? I'm talking about random strings from a set. Commented Aug 18, 2014 at 12:05

3 Answers 3

1

Iterate through the characters of the string (by making substrings with length 1); once you detect a number or a decimal point, save the start position in a variable. Then, once you detect something that is not a number or decimal point, or a second decimal point (count the number of decimal points), the number has ended; save the end position and voilà, you have the start and end position of the number and can get it by making a substring.

answered Aug 18, 2014 at 12:05
Sign up to request clarification or add additional context in comments.

1 Comment

this should be posted as a comment not an answer.
0

With this regex:

/((-|\+)?([0-9]+|)(\.?[0-9]+))/g

You can use Array.prototype.reduce like this to sum your numbers up

var str = 'Hello +0.50 World Hello 1 World Hello -10 World',
 re = /((-|\+)?([0-9]+|)(\.?[0-9]+))/g,
 sum;
sum = (str.match(re) || []).reduce(function (prev, current) {
 if (Object.prototype.toString.call(prev) !== '[object Number]') {
 prev = parseFloat(prev, 10);
 }
 return prev + parseFloat(current, 10);
}, 0);
// sum should be equal to -8.5 here

Note that str.match(re) may return null, so we just make sure we call reduce on an array.

answered Aug 18, 2014 at 12:18

1 Comment

Would it be possible to ignore things within brackets? Example: +0.3 health per level (+5.4 at champion level 18) ignore (+5.4 at champion level 18)
0
var strs = [
 "Hello 1 World",
 "Hello 2 World"
];
var n = 0;
strs.forEach(function(str) {
 str.match(/[\d|\.|\-]+/g).forEach(function(num) {
 n+= window.parseFloat(num);
 });
});

Edit: Oh, that "list of possible strings" (with percentage values etc) changed the game. Well, start with this and improve the implementation :|

answered Aug 18, 2014 at 12:07

1 Comment

i've added a list of all the possible strings, this might help you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.