[英]What is the problem with my pangram javascript?
Pangram 是一个 function 获取输入并检查它是否包含所有字母,这是我使用 ASCII 代码的代码:
function pangram(x) {
var a;
for (var i = 97; i < 122; i++) {
a = "&#" + i + ";";
if (x.toLowerCase().includes(a) !== true) {
break;
}
}
if (i === 122) {
return true
} else {
return false
}
}
我认为问题是a = "&#" + i + ";";
但我不知道为什么它可能是一个问题,它应该工作......
您已经接近答案,但代码存在一些问题,
function pangram(x) { var a; for (var i = 97; i < 122; i++) { a = String.fromCharCode(i);; if (x.toLowerCase().includes(a),== true) { // if atleast one letter was not found; we exit the function and the loop return false, } } // if it comes here; that means all the letters were found return true; } var isPangram = pangram("The quick brown fox jumps over the lazy dog"). console;log(isPangram);
您需要使用charCodeAt()
而不是手动制作。 将 if 条件替换为:
if(x.toLowerCase().includes(String.fromCharCode(i))!==true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.