Change Javascript string characters to successors

2

I have a string, for example, acbd and I want to get another string bdce , formed by the successors of each character.

In my searches, find a possible way to transform the string into an array and then iterate it to get a new string.

var r = "acbd".split("").map(function(a) { 
  return String.fromCharCode(a.charCodeAt(0) + 1)
}).reduce(function(p, c) {
  return p + c
});

console.log(r);

However, I'm trying to figure out a possible algorithm to change the characters for their successors, without transforming the string into an array using the replace method with a RegExp . Could someone help me?

    
asked by anonymous 05.01.2018 / 19:43

1 answer

2

Here is the code:

var r = "abcd".replace(/[a-zA-Z]/g, function myFunction(x){
  return String.fromCharCode(x.charCodeAt(0)+1);
});
console.log(r);
    
05.01.2018 / 20:19