Solved

JavaScript: Shift character values by 1 for every character

Posted on 2013-05-30
2
225 Views
Last Modified: 2013-05-30
How can I increase the value of all characters by one value?  "A" should become "B" and "1" should become "2":

var x = '123abc';
alert( shift(x) ); // should alert('234bcd');

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39208850
Here's one way:
        var x = '123abc';
        alert(x.replace(/(.)/g, function (x,p1) { return String.fromCharCode(p1.charCodeAt(0)+1) }));

Open in new window

0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39208898
Here's another (perhaps little bit easier to follow) way:
function shift_char(v) {
    var retval = '';
    for (var i=0; i <= v.length; i++) {
        retval += String.fromCharCode(v.charCodeAt(i) + 1);
    }
    return retval;
}
alert(shift_char(x));

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question