Solved

JavaScript: Shift character values by 1 for every character

Posted on 2013-05-30
2
220 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I'm a Human captcha checkbox 13 39
Adding 1 the value of input "text" every 3 seconds 5 32
Rotate images for Web Page 11 24
Aspx calendar pop up control 3 25
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

679 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