How to Reverse a string with numbers, but don't reverse 1 and 0? [on hold]












8















I am learning random algorithms, and I am currently stock in one, where I have to reverse a string that contains numbers, but I am not to reverse 1 and 0 in the string e.g, 2345678910 would be 1098765432.



Here's what I've done so far:






function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));





I am currently having the issue of not reversing the 10.



What am I doing wrong?










share|improve this question















put on hold as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    please format you code!

    – MrSmith42
    2 days ago






  • 1





    OP wants A S U 3 2 10 if I understood correctly

    – Pac0
    2 days ago








  • 1





    I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

    – Pac0
    2 days ago








  • 2





    What about the string USA101001 ? Should it give 101001ASU ?

    – Pac0
    2 days ago








  • 3





    I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

    – user10472446
    2 days ago


















8















I am learning random algorithms, and I am currently stock in one, where I have to reverse a string that contains numbers, but I am not to reverse 1 and 0 in the string e.g, 2345678910 would be 1098765432.



Here's what I've done so far:






function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));





I am currently having the issue of not reversing the 10.



What am I doing wrong?










share|improve this question















put on hold as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    please format you code!

    – MrSmith42
    2 days ago






  • 1





    OP wants A S U 3 2 10 if I understood correctly

    – Pac0
    2 days ago








  • 1





    I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

    – Pac0
    2 days ago








  • 2





    What about the string USA101001 ? Should it give 101001ASU ?

    – Pac0
    2 days ago








  • 3





    I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

    – user10472446
    2 days ago
















8












8








8


2






I am learning random algorithms, and I am currently stock in one, where I have to reverse a string that contains numbers, but I am not to reverse 1 and 0 in the string e.g, 2345678910 would be 1098765432.



Here's what I've done so far:






function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));





I am currently having the issue of not reversing the 10.



What am I doing wrong?










share|improve this question
















I am learning random algorithms, and I am currently stock in one, where I have to reverse a string that contains numbers, but I am not to reverse 1 and 0 in the string e.g, 2345678910 would be 1098765432.



Here's what I've done so far:






function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));





I am currently having the issue of not reversing the 10.



What am I doing wrong?






function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));





function split(str) {
let temp = ;
temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);

}
return backwards.join('').toString();

}
console.log(split("10 2 3 U S A"));
console.log(split("2345678910"));






javascript algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Pac0

7,75122745




7,75122745










asked 2 days ago









user8107351user8107351

717




717




put on hold as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









put on hold as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap yesterday


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1





    please format you code!

    – MrSmith42
    2 days ago






  • 1





    OP wants A S U 3 2 10 if I understood correctly

    – Pac0
    2 days ago








  • 1





    I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

    – Pac0
    2 days ago








  • 2





    What about the string USA101001 ? Should it give 101001ASU ?

    – Pac0
    2 days ago








  • 3





    I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

    – user10472446
    2 days ago
















  • 1





    please format you code!

    – MrSmith42
    2 days ago






  • 1





    OP wants A S U 3 2 10 if I understood correctly

    – Pac0
    2 days ago








  • 1





    I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

    – Pac0
    2 days ago








  • 2





    What about the string USA101001 ? Should it give 101001ASU ?

    – Pac0
    2 days ago








  • 3





    I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

    – user10472446
    2 days ago










1




1





please format you code!

– MrSmith42
2 days ago





please format you code!

– MrSmith42
2 days ago




1




1





OP wants A S U 3 2 10 if I understood correctly

– Pac0
2 days ago







OP wants A S U 3 2 10 if I understood correctly

– Pac0
2 days ago






1




1





I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

– Pac0
2 days ago







I added the other example mentioned above the snippet into it, as already two answers (mine deleted included) missed that one.

– Pac0
2 days ago






2




2





What about the string USA101001 ? Should it give 101001ASU ?

– Pac0
2 days ago







What about the string USA101001 ? Should it give 101001ASU ?

– Pac0
2 days ago






3




3





I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

– user10472446
2 days ago







I think the reversing rule is not clear. Normally 5864 would become 4685. But what 8150 should become? Certainly not 0518, but perhaps 5180 or 5810 or 1058 or else? This is not clear to me...:(

– user10472446
2 days ago














6 Answers
6






active

oldest

votes


















12














You can replace 10 with a specified character which does not exist in the text, and after running the implemented algorithm replace it back with 10.






let out_of_alphabet_character = '#';
var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

function specific_revert(str) {
str = str.replace(/(10)/g, out_of_alphabet_character);
let temp = ;

temp = str.split('');
const backwards = ;
const totalItems = str.length - 1;
for (let i = totalItems; i >= 0; i--) {
backwards.push(temp[i]);
}
return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
}
console.log(specific_revert("10 2 3 U S A"));
console.log(specific_revert("234567891010"));








share|improve this answer





















  • 1





    That would be the simplest (in term of understandability) way of doing that IMHO

    – Pac0
    2 days ago











  • ha ! I think it fails for complex 10 strings like my example above : USA101001

    – Pac0
    2 days ago






  • 1





    @Pac0 What should be the result for USA101001?

    – OmG
    2 days ago











  • We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

    – Pac0
    2 days ago






  • 1





    This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

    – Amy
    2 days ago



















3














Just check for the special case & code the normal logic or reversing as usual






    const reverse = str => {
let rev = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
rev = '10' + rev;
i++;
} else rev = str[i] + rev;
}

return rev;
}

console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
console.log(reverse("2345678910")); // returns 1098765432








share|improve this answer


























  • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

    – mohammad javad ahmadi
    2 days ago






  • 1





    @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

    – Danyal Imran
    2 days ago











  • correct is 100000098765432

    – mohammad javad ahmadi
    2 days ago






  • 1





    @mohammadjavadahmadi No it is not... that was not a condition in the question.

    – Mr. Polywhirl
    2 days ago






  • 3





    OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

    – Danyal Imran
    2 days ago



















3














You can reduce over the matched array from using a regular expression. It's more costly than a for/loop that concatenates strings, but it was fun figuring it out.






function split(str) {
const re = /([A-Z23456789 ]+)|(10)/g
return str.match(re).reduce((acc, c) => {

// if the match is 10 prepend it to the accumulator
// otherwise reverse the match and then prepend it
acc.unshift(c === '10' ? c : [...c].reverse().join(''));
return acc;
}, ).join('');
}

console.log(split('2345678910'));
console.log(split('10 2 3 U S A'));
console.log(split('2 3 U S A10'));








share|improve this answer

































    1














    You need some pre-conditions to check each character's value.



    Due to the vagueness of the question, it is reasonable to believe that the number system that OP defines consists of [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.






    String.prototype.isNumeric = function() {
    return !isNaN(parseFloat(this)) && isFinite(this);
    };

    function reverse(str) {
    let tokens = , len = str.length;
    while (len--) {
    let char = str.charAt(len);
    if (char.isNumeric()) {
    if (len > 0 && str.charAt(len - 1).isNumeric()) {
    let curr = parseInt(char, 10),
    next = parseInt(str.charAt(len - 1), 10);
    if (curr === 0 && next === 1) {
    tokens.push(10);
    len--;
    continue;
    }
    }
    }
    tokens.push(char);
    }
    return tokens.join('');
    }

    console.log(reverse("10 2 3 U S A"));
    console.log(reverse('2345678910'));





    Output:




    A S U 3 2 10
    1098765432






    share|improve this answer


























    • for 'USA101001' it gives 11010, which looks wrong.

      – Pac0
      2 days ago











    • Based on the original example: "e.g, 2345678910 would be 1098765432."

      – Mr. Polywhirl
      2 days ago











    • my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

      – Pac0
      2 days ago











    • Ok, I fixed it.

      – Mr. Polywhirl
      2 days ago











    • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

      – mohammad javad ahmadi
      2 days ago



















    1














    Below is a recursive approach.






    function f(s, i=0){
    if (i == s.length)
    return '';
    if (['0', '1'].includes(s[i])){
    let curr = s[i];
    while (['0', '1'].includes(s[++i]))
    curr += s[i]
    return f(s, i) + curr;
    }
    return f(s, i + 1) + s[i];
    }

    console.log(f('10 2 3 U S A'));
    console.log(f('2345678910'));
    console.log(f('USA101001'));








    share|improve this answer


























    • Wouldn't USA101001 be 101010ASU ?

      – Mr. Polywhirl
      2 days ago











    • @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

      – elena a
      2 days ago











    • U S A 10 10 0 11 0 10 10 A S U101010ASU

      – Mr. Polywhirl
      2 days ago











    • @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

      – elena a
      2 days ago











    • Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

      – Mr. Polywhirl
      2 days ago





















    0














    Nice question so far.



    You may try this recursive approach(if not changing 10 for other character not allowed):






    function reverseKeepTen(str, arr = ) {
    const tenIdx = str.indexOf('10');

    if (!str.length) {
    return arr.join('');
    }

    if (tenIdx === -1) {
    return [...str.split('').reverse(), ...arr].join('');
    } else {
    const digitsBefore = str.slice(0, tenIdx);

    const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
    return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
    }
    };


    console.log(reverseKeepTen('101234105678910')) // 109876510432110
    console.log(reverseKeepTen('12341056789')) // 98765104321
    console.log(reverseKeepTen('1012345')) // 5432110
    console.log(reverseKeepTen('5678910')) // 1098765
    console.log(reverseKeepTen('10111101')) // 11011110








    share|improve this answer
































      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      You can replace 10 with a specified character which does not exist in the text, and after running the implemented algorithm replace it back with 10.






      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));








      share|improve this answer





















      • 1





        That would be the simplest (in term of understandability) way of doing that IMHO

        – Pac0
        2 days ago











      • ha ! I think it fails for complex 10 strings like my example above : USA101001

        – Pac0
        2 days ago






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        2 days ago











      • We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

        – Pac0
        2 days ago






      • 1





        This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

        – Amy
        2 days ago
















      12














      You can replace 10 with a specified character which does not exist in the text, and after running the implemented algorithm replace it back with 10.






      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));








      share|improve this answer





















      • 1





        That would be the simplest (in term of understandability) way of doing that IMHO

        – Pac0
        2 days ago











      • ha ! I think it fails for complex 10 strings like my example above : USA101001

        – Pac0
        2 days ago






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        2 days ago











      • We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

        – Pac0
        2 days ago






      • 1





        This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

        – Amy
        2 days ago














      12












      12








      12







      You can replace 10 with a specified character which does not exist in the text, and after running the implemented algorithm replace it back with 10.






      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));








      share|improve this answer















      You can replace 10 with a specified character which does not exist in the text, and after running the implemented algorithm replace it back with 10.






      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));








      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));





      let out_of_alphabet_character = '#';
      var reg_for_the_alphabet = new RegExp(out_of_alphabet_character, "g");

      function specific_revert(str) {
      str = str.replace(/(10)/g, out_of_alphabet_character);
      let temp = ;

      temp = str.split('');
      const backwards = ;
      const totalItems = str.length - 1;
      for (let i = totalItems; i >= 0; i--) {
      backwards.push(temp[i]);
      }
      return backwards.join('').toString().replace(reg_for_the_alphabet, '10');
      }
      console.log(specific_revert("10 2 3 U S A"));
      console.log(specific_revert("234567891010"));






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago

























      answered 2 days ago









      OmGOmG

      8,17352944




      8,17352944








      • 1





        That would be the simplest (in term of understandability) way of doing that IMHO

        – Pac0
        2 days ago











      • ha ! I think it fails for complex 10 strings like my example above : USA101001

        – Pac0
        2 days ago






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        2 days ago











      • We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

        – Pac0
        2 days ago






      • 1





        This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

        – Amy
        2 days ago














      • 1





        That would be the simplest (in term of understandability) way of doing that IMHO

        – Pac0
        2 days ago











      • ha ! I think it fails for complex 10 strings like my example above : USA101001

        – Pac0
        2 days ago






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        2 days ago











      • We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

        – Pac0
        2 days ago






      • 1





        This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

        – Amy
        2 days ago








      1




      1





      That would be the simplest (in term of understandability) way of doing that IMHO

      – Pac0
      2 days ago





      That would be the simplest (in term of understandability) way of doing that IMHO

      – Pac0
      2 days ago













      ha ! I think it fails for complex 10 strings like my example above : USA101001

      – Pac0
      2 days ago





      ha ! I think it fails for complex 10 strings like my example above : USA101001

      – Pac0
      2 days ago




      1




      1





      @Pac0 What should be the result for USA101001?

      – OmG
      2 days ago





      @Pac0 What should be the result for USA101001?

      – OmG
      2 days ago













      We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

      – Pac0
      2 days ago





      We don't know yet, I presume. Need OP clarification. (but it works for both the examples given, at least)

      – Pac0
      2 days ago




      1




      1





      This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

      – Amy
      2 days ago





      This would work, but extend it so it replaces the longest sequence of 1 and 0 characters, not just 10.

      – Amy
      2 days ago













      3














      Just check for the special case & code the normal logic or reversing as usual






          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432








      share|improve this answer


























      • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

        – Danyal Imran
        2 days ago











      • correct is 100000098765432

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi No it is not... that was not a condition in the question.

        – Mr. Polywhirl
        2 days ago






      • 3





        OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

        – Danyal Imran
        2 days ago
















      3














      Just check for the special case & code the normal logic or reversing as usual






          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432








      share|improve this answer


























      • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

        – Danyal Imran
        2 days ago











      • correct is 100000098765432

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi No it is not... that was not a condition in the question.

        – Mr. Polywhirl
        2 days ago






      • 3





        OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

        – Danyal Imran
        2 days ago














      3












      3








      3







      Just check for the special case & code the normal logic or reversing as usual






          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432








      share|improve this answer















      Just check for the special case & code the normal logic or reversing as usual






          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432








          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432





          const reverse = str => {
      let rev = "";
      for (let i = 0; i < str.length; i++) {
      if (str[i] === '1' && i + 1 < str.length && str[i+1] === '0') {
      rev = '10' + rev;
      i++;
      } else rev = str[i] + rev;
      }

      return rev;
      }

      console.log(reverse("10 2 3 U S A")); // returns A S U 3 2 10
      console.log(reverse("2345678910")); // returns 1098765432






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago









      Pac0

      7,75122745




      7,75122745










      answered 2 days ago









      Danyal ImranDanyal Imran

      1,015214




      1,015214













      • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

        – Danyal Imran
        2 days ago











      • correct is 100000098765432

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi No it is not... that was not a condition in the question.

        – Mr. Polywhirl
        2 days ago






      • 3





        OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

        – Danyal Imran
        2 days ago



















      • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

        – Danyal Imran
        2 days ago











      • correct is 100000098765432

        – mohammad javad ahmadi
        2 days ago






      • 1





        @mohammadjavadahmadi No it is not... that was not a condition in the question.

        – Mr. Polywhirl
        2 days ago






      • 3





        OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

        – Danyal Imran
        2 days ago

















      this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

      – mohammad javad ahmadi
      2 days ago





      this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

      – mohammad javad ahmadi
      2 days ago




      1




      1





      @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

      – Danyal Imran
      2 days ago





      @mohammadjavadahmadi I am getting "000001098765432", which seems to be the correct value.

      – Danyal Imran
      2 days ago













      correct is 100000098765432

      – mohammad javad ahmadi
      2 days ago





      correct is 100000098765432

      – mohammad javad ahmadi
      2 days ago




      1




      1





      @mohammadjavadahmadi No it is not... that was not a condition in the question.

      – Mr. Polywhirl
      2 days ago





      @mohammadjavadahmadi No it is not... that was not a condition in the question.

      – Mr. Polywhirl
      2 days ago




      3




      3





      OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

      – Danyal Imran
      2 days ago





      OP said that he doesn't want to reverse 10, he doesn't say anything about how other values before or after 10 are affected. I don't know why would you even think that since its not even mentioned anywhere lol @mohammadjavadahmadi

      – Danyal Imran
      2 days ago











      3














      You can reduce over the matched array from using a regular expression. It's more costly than a for/loop that concatenates strings, but it was fun figuring it out.






      function split(str) {
      const re = /([A-Z23456789 ]+)|(10)/g
      return str.match(re).reduce((acc, c) => {

      // if the match is 10 prepend it to the accumulator
      // otherwise reverse the match and then prepend it
      acc.unshift(c === '10' ? c : [...c].reverse().join(''));
      return acc;
      }, ).join('');
      }

      console.log(split('2345678910'));
      console.log(split('10 2 3 U S A'));
      console.log(split('2 3 U S A10'));








      share|improve this answer






























        3














        You can reduce over the matched array from using a regular expression. It's more costly than a for/loop that concatenates strings, but it was fun figuring it out.






        function split(str) {
        const re = /([A-Z23456789 ]+)|(10)/g
        return str.match(re).reduce((acc, c) => {

        // if the match is 10 prepend it to the accumulator
        // otherwise reverse the match and then prepend it
        acc.unshift(c === '10' ? c : [...c].reverse().join(''));
        return acc;
        }, ).join('');
        }

        console.log(split('2345678910'));
        console.log(split('10 2 3 U S A'));
        console.log(split('2 3 U S A10'));








        share|improve this answer




























          3












          3








          3







          You can reduce over the matched array from using a regular expression. It's more costly than a for/loop that concatenates strings, but it was fun figuring it out.






          function split(str) {
          const re = /([A-Z23456789 ]+)|(10)/g
          return str.match(re).reduce((acc, c) => {

          // if the match is 10 prepend it to the accumulator
          // otherwise reverse the match and then prepend it
          acc.unshift(c === '10' ? c : [...c].reverse().join(''));
          return acc;
          }, ).join('');
          }

          console.log(split('2345678910'));
          console.log(split('10 2 3 U S A'));
          console.log(split('2 3 U S A10'));








          share|improve this answer















          You can reduce over the matched array from using a regular expression. It's more costly than a for/loop that concatenates strings, but it was fun figuring it out.






          function split(str) {
          const re = /([A-Z23456789 ]+)|(10)/g
          return str.match(re).reduce((acc, c) => {

          // if the match is 10 prepend it to the accumulator
          // otherwise reverse the match and then prepend it
          acc.unshift(c === '10' ? c : [...c].reverse().join(''));
          return acc;
          }, ).join('');
          }

          console.log(split('2345678910'));
          console.log(split('10 2 3 U S A'));
          console.log(split('2 3 U S A10'));








          function split(str) {
          const re = /([A-Z23456789 ]+)|(10)/g
          return str.match(re).reduce((acc, c) => {

          // if the match is 10 prepend it to the accumulator
          // otherwise reverse the match and then prepend it
          acc.unshift(c === '10' ? c : [...c].reverse().join(''));
          return acc;
          }, ).join('');
          }

          console.log(split('2345678910'));
          console.log(split('10 2 3 U S A'));
          console.log(split('2 3 U S A10'));





          function split(str) {
          const re = /([A-Z23456789 ]+)|(10)/g
          return str.match(re).reduce((acc, c) => {

          // if the match is 10 prepend it to the accumulator
          // otherwise reverse the match and then prepend it
          acc.unshift(c === '10' ? c : [...c].reverse().join(''));
          return acc;
          }, ).join('');
          }

          console.log(split('2345678910'));
          console.log(split('10 2 3 U S A'));
          console.log(split('2 3 U S A10'));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          AndyAndy

          29.5k73462




          29.5k73462























              1














              You need some pre-conditions to check each character's value.



              Due to the vagueness of the question, it is reasonable to believe that the number system that OP defines consists of [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.






              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));





              Output:




              A S U 3 2 10
              1098765432






              share|improve this answer


























              • for 'USA101001' it gives 11010, which looks wrong.

                – Pac0
                2 days ago











              • Based on the original example: "e.g, 2345678910 would be 1098765432."

                – Mr. Polywhirl
                2 days ago











              • my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

                – Pac0
                2 days ago











              • Ok, I fixed it.

                – Mr. Polywhirl
                2 days ago











              • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

                – mohammad javad ahmadi
                2 days ago
















              1














              You need some pre-conditions to check each character's value.



              Due to the vagueness of the question, it is reasonable to believe that the number system that OP defines consists of [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.






              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));





              Output:




              A S U 3 2 10
              1098765432






              share|improve this answer


























              • for 'USA101001' it gives 11010, which looks wrong.

                – Pac0
                2 days ago











              • Based on the original example: "e.g, 2345678910 would be 1098765432."

                – Mr. Polywhirl
                2 days ago











              • my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

                – Pac0
                2 days ago











              • Ok, I fixed it.

                – Mr. Polywhirl
                2 days ago











              • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

                – mohammad javad ahmadi
                2 days ago














              1












              1








              1







              You need some pre-conditions to check each character's value.



              Due to the vagueness of the question, it is reasonable to believe that the number system that OP defines consists of [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.






              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));





              Output:




              A S U 3 2 10
              1098765432






              share|improve this answer















              You need some pre-conditions to check each character's value.



              Due to the vagueness of the question, it is reasonable to believe that the number system that OP defines consists of [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.






              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));





              Output:




              A S U 3 2 10
              1098765432






              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));





              String.prototype.isNumeric = function() {
              return !isNaN(parseFloat(this)) && isFinite(this);
              };

              function reverse(str) {
              let tokens = , len = str.length;
              while (len--) {
              let char = str.charAt(len);
              if (char.isNumeric()) {
              if (len > 0 && str.charAt(len - 1).isNumeric()) {
              let curr = parseInt(char, 10),
              next = parseInt(str.charAt(len - 1), 10);
              if (curr === 0 && next === 1) {
              tokens.push(10);
              len--;
              continue;
              }
              }
              }
              tokens.push(char);
              }
              return tokens.join('');
              }

              console.log(reverse("10 2 3 U S A"));
              console.log(reverse('2345678910'));






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered 2 days ago









              Mr. PolywhirlMr. Polywhirl

              16.7k84888




              16.7k84888













              • for 'USA101001' it gives 11010, which looks wrong.

                – Pac0
                2 days ago











              • Based on the original example: "e.g, 2345678910 would be 1098765432."

                – Mr. Polywhirl
                2 days ago











              • my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

                – Pac0
                2 days ago











              • Ok, I fixed it.

                – Mr. Polywhirl
                2 days ago











              • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

                – mohammad javad ahmadi
                2 days ago



















              • for 'USA101001' it gives 11010, which looks wrong.

                – Pac0
                2 days ago











              • Based on the original example: "e.g, 2345678910 would be 1098765432."

                – Mr. Polywhirl
                2 days ago











              • my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

                – Pac0
                2 days ago











              • Ok, I fixed it.

                – Mr. Polywhirl
                2 days ago











              • this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

                – mohammad javad ahmadi
                2 days ago

















              for 'USA101001' it gives 11010, which looks wrong.

              – Pac0
              2 days ago





              for 'USA101001' it gives 11010, which looks wrong.

              – Pac0
              2 days ago













              Based on the original example: "e.g, 2345678910 would be 1098765432."

              – Mr. Polywhirl
              2 days ago





              Based on the original example: "e.g, 2345678910 would be 1098765432."

              – Mr. Polywhirl
              2 days ago













              my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

              – Pac0
              2 days ago





              my example is a bit complex, but OP gave at least an example with letters, 10 2 3 U S A

              – Pac0
              2 days ago













              Ok, I fixed it.

              – Mr. Polywhirl
              2 days ago





              Ok, I fixed it.

              – Mr. Polywhirl
              2 days ago













              this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

              – mohammad javad ahmadi
              2 days ago





              this code dose not work for console.log(reverse("234567891000000")) only uses for 2345678910

              – mohammad javad ahmadi
              2 days ago











              1














              Below is a recursive approach.






              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));








              share|improve this answer


























              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

                – elena a
                2 days ago











              • U S A 10 10 0 11 0 10 10 A S U101010ASU

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

                – elena a
                2 days ago











              • Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

                – Mr. Polywhirl
                2 days ago


















              1














              Below is a recursive approach.






              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));








              share|improve this answer


























              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

                – elena a
                2 days ago











              • U S A 10 10 0 11 0 10 10 A S U101010ASU

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

                – elena a
                2 days ago











              • Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

                – Mr. Polywhirl
                2 days ago
















              1












              1








              1







              Below is a recursive approach.






              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));








              share|improve this answer















              Below is a recursive approach.






              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));








              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));





              function f(s, i=0){
              if (i == s.length)
              return '';
              if (['0', '1'].includes(s[i])){
              let curr = s[i];
              while (['0', '1'].includes(s[++i]))
              curr += s[i]
              return f(s, i) + curr;
              }
              return f(s, i + 1) + s[i];
              }

              console.log(f('10 2 3 U S A'));
              console.log(f('2345678910'));
              console.log(f('USA101001'));






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered 2 days ago









              elena aelena a

              764




              764













              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

                – elena a
                2 days ago











              • U S A 10 10 0 11 0 10 10 A S U101010ASU

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

                – elena a
                2 days ago











              • Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

                – Mr. Polywhirl
                2 days ago





















              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

                – elena a
                2 days ago











              • U S A 10 10 0 11 0 10 10 A S U101010ASU

                – Mr. Polywhirl
                2 days ago











              • @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

                – elena a
                2 days ago











              • Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

                – Mr. Polywhirl
                2 days ago



















              Wouldn't USA101001 be 101010ASU ?

              – Mr. Polywhirl
              2 days ago





              Wouldn't USA101001 be 101010ASU ?

              – Mr. Polywhirl
              2 days ago













              @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

              – elena a
              2 days ago





              @Mr.Polywhirl I thought sequences of 1 and 0 were not to be reversed.

              – elena a
              2 days ago













              U S A 10 10 0 11 0 10 10 A S U101010ASU

              – Mr. Polywhirl
              2 days ago





              U S A 10 10 0 11 0 10 10 A S U101010ASU

              – Mr. Polywhirl
              2 days ago













              @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

              – elena a
              2 days ago





              @Mr.Polywhirl can you please show me where the OP indicated your interpretation is the correct one?

              – elena a
              2 days ago













              Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

              – Mr. Polywhirl
              2 days ago







              Due to the vagueness of the question, it is reasonable to believe that the number system is [2, 3, 4, 5, 6, 7, 8, 9, 10] and all other characters A-Z (including 0 and 1) are simply characters.

              – Mr. Polywhirl
              2 days ago













              0














              Nice question so far.



              You may try this recursive approach(if not changing 10 for other character not allowed):






              function reverseKeepTen(str, arr = ) {
              const tenIdx = str.indexOf('10');

              if (!str.length) {
              return arr.join('');
              }

              if (tenIdx === -1) {
              return [...str.split('').reverse(), ...arr].join('');
              } else {
              const digitsBefore = str.slice(0, tenIdx);

              const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
              return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
              }
              };


              console.log(reverseKeepTen('101234105678910')) // 109876510432110
              console.log(reverseKeepTen('12341056789')) // 98765104321
              console.log(reverseKeepTen('1012345')) // 5432110
              console.log(reverseKeepTen('5678910')) // 1098765
              console.log(reverseKeepTen('10111101')) // 11011110








              share|improve this answer






























                0














                Nice question so far.



                You may try this recursive approach(if not changing 10 for other character not allowed):






                function reverseKeepTen(str, arr = ) {
                const tenIdx = str.indexOf('10');

                if (!str.length) {
                return arr.join('');
                }

                if (tenIdx === -1) {
                return [...str.split('').reverse(), ...arr].join('');
                } else {
                const digitsBefore = str.slice(0, tenIdx);

                const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
                return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
                }
                };


                console.log(reverseKeepTen('101234105678910')) // 109876510432110
                console.log(reverseKeepTen('12341056789')) // 98765104321
                console.log(reverseKeepTen('1012345')) // 5432110
                console.log(reverseKeepTen('5678910')) // 1098765
                console.log(reverseKeepTen('10111101')) // 11011110








                share|improve this answer




























                  0












                  0








                  0







                  Nice question so far.



                  You may try this recursive approach(if not changing 10 for other character not allowed):






                  function reverseKeepTen(str, arr = ) {
                  const tenIdx = str.indexOf('10');

                  if (!str.length) {
                  return arr.join('');
                  }

                  if (tenIdx === -1) {
                  return [...str.split('').reverse(), ...arr].join('');
                  } else {
                  const digitsBefore = str.slice(0, tenIdx);

                  const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
                  return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
                  }
                  };


                  console.log(reverseKeepTen('101234105678910')) // 109876510432110
                  console.log(reverseKeepTen('12341056789')) // 98765104321
                  console.log(reverseKeepTen('1012345')) // 5432110
                  console.log(reverseKeepTen('5678910')) // 1098765
                  console.log(reverseKeepTen('10111101')) // 11011110








                  share|improve this answer















                  Nice question so far.



                  You may try this recursive approach(if not changing 10 for other character not allowed):






                  function reverseKeepTen(str, arr = ) {
                  const tenIdx = str.indexOf('10');

                  if (!str.length) {
                  return arr.join('');
                  }

                  if (tenIdx === -1) {
                  return [...str.split('').reverse(), ...arr].join('');
                  } else {
                  const digitsBefore = str.slice(0, tenIdx);

                  const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
                  return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
                  }
                  };


                  console.log(reverseKeepTen('101234105678910')) // 109876510432110
                  console.log(reverseKeepTen('12341056789')) // 98765104321
                  console.log(reverseKeepTen('1012345')) // 5432110
                  console.log(reverseKeepTen('5678910')) // 1098765
                  console.log(reverseKeepTen('10111101')) // 11011110








                  function reverseKeepTen(str, arr = ) {
                  const tenIdx = str.indexOf('10');

                  if (!str.length) {
                  return arr.join('');
                  }

                  if (tenIdx === -1) {
                  return [...str.split('').reverse(), ...arr].join('');
                  } else {
                  const digitsBefore = str.slice(0, tenIdx);

                  const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
                  return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
                  }
                  };


                  console.log(reverseKeepTen('101234105678910')) // 109876510432110
                  console.log(reverseKeepTen('12341056789')) // 98765104321
                  console.log(reverseKeepTen('1012345')) // 5432110
                  console.log(reverseKeepTen('5678910')) // 1098765
                  console.log(reverseKeepTen('10111101')) // 11011110





                  function reverseKeepTen(str, arr = ) {
                  const tenIdx = str.indexOf('10');

                  if (!str.length) {
                  return arr.join('');
                  }

                  if (tenIdx === -1) {
                  return [...str.split('').reverse(), ...arr].join('');
                  } else {
                  const digitsBefore = str.slice(0, tenIdx);

                  const arrBefore = digitsBefore ? [...digitsBefore.split(''), 10].reverse() : [10];
                  return reverseKeepTen(str.slice(tenIdx + 2), [...arrBefore, ...arr])
                  }
                  };


                  console.log(reverseKeepTen('101234105678910')) // 109876510432110
                  console.log(reverseKeepTen('12341056789')) // 98765104321
                  console.log(reverseKeepTen('1012345')) // 5432110
                  console.log(reverseKeepTen('5678910')) // 1098765
                  console.log(reverseKeepTen('10111101')) // 11011110






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  Shevchenko ViktorShevchenko Viktor

                  864615




                  864615















                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?