【问题标题】:Javascript if and for loop confusionJavascript if 和 for 循环混淆
【发布时间】: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


【解决方案1】:

第二个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]);
}

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);
}

【讨论】:

  • 非常感谢!你真的超出了我的预期!我也学到了一两件事!
【解决方案2】:

所以我不完全确定你的问题是否正确。如果要查找所有双辅音(两个相同的字母直接连续,例如“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:添加更简洁的示例

【讨论】:

  • 第二个是我要找的那个。谢谢!!
  • 不客气,很高兴我能帮上忙!如果这是您需要的,您能接受答案吗?干杯!
  • @JesseM 我更新了我的答案,因为我注意到了一个错误:正则表达式没有找到大写人声。此外,如果您还想找到像 "ctt" 这样的字符序列,我添加了另一个示例作为 ["ctt"]["ct", "tt"] 而不仅仅是 ["ct"] // omitting the "tt"
猜你喜欢
  • 2016-06-16
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多