【发布时间】:2018-09-08 22:32:18
【问题描述】:
我正在创建一个 Pig Latin 翻译器,下面的这个函数检查双辅音。
一切都运行良好,直到它检查函数中的单词 more。它也将第二个字母输出为“m”。在那个词之后,函数似乎出现故障,smile 这个词将第一个和第二个字母作为“st”输出到控制台。让我感到困惑的是数组“string”中的最后一个单词最终会正确输出到控制台“st”。
如果有人可以查看我的代码并看到我没有看到的内容,我将不胜感激。我的 JSFiddle 在下面。
var vowel = ["a", "e", "i", "o", "u"];
var consonant = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"];
doubleConsonate();
function doubleConsonate() { // add the sent as a parameter
var theSent = ["ctt", "cheers", "to", "your", "mother", "chzek", "a", "few", "more", "smile", "its", "a", "string"];
console.log("the word or phrase: " + theSent);
var consonantCount;
for (var i = 0; i < theSent.length; i++) {
for (var j = 0; j < consonant.length; j++) {
if (theSent[i].slice(0, 1) == consonant[j]) {
console.log(theSent[i]);
console.log("1st letter being checked is: " + theSent[i].slice(0, 1));
} else if (theSent[i].slice(1, 2) == consonant[j]) {
// consonantCount = true;
console.log("2nd letter being checked is: " + theSent[i].slice(1, 2));
} //else if (consonantCount == true) {
//theSent[i] = theSent[i].slice(2) + theSent[i].slice(1,2) + "ay";
//consonantCount = false;
//}
}
//console.log(consonantCount);
}
console.log(theSent);
}
【问题讨论】:
-
第二个
m来自smile。
标签: javascript function for-loop if-statement