3

Is it possible to split a string (possibly with regex) so that every other number is a pair(starting from the right hand end)?

//             1                [1]
//            12               [12]
//           123             [1,23]
//          1234           [1,2,34]
//         12345          [12,3,45]
//        123456        [1,23,4,56]
//       1234567      [1,2,34,5,67]
//      12345678     [12,3,45,6,78]
//     123456789   [1,23,4,56,7,89]
//    1234567890 [1,2,34,5,67,8,90]

I tried reversing the string & then adding blocks of alternating two and one characters till the end (front) of the string. Then reversing it again. That mainly worked but it was flawed (didn't work for all cases). I also tried the regex

(\d\d)(\d)(\d\d)(\d)(\d\d)(\d)

But that doesn't work either (only in the regex tester ironically) - it's too long but I'll need it to work for 10 digit numbers max.

5
  • What is the language? Commented Feb 3, 2016 at 20:58
  • Yes. It's possible. Now please read How to Ask. Commented Feb 3, 2016 at 21:00
  • In this case: // 1234 [1,2,34] maybe this // 1234 [12,34] ? Commented Feb 3, 2016 at 21:01
  • I'm breaking the digits down to words. 1 thousand, 2 hundred, thirty four. Commented Feb 3, 2016 at 21:03
  • Interesting question, what have you tried? Commented Feb 3, 2016 at 21:09

2 Answers 2

3

No so hard:

Start from the right, I will take one time 2 digits, the second time 1 digits. Using slice. Then I will use unshift to push it in the begining of the array.

I'm using a flag, to know when to take only 1 parameter, and when to take two parameters (pair flag)

m(1)
m(12)
m(123)
m(1234)
m(12345)
m(123456)
m(12345678)
m(123456789)
m(1234567890)
function m(x){
    x=x.toString()
    var a=[]
    var v;
    var y=2
    while(x){
        v=x.slice(-y)
        x=x.slice(0,-y)        
        y=y==1? 2:1
        a.unshift(v)
    }
    console.log(a)
}

Result:

["1"]
["12"]
["1", "23"]
["1", "2", "34"]
["12", "3", "45"]
["1", "23", "4", "56"]
["12", "3", "45", "6", "78"]
["1", "23", "4", "56", "7", "89"]
["1", "2", "34", "5", "67", "8", "90"]
Sign up to request clarification or add additional context in comments.

1 Comment

#Amina Your solution is more elegant and quicker - but I was determined to get there!
1

I got there in the end.

//             1                [1]
//            12               [12]
//           123             [1,23]
//          1234           [1,2,34]
//         12345          [12,3,45]
//        123456        [1,23,4,56]
//       1234567      [1,2,34,5,67]
//      12345678     [12,3,45,6,78]
//     123456789   [1,23,4,56,7,89]
//    1234567890 [1,2,34,5,67,8,90]

var results = "";

pairUp(1)
pairUp(12)
pairUp(123)
pairUp(1234)
pairUp(12345)
pairUp(123456)
pairUp(12345678)
pairUp(123456789)
pairUp(1234567890)

alert(results);

function pairUp(num)
{
  var s = num.toString();
  s = s.split("").reverse().join("");
  var a = s.split("");

  var r = []; // our results array
  count = 0;

  for (var i = 0; i <= a.length -1; i++)
  {
    temp = a[count];

    if ((i % 2) == 0) // even (0, 2, 4)
    {
      var p = a[count+1];
      var q = a[count];

      if (p == undefined) p = "";
      if (q) r.push(p + q+ "");

      count+=2;
    }
    else 
    {
      if (temp != undefined) r.push(temp + "");
      count+=1;
    }
  } // end loop


  r = r.reverse();

  results+= r + "\n";

} // end pair up

Comments

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.