Javascript if和for循环混淆

时间:2018-03-29 21:04:52

标签: javascript function for-loop if-statement

我正在创建一个Pig Latin翻译器,下面的这个函数检查双辅音。

一切都运行良好,直到它在函数中检查更多单词。它还将第二个字母输出为“m”。在该单词之后,该功能似乎出现故障,单词smile将第一个和第二个字母输出到控制台作为“st”。令我困惑的是数组中的最后一个字“字符串”最终输出到控制台“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);
}

2 个答案:

答案 0 :(得分:0)

第二个m来自smile,即more之后的单词。

theSent[i] == "more"时,它会循环通过辅音。当它到达m时,它与theSent[i].slice(0, 1)匹配,因此它会记录

more
1st letter being checked is: m

由于第二个字母不是辅音,因此它永远不会被记录。

然后外循环继续theSent[i] == "smile"。当它循环到consonant[j] == "m"时,这不匹配theSent[i].slice(0, 1),但它与theSent[i].slice(1, 2)匹配,因此它会记录:

2nd letter being checked is: m

在此之前你没有记录theSent[i],因此不太明显它是一个不同的词。

基本问题是你在辅音中循环,根据第一个和第二个字母检查每个辅音。这意味着您将显示最低辅音字母。如果你想知道一个字母是否是一个辅音,你就不应该有这个循环,只需用indexOf()测试该字母是否在数组中。

此外,您可以使用下标表示法来访问字符串中的特定字符,而不是.slice()

if (consonant.indexOf(theSent[i][0]) != -1) {
    console.log(theSent[i]);
    console.log("The 1st letter being checked is: " + theSent[i][0]);
} else if (consonant.indexOf(theSent[i][1]) != -1) {
    console.log(theSent[i]);
    console.log("The 2nd letter being checked is: " + theSent[i][1]);
}

&#13;
&#13;
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++) {
    if (consonant.indexOf(theSent[i][0]) != -1) {
      console.log(theSent[i]);
      console.log("The 1st letter being checked is: " + theSent[i][0]);
    } else if (consonant.indexOf(theSent[i][1]) != -1) {
      console.log(theSent[i]);
      console.log("The 2nd letter being checked is: " + theSent[i][1]);
    }
  }
  console.log(theSent);
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

所以我不确定我的问题是否正确。如果你想找到所有的双辅音(直接连续的两个相同的字母,例如“xx”“tt”“dd”):

var doubleConsonantsRegex = /([^AaEeIiOoUu\s])\1+/g;

function doubleConsonants() {
    var theSent = ["ctt", "cheers", "to", "your", "mother", "chzek", 
                   "a", "few", "more","smile", "its", "a", "string"];
    var sentence = theSent.join(' ');
    var foundOccurences = sentence.match(doubleConsonantsRegex);

    // foundOccurences: ["tt"];
}

如果您的目标是直接找到两个辅音,但它们不需要相同(例如“st”,“wt”,“xp”,“xx”),请使用不同的正则表达式:

var doubleConsonantsRegex = /([^AaEeIiOoUu\s]){2}/g; 

function doubleConsonants() {
    var theSent = ["ctt", "cheers", "to", "your", "mother", "chzek", 
                   "a", "few", "more","smile", "its", "a", "string"];
    var sentence = theSent.join(' ');
    var foundOccurences = sentence.match(doubleConsonantsRegex);

    // foundOccurences: [ "ct", "ch", "rs", "th", "ch", "sm", "ts", "st", "ng" ];
}

如果你想在彼此之后找到两个以上的辅音:

var doubleConsonantsRegex = /([^AaEeIiOoUu\s]){2,}/g; 

function doubleConsonants() {
    var theSent = ["ctt", "cheers", "to", "your", "mother", "chzek", 
                   "a", "few", "more","smile", "its", "a", "string"];
    var sentence = theSent.join(' ');
    var foundOccurences = sentence.match(doubleConsonantsRegex);

    // foundOccurences: [ "ctt", "ch", "rs", "th", "chz", "sm", "ts", "str", "ng" ]

    var allDoubleConsonants = foundOccurences.reduce(function (newArr, entry) {
        for (var i=0; i<entry.length - 1; i++) {
            newArr.push(entry[i] + entry[i+1]);
        }
        return newArr;
    }, []);

    // allDoubleConsonants: ["ct","tt","ch","rs","th","ch","hz","sm","ts","st","tr","ng"]
}

或者,如果你想写得更简洁:

var doubleConsonantsRegex = /([^AaEeIiOoUu\s]){2,}/g; 
var theSent = ["ctt", "cheers", "to", "your", "mother", "chzek", 
               "a", "few", "more","smile", "its", "a", "string"];

function doubleConsonants(input) {
    return input.join(' ')
        .match(doubleConsonantsRegex)
        .reduce(function (newArr, entry) {
            for (var i=0; i<entry.length - 1; i++) {
                newArr.push(entry[i] + entry[i+1]);
            }
            return newArr;
        }, []);
}

console.log(doubleConsonants(theSent));

Edit1 :修正了大写人声的错误。添加了两个以上辅音的示例

Edit2 :添加更简洁的示例