In this kata you are required to, given a string, replace every letter with its position in the alphabet.
If anything in the text isn't a letter, ignore it and don't return it.
"a" = 1, "b" = 2, etc.
Example
alphabet_position("The sunset sets at twelve o' clock.")
Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
(as a string)
How to access this object? it keeps returning a string of undefined.
function alphabetPosition(text) { var alphabet = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 } var number = 0; var string = ""; var letter = ""; for (i = 0; i < text.length; i++) { letter = text.charAt(i); number = alphabet.letter; string += number + " "; } return string; } var res = alphabetPosition("The sunset sets at twelve o' clock."); console.log(res, res === "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11");
Try following
alphabet.letter
should be alphabet[letter.toLowerCase()]
- for ignore case if(number)
function alphabetPosition(text) { var alphabet = {a: 1,b: 2,c: 3,d: 4,e: 5,f: 6,g: 7,h: 8,i: 9,j: 10,k: 11,l: 12,m: 13,n: 14,o: 15,p: 16,q: 17,r: 18,s: 19,t: 20,u: 21,v: 22,w: 23,x: 24,y: 25,z: 26}; var number = 0; var string = ""; var letter =""; for (i=0; i<text.length; i++) { letter = text.charAt(i); number = alphabet[letter.toLowerCase()]; if(number) string += number + " "; } return string; } console.log(alphabetPosition("The sunset sets at twelve o' clock."));
There are some mistakes in your code. Please follow below steps in order to solve the problem.
toLowerCase()
method in order to convert upper case
letters also. function alphabetPosition(text) { var alphabet ={ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 } var number = 0; var string = ""; var letter =""; for (i=0; i< text.length; i++) { letter = text.charAt(i).toLowerCase(); number = alphabet[letter] || ''; string += number + " "; } return string; } console.log(alphabetPosition("The sunset sets at twelve o' clock."))
Also, you can simplify your method by using map
method.
return text.split('').map(c => alphabet[c.toLowerCase()] || '').join(' ');
You can skip that object altogether if you consider that the index of each letter is its ascii/utf-16 code minus 97 (the ascii code of the letter 'a') plus 1 (because your letters start at 1 instead of 0). So you can solve it like this:
function alphabetPosition(text) { var str = ""; for (i = 0; i < text.length; i++) { var c = text.charAt(i).toLowerCase(); var code = c.charCodeAt(0); if (code >= 97 && code <= 122) str += ((code - 97 + 1) + " ") } return str; } console.log(alphabetPosition("The sunset sets at twelve o' clock."));
Use ASCII codes, your code will not required alphabet array
var str = "The sunset sets at twelve o' clock."; function CharToAsciiConversion(str){ var result = ""; str = str.toLowerCase(); for (var i = 0; i < str.length; i++) { var ascii = str.charCodeAt(i) - 96; if(ascii > 0 && ascii < 26) result += ascii + " "; } return result; } console.log(CharToAsciiConversion(str));
You had issue with your code: when storing object property name in a variable, you should use []
notation to access property value. in your example you had alphabet.letter
which should have been alphabet[letter]
function alphabetPosition(text) { var alphabet = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 } var number = 0; var string = ""; var letter = ""; for (i = 0; i < text.length; i++) { letter = text.charAt(i).toLowerCase(); number = alphabet[letter]; if (typeof number !== 'undefined') { string += number + " "; } } return string.trim(); } var res = alphabetPosition("The sunset sets at twelve o' clock."); console.log(res, res === "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11");
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.