When i try the function i try to display every letter in the aplhabet
it outputs ("01bcdefghijklmnoprstuvwqxyz").
So it only turns letter "a" to a number "01".The rest of the letters stay the same.I am trying to do this specifically for lating letter so when the code is executed it only display numbers.
This is the code i have tried:
function letters_to_numbers(st)
{
a = st.replace("a", "01");
return a;
b = st.replace("b", "02");
return b;
c = st.replace("c", "03");
return c;
d = st.replace("d", "04");
return d;
e = st.replace("e", "05");
return e;
f = st.replace("f", "06");
return f;
g = st.replace("g", "07");
return g;
h = st.replace("h", "08");
return h;
i = st.replace("i", "09");
return i;
j = st.replace("j", "10");
return j;
k = st.replace("k", "11");
return k;
l = st.replace("l", "12");
return l;
m = st.replace("m", "13");
return m;
n = st.replace("n", "14");
return n;
o = st.replace("o", "15");
return o;
p = st.replace("p", "16");
return p;
r = st.replace("r", "17");
return r;
s = st.replace("s", "18");
return s;
t = st.replace("t", "19");
return r;
u = st.replace("u", "20");
return u;
v = st.replace("v", "21");
return v;
w = st.replace("w", "22");
return w;
q = st.replace("q", "23");
return q;
x = st.replace("x", "24");
return x;
y = st.replace("y", "25");
return y;
z = st.replace("z", "26");
return z;
}
The return
statement stops the execution of a function.
Instead, try adding the results to an empty array for each code execution, and I would look into looping through arrays for this instead of hard-coding it 26+ times.
If you want to read more about return statement: https://www.w3schools.com/jsref/jsref_return.asp
If you want to read more about for loops: https://www.w3schools.com/js/js_loop_for.asp
Your function 'returns' as soon as it finds a match for any letter. Since 'a' is first in your ladder of tests, any string that contains an 'a' will only get that replaced.
Each section of your function operates on the original string, you need to alter it so that each one is aware of the changes made by the previous test.
You also need to make your RegExp 'global' with the g flag, otherwise it will only ever replace the first occurance of any letter.
Once a
is replaced, you are immediately returning from the function. Instead, you need replace
each character and return only after z
is replaced:
function letters_to_numbers(st) {
st = st.replace("a", "01");
st = st.replace("b", "02");
st = st.replace("c", "03");
...............
...............
st = st.replace("z", "26");
return st;
}
Another alternative is to replace
all az
characters based on their charCode.
The charcode of a
-> 97 and b
-> 98 and so on. So, m.charCodeAt(0) - 96
returns a number between 1-26. Then, you can use padStart
to get the 0 prefix for numbers with single digit
function letters_to_numbers(st) { return st.replace(/[az]/g, m => (m.charCodeAt(0) - 96).toString().padStart(2, 0)) } console.log(letters_to_numbers("abcdefghijklmnoprstuvwqxyz")) console.log(letters_to_numbers("lorem ipsum"))
The return ends the function.
Don't return anything until the very end.
update: shoter version
If you want to scramble the alphabet for encryption or something, just change the order of the letters in alphabet
.
function letters_to_numbers(st)
{
var updatedString = st + '';
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
for (var i = 0; i < alphabet.length; i++) {
var letter = alphabet.charAt(i);
var number = i.toString().padStart(2, '0');
updatedString = updatedString.replace(letter, number);
};
return updatedString;
}
the old way This would get you started:
function letters_to_numbers(st)
{
let updatedString;
updatedString = st.replace("a", "01");
updatedString = updatedString.replace("b", "02")
updatedString = updatedString.replace("c", "03");
updatedString = updatedString.replace("d", "04");
updatedString = updatedString.replace("e", "05");
updatedString = updatedString.replace("f", "06");
updatedString = updatedString.replace("g", "07");
updatedString = updatedString.replace("h", "08");
updatedString = updatedString.replace("i", "09");
updatedString = updatedString.replace("j", "10");
updatedString = updatedString.replace("k", "11");
updatedString = updatedString.replace("l", "12");
updatedString = updatedString.replace("m", "13");
updatedString = updatedString.replace("n", "14");
updatedString = updatedString.replace("o", "15");
updatedString = updatedString.replace("p", "16");
updatedString = updatedString.replace("r", "17");
updatedString = updatedString.replace("s", "18");
updatedString = updatedString.replace("t", "19");
updatedString = updatedString.replace("u", "20");
updatedString = updatedString.replace("v", "21");
updatedString = updatedString.replace("w", "22");
updatedString = updatedString.replace("q", "23");
updatedString = updatedString.replace("x", "24");
updatedString = updatedString.replace("y", "25");
updatedString = updatedString.replace("z", "26");
return updatedString;
}
string.replace()
only replaces the first match if your pattern is a string. Use a regular rexpression with a global flag to match multiple times. Also, you can use a function as your replacement value to dynamically determine a replacement.
You can either use a lookup object to map your letters to numbers, or just use ASCII character codes. 97
to 122
is all lowercase. Just subtract 96
from the character's charcode, and pad zeroes to get your number.
// Adds two zeroes ahead of the number, then take the last two digits. const pad = n => `00${n}`.slice(-2) const lettersToNumbers = s => { // Match all lowercase letters. return s.replace(/[az]/g, m => { // For each match, get the character code. // Subtract by 96 so that "a" is 1. return pad(m.charCodeAt(0) - 96) }) } console.log(lettersToNumbers('abcdefghijklmnopqrstuvwxyz'))
The problem doesn't seem to be worth so many lines of code:
const strToNum = str => [...str] .map(char => ([...'abcdefghigklmnopqrstuvwxyz'].indexOf(char)+1) .toString() .padStart(2,0)) .join(''); console.log(strToNum('test'));
Here is another way to implement this
const input = "abcdefghijklmnoprstuvwqxyz"; const letterToNumber = (char) => { const initialValue = 'a'.charCodeAt(0); num = char.charCodeAt(0) - initialValue + 1; return num < 10 ? '0'+num : num; } let output = input.split('').reduce((convertedString, char) => { convertedString += letterToNumber(char); return convertedString; },'') console.log(output)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.