Assigning numbers to letters in Javascript Phrase with what they are in the alphabet

时间:2016-11-12 21:59:01

标签: javascript arrays

I am wondering how I would take a phrase that is an array in Javascript then make it so it would output letters next to the letter to show where in the alphabet they are such as Computer outputting 3C15o13m21u and so on. The phrase I have is shown in the code below

var firstPhrase = "Computers are the most fun way to use your time";
alert(firstPhrase);

1 个答案:

答案 0 :(得分:0)

这应该有效:

var phrase = 'Computers are the most fun way to use your time';
console.log(phrase.replace(/([a-zA-Z)/g, function(c) {
    return (c.toUpperCase().charCodeAt(0) - 64) + c;
}));