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












10















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















closed as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap Jan 24 at 9:40


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
    Jan 23 at 15:10






  • 1





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

    – Pac0
    Jan 23 at 15:12








  • 1





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

    – Pac0
    Jan 23 at 15:17








  • 2





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

    – Pac0
    Jan 23 at 15:22








  • 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
    Jan 23 at 17:06


















10















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















closed as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap Jan 24 at 9:40


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
    Jan 23 at 15:10






  • 1





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

    – Pac0
    Jan 23 at 15:12








  • 1





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

    – Pac0
    Jan 23 at 15:17








  • 2





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

    – Pac0
    Jan 23 at 15:22








  • 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
    Jan 23 at 17:06
















10












10








10


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 Jan 23 at 15:16









Pac0

7,84122745




7,84122745










asked Jan 23 at 15:06









user8107351user8107351

818




818




closed as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap Jan 24 at 9:40


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.









closed as unclear what you're asking by Pham Trung, גלעד ברקן, KittMedia, Graipher, Vikrant Kashyap Jan 24 at 9:40


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
    Jan 23 at 15:10






  • 1





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

    – Pac0
    Jan 23 at 15:12








  • 1





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

    – Pac0
    Jan 23 at 15:17








  • 2





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

    – Pac0
    Jan 23 at 15:22








  • 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
    Jan 23 at 17:06
















  • 1





    please format you code!

    – MrSmith42
    Jan 23 at 15:10






  • 1





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

    – Pac0
    Jan 23 at 15:12








  • 1





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

    – Pac0
    Jan 23 at 15:17








  • 2





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

    – Pac0
    Jan 23 at 15:22








  • 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
    Jan 23 at 17:06










1




1





please format you code!

– MrSmith42
Jan 23 at 15:10





please format you code!

– MrSmith42
Jan 23 at 15:10




1




1





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

– Pac0
Jan 23 at 15:12







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

– Pac0
Jan 23 at 15:12






1




1





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

– Pac0
Jan 23 at 15:17







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

– Pac0
Jan 23 at 15:17






2




2





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

– Pac0
Jan 23 at 15:22







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

– Pac0
Jan 23 at 15:22






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
Jan 23 at 17:06







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
Jan 23 at 17:06














6 Answers
6






active

oldest

votes


















13














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
    Jan 23 at 15:20











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

    – Pac0
    Jan 23 at 15:22






  • 1





    @Pac0 What should be the result for USA101001?

    – OmG
    Jan 23 at 15:24











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

    – Pac0
    Jan 23 at 15:25






  • 1





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

    – Amy
    Jan 23 at 15:26



















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
    Jan 23 at 15:38






  • 1





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

    – Danyal Imran
    Jan 23 at 15:40






  • 1





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

    – Mr. Polywhirl
    Jan 23 at 15:44






  • 4





    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
    Jan 23 at 15:44






  • 1





    @Pac0 definitely, we need more test cases for clarity.

    – Danyal Imran
    Jan 23 at 16:14



















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
      Jan 23 at 15:24











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

      – Mr. Polywhirl
      Jan 23 at 15:25











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

      – Pac0
      Jan 23 at 15:32











    • Ok, I fixed it.

      – Mr. Polywhirl
      Jan 23 at 15:39











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

      – mohammad javad ahmadi
      Jan 23 at 15:40



















    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
      Jan 23 at 15:46











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

      – elena a
      Jan 23 at 15:48











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

      – Mr. Polywhirl
      Jan 23 at 15:49











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

      – elena a
      Jan 23 at 15:51











    • 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
      Jan 23 at 15:54





















    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









      13














      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
        Jan 23 at 15:20











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

        – Pac0
        Jan 23 at 15:22






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        Jan 23 at 15:24











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

        – Pac0
        Jan 23 at 15:25






      • 1





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

        – Amy
        Jan 23 at 15:26
















      13














      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
        Jan 23 at 15:20











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

        – Pac0
        Jan 23 at 15:22






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        Jan 23 at 15:24











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

        – Pac0
        Jan 23 at 15:25






      • 1





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

        – Amy
        Jan 23 at 15:26














      13












      13








      13







      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 Jan 23 at 15:57

























      answered Jan 23 at 15:14









      OmGOmG

      8,23352945




      8,23352945








      • 1





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

        – Pac0
        Jan 23 at 15:20











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

        – Pac0
        Jan 23 at 15:22






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        Jan 23 at 15:24











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

        – Pac0
        Jan 23 at 15:25






      • 1





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

        – Amy
        Jan 23 at 15:26














      • 1





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

        – Pac0
        Jan 23 at 15:20











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

        – Pac0
        Jan 23 at 15:22






      • 1





        @Pac0 What should be the result for USA101001?

        – OmG
        Jan 23 at 15:24











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

        – Pac0
        Jan 23 at 15:25






      • 1





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

        – Amy
        Jan 23 at 15:26








      1




      1





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

      – Pac0
      Jan 23 at 15:20





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

      – Pac0
      Jan 23 at 15:20













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

      – Pac0
      Jan 23 at 15:22





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

      – Pac0
      Jan 23 at 15:22




      1




      1





      @Pac0 What should be the result for USA101001?

      – OmG
      Jan 23 at 15:24





      @Pac0 What should be the result for USA101001?

      – OmG
      Jan 23 at 15:24













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

      – Pac0
      Jan 23 at 15:25





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

      – Pac0
      Jan 23 at 15:25




      1




      1





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

      – Amy
      Jan 23 at 15:26





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

      – Amy
      Jan 23 at 15:26













      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
        Jan 23 at 15:38






      • 1





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

        – Danyal Imran
        Jan 23 at 15:40






      • 1





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

        – Mr. Polywhirl
        Jan 23 at 15:44






      • 4





        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
        Jan 23 at 15:44






      • 1





        @Pac0 definitely, we need more test cases for clarity.

        – Danyal Imran
        Jan 23 at 16:14
















      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
        Jan 23 at 15:38






      • 1





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

        – Danyal Imran
        Jan 23 at 15:40






      • 1





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

        – Mr. Polywhirl
        Jan 23 at 15:44






      • 4





        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
        Jan 23 at 15:44






      • 1





        @Pac0 definitely, we need more test cases for clarity.

        – Danyal Imran
        Jan 23 at 16:14














      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 Jan 23 at 15:33









      Pac0

      7,84122745




      7,84122745










      answered Jan 23 at 15:25









      Danyal ImranDanyal Imran

      1,227214




      1,227214













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

        – mohammad javad ahmadi
        Jan 23 at 15:38






      • 1





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

        – Danyal Imran
        Jan 23 at 15:40






      • 1





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

        – Mr. Polywhirl
        Jan 23 at 15:44






      • 4





        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
        Jan 23 at 15:44






      • 1





        @Pac0 definitely, we need more test cases for clarity.

        – Danyal Imran
        Jan 23 at 16:14



















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

        – mohammad javad ahmadi
        Jan 23 at 15:38






      • 1





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

        – Danyal Imran
        Jan 23 at 15:40






      • 1





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

        – Mr. Polywhirl
        Jan 23 at 15:44






      • 4





        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
        Jan 23 at 15:44






      • 1





        @Pac0 definitely, we need more test cases for clarity.

        – Danyal Imran
        Jan 23 at 16:14

















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

      – mohammad javad ahmadi
      Jan 23 at 15:38





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

      – mohammad javad ahmadi
      Jan 23 at 15:38




      1




      1





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

      – Danyal Imran
      Jan 23 at 15:40





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

      – Danyal Imran
      Jan 23 at 15:40




      1




      1





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

      – Mr. Polywhirl
      Jan 23 at 15:44





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

      – Mr. Polywhirl
      Jan 23 at 15:44




      4




      4





      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
      Jan 23 at 15:44





      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
      Jan 23 at 15:44




      1




      1





      @Pac0 definitely, we need more test cases for clarity.

      – Danyal Imran
      Jan 23 at 16:14





      @Pac0 definitely, we need more test cases for clarity.

      – Danyal Imran
      Jan 23 at 16:14











      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 Jan 23 at 16:30

























          answered Jan 23 at 15:54









          AndyAndy

          29.6k73462




          29.6k73462























              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
                Jan 23 at 15:24











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

                – Mr. Polywhirl
                Jan 23 at 15:25











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

                – Pac0
                Jan 23 at 15:32











              • Ok, I fixed it.

                – Mr. Polywhirl
                Jan 23 at 15:39











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

                – mohammad javad ahmadi
                Jan 23 at 15:40
















              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
                Jan 23 at 15:24











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

                – Mr. Polywhirl
                Jan 23 at 15:25











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

                – Pac0
                Jan 23 at 15:32











              • Ok, I fixed it.

                – Mr. Polywhirl
                Jan 23 at 15:39











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

                – mohammad javad ahmadi
                Jan 23 at 15:40














              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 Jan 23 at 16:09

























              answered Jan 23 at 15:22









              Mr. PolywhirlMr. Polywhirl

              16.9k84888




              16.9k84888













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

                – Pac0
                Jan 23 at 15:24











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

                – Mr. Polywhirl
                Jan 23 at 15:25











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

                – Pac0
                Jan 23 at 15:32











              • Ok, I fixed it.

                – Mr. Polywhirl
                Jan 23 at 15:39











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

                – mohammad javad ahmadi
                Jan 23 at 15:40



















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

                – Pac0
                Jan 23 at 15:24











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

                – Mr. Polywhirl
                Jan 23 at 15:25











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

                – Pac0
                Jan 23 at 15:32











              • Ok, I fixed it.

                – Mr. Polywhirl
                Jan 23 at 15:39











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

                – mohammad javad ahmadi
                Jan 23 at 15:40

















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

              – Pac0
              Jan 23 at 15:24





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

              – Pac0
              Jan 23 at 15:24













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

              – Mr. Polywhirl
              Jan 23 at 15:25





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

              – Mr. Polywhirl
              Jan 23 at 15:25













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

              – Pac0
              Jan 23 at 15:32





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

              – Pac0
              Jan 23 at 15:32













              Ok, I fixed it.

              – Mr. Polywhirl
              Jan 23 at 15:39





              Ok, I fixed it.

              – Mr. Polywhirl
              Jan 23 at 15:39













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

              – mohammad javad ahmadi
              Jan 23 at 15:40





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

              – mohammad javad ahmadi
              Jan 23 at 15:40











              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
                Jan 23 at 15:46











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

                – elena a
                Jan 23 at 15:48











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

                – Mr. Polywhirl
                Jan 23 at 15:49











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

                – elena a
                Jan 23 at 15:51











              • 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
                Jan 23 at 15:54


















              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
                Jan 23 at 15:46











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

                – elena a
                Jan 23 at 15:48











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

                – Mr. Polywhirl
                Jan 23 at 15:49











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

                – elena a
                Jan 23 at 15:51











              • 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
                Jan 23 at 15:54
















              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 Jan 23 at 17:30

























              answered Jan 23 at 15:44









              elena aelena a

              764




              764













              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                Jan 23 at 15:46











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

                – elena a
                Jan 23 at 15:48











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

                – Mr. Polywhirl
                Jan 23 at 15:49











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

                – elena a
                Jan 23 at 15:51











              • 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
                Jan 23 at 15:54





















              • Wouldn't USA101001 be 101010ASU ?

                – Mr. Polywhirl
                Jan 23 at 15:46











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

                – elena a
                Jan 23 at 15:48











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

                – Mr. Polywhirl
                Jan 23 at 15:49











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

                – elena a
                Jan 23 at 15:51











              • 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
                Jan 23 at 15:54



















              Wouldn't USA101001 be 101010ASU ?

              – Mr. Polywhirl
              Jan 23 at 15:46





              Wouldn't USA101001 be 101010ASU ?

              – Mr. Polywhirl
              Jan 23 at 15:46













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

              – elena a
              Jan 23 at 15:48





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

              – elena a
              Jan 23 at 15:48













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

              – Mr. Polywhirl
              Jan 23 at 15:49





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

              – Mr. Polywhirl
              Jan 23 at 15:49













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

              – elena a
              Jan 23 at 15:51





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

              – elena a
              Jan 23 at 15:51













              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
              Jan 23 at 15:54







              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
              Jan 23 at 15:54













              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 Jan 23 at 16:03

























                  answered Jan 23 at 15:55









                  Shevchenko ViktorShevchenko Viktor

                  1,019816




                  1,019816















                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents