[英]How to access this object? it keeps returning a string of undefined
In this kata you are required to, given a string, replace every letter with its position in the alphabet. 在此kata中,需要给定一个字符串,将每个字母替换为其在字母表中的位置。
If anything in the text isn't a letter, ignore it and don't return it. 如果文本中的任何内容都不是字母,请忽略它,不要返回它。
"a" = 1, "b" = 2, etc. “ a” = 1,“ b” = 2,依此类推。
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) 应返回
"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"
(字符串)
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 alphabet.letter
应当alphabet[letter.toLowerCase()]
-用于忽略大小写 if(number)
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. toLowerCase()
方法也可以转换upper case
字母。 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. 另外,您可以使用
map
方法简化方法。
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). 如果您认为每个字母的索引是它的ascii / utf-16代码减去97(字母“ a”的ascii代码)加1(因为您的字母以1而不是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 使用ASCII码,您的代码将不需要字母数组
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]
在您的示例中,您有
alphabet.letter
应该是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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.