5

I will have a string never long than 8 characters in length, e.g.:

// represented as array to demonstrate multiple examples
var strs = [
    '11111111',
    '1RBN4',
    '12B5'
]    

When ran through a function, I would like all digit characters to be summed to return a final string:

var strsAfterFunction = [
    '8',
    '1RBN4',
    '3B5'
]

Where you can see all of the 8 single 1 characters in the first string end up as a single 8 character string, the second string remains unchanged as at no point are there adjacent digit characters and the third string changes as the 1 and 2 characters become a 3 and the rest of the string is unchanged.

I believe the best way to do this, in pseudo-code, would be:

1. split the array by regex to find multiple digit characters that are adjacent
2. if an item in the split array contains digits, add them together
3. join the split array items

What would be the .split regex to split by multiple adajcent digit characters, e.g.:

var str = '12RB1N1'
  => ['12', 'R', 'B', '1', 'N', '1']

EDIT:

question: What about the string "999" should the result be "27", or "9"

If it was clear, always SUM the digits, 999 => 27, 234 => 9

4
  • 5
    What about the string "999" should the result be "27", or "9" Commented Jun 11, 2013 at 19:15
  • do I have to use a regexp? Commented Jun 11, 2013 at 19:18
  • @MarshallAnschutz have answered your question - always SUM the digits Commented Jun 11, 2013 at 19:20
  • @Shanimal does not necessarily have to use regexp - I thought this would be best. Please give suitable alternative if you have one! Commented Jun 11, 2013 at 19:21

3 Answers 3

12

You can do this for the whole transformation :

var results = strs.map(function(s){
    return s.replace(/\d+/g, function(n){
       return n.split('').reduce(function(s,i){ return +i+s }, 0)
    })
});

For your strs array, it returns ["8", "1RBN4", "3B5"].

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

1 Comment

perfection!!. This wasn't my exact issue, but this solution basically saved lives...
5
var results = string.match(/(\d+|\D+)/g);

Testing:

"aoueoe34243euouoe34432euooue34243".match(/(\d+|\D+)/g)

Returns

["aoueoe", "34243", "euouoe", "34432", "euooue", "34243"]

1 Comment

..but @dystroy has taken the whole fun out of it and provided the whole algorithm for you :)
1

George... My answer was originally similar to dystroy's, but when I got home tonight and found your comment I couldn't pass up a challenge

:)

Here it is without regexp. fwiw it might be faster, it would be an interesting benchmark since the iterations are native.

function p(s){
  var str = "", num = 0;
  s.split("").forEach(function(v){
    if(!isNaN(v)){
        (num = (num||0) + +v);
    } else if(num!==undefined){
        (str += num + v,num = undefined);
    } else {
        str += v;
    }
  });
  return str+(num||"");
};

// TESTING
console.log(p("345abc567"));
// 12abc18
console.log(p("35abc2134mb1234mnbmn-135"));
// 8abc10mb10mnbmn-9
console.log(p("1 d0n't kn0w wh@t 3153 t0 thr0w @t th15 th1n6"));
// 1d0n't0kn0w0wh@t12t0thr0w0@t0th6th1n6

// EXTRY CREDIT
function fn(s){
    var a = p(s);
    return a === s ? a : fn(a);
}

console.log(fn("9599999gh999999999999999h999999999999345"));
// 5gh9h3

and here is the Fiddle & a new Fiddle without overly clever ternary

4 Comments

Why would you not just use if () { } else { }? That is not the appropriate way to use a tertiary operator. You might think it looks slick, but only a few programmers will get it at first glance.
@KthProg I think pretty much anyone who has seen a ternary before can read it just fine. Sure, it's not pretty to read but I wouldn't call it a huge problem.
@Nit I'd just hate to be the guy maintaining that code.
@KthProg... Ya, that's what the folks that wrote the book on code maintainability call "overly clever". I'll edit the post and clean it up.

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.