4

I'm trying to create a string increaser function that will take one string and raise it up one, for example:

000000 is the first string, then comes 000001, up to 000009 where it then goes onto 00000a up to 00000z.

My idea was that I will replace the last pattern (z) with the first (0), then, get the point where it was replaced and increase that by one in the pattern.

The code I tried to use:

cycler(string) {
   //string="00000z"
   var pos = string.replace('z', '0');
   console.log(pos);
}

I sadly realized that the return of this function was the replaced version, not what I was after. Which makes total logical sense.

I'm wondering if there is a function in JavaScript for this functionality of finding where the all the replaces were made?

Jack Hales
  • 1,574
  • 23
  • 51

3 Answers3

1

If I understand correctly, you want to replace all occurrences.

cycler(string) {
   //string="00000z"
   var pos = string.replace(/z/g, '0');
   console.log(pos);
} 

Ignore if misunderstood. Thanks.

  • 1
    I guess you misread. The title says _"Get the point where the replace occurred in a replace"_ and in the question _"...finding where the all the replaces were made?"_, and nowhere is asked how to _replace all occurrences_ – Asons Jul 24 '18 at 08:09
1

this way you can increase the string. first you find the place you want to change. and then you change it

const range = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];


cycler = function(string) {   
   const pos = string.match(/[0-9a-z]z/ )
   return pos === null ? string.length - 1 : pos.index
}

inc =  function(string,pos) {   

  let rangeIndex  = range.indexOf(string[pos]);
  rangeIndex++;
  let arr = string.split("");
  arr[pos] = range[rangeIndex];
 
  return arr.join("");
}

var string = '00002z';
const  pos = cycler(string);
alert(inc(string,pos))
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
1

I don't think there is any inbuilt method for this but you can create your customized logic as follows:

var orgstr = '000000';



var arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];



function getNextStr(str) {
  var count = (str.match(/0/g) || []).length;
  var rStr = ""

  var found = str.replace(/0/g, '');
  var next = getNext(found);
  var nexIn = arr.indexOf(found) + 1;
  if (next.length != found.length) {
    rStr = str.slice(0, count - 1) + next;
  } else {
    rStr = str.slice(0, count) + next;
  }

  return rStr;
}

function getNext(fou) {
  var str = '';
  var nextTobeGrown = true;
  for (var i = fou.length - 1; i >= 0; i--) {
    var x = fou.substring(i + 1, i);
    var ind = arr.indexOf(x);
    if (nextTobeGrown) {

      if (ind + 1 < arr.length) {
        str += arr[ind + 1];
        nextTobeGrown = false;
      } else {
        str += arr[0]
      }
    } else {
      str += x;
    }

  }


  //console.log(str)
  if (nextTobeGrown) {
    str += arr[0];
  }
  return str.split("").reverse().join("");;
}

var rst;
for (var i = 0; i < 160; i++) {
  var org = i == 0 ? orgstr : rst;
  rst = getNextStr(org);
  console.log(rst)
}
Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28