使用 for loop,break,and continue 查找字母 "x" 在句子中出现的次数

Finding how Many time the letter "x" is appear in the sentence using for loop,break,and continue

多年未编码后,我刚刚回到 JAVASCRIPT 并尝试刷新我对如何使用 "FOR"、"BREAK"、"CONTINUE" 循环的记忆。

我想要实现的是找到一个特定字母在句子中出现了多少次 console.log 它,所以基本上

1. 我创建了一个包含我的句子

的新变量“mywords
var mywords="this is the for loop wiTh break and continue.".toLowerCase();    

2. 比变量“myletter” 比包含要查找的字母的值

var myletter ='t';    

3. 和一个变量“howmanytime” 计算字母在句子中出现的次数

var howmanytime = 0;

4. 接下来我创建了一个for循环来计算句子的长度,不断循环并将+ 1添加到“howmanytime " 使用 i++ 直到 i 等于句子长度

for (var i=0; i <= mywords.length;i++){
if (mywords[i]===myletter){
    howmanytime ++;
}    

*所以现在 howmanytime 的值是 4,我知道字母 t 在句子中出现了 4 次

5. 接下来,我在我的 for 循环中创建了 if 语句,检查该字母是否出现超过 4 次或更少,以防它出现超过 4 次显示在 console.log 句子中出现的字母数 + "more than 3" if so than break 如果小于则循环4比只显示"less than 3"

if (howmanytime >= 4) {
console.log('more than 3');
console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the senetnce');    
break;    

}else if (howmanytime <=4 ){    

console.log('less than 3');
continue;
 }    

现在的问题: 我无法弄清楚为什么当一个字母少于 4 次时会显示 "e" 在控制台日志中 "less than 3 times" 没有我想要的任何其他文本

但是当它超过 4 次时让我们回到字母 "t" 它正在显示 "more than 3" 和 "less than 3"

为什么 "less than 3" 也出现了?

我的完整代码

var mywords = "this is the for loop wiTh break and continue.".toLowerCase();

var howmanytime = 0;
var myletter = 'e';

for (var i = 0; i <= mywords.length; i++) {
  if (mywords[i] === myletter) {
    howmanytime++;
  }

  if (howmanytime >= 4) {
    console.log('more than 3');
    console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the senetnce');
    break;
  } else if (howmanytime <= 4) {
    console.log('less than 3');
    continue;
  }
}

这是一个稍微有用的代码版本,它指示正在发生的事情

var mywords = "this is the for loop wiTh break and continue.".toLowerCase();

var howmanytime = 0;
var myletter = 't';

for (var i = 0; i <= mywords.length; i++) {
  if (mywords[i] === myletter) {
    howmanytime++;
    console.log('Found ' + howmanytime + ' times the letter ' + myletter);
  }

  if (howmanytime >= 4) {
    console.log('more than 3');
    console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the sentence');
    break;
  }
}

/* NOTE! Outside of the loop */
if (howmanytime < 4) {
    console.log('less than 3');
}

I can't figurate out why when a letter is less than 4 times for example "e" it's displayed in a console log "less than 3 times" without any other text which is what I want

因为您在 ifif (howmanytime >= 4) 中显示的其他文本。对于字母 "e",它永远不会为真,因此不会记录其他文本。

but when it's more than 4 times lets go back to letter "t" it's displaying "more than 3" and "less than 3" why the "less than 3" is appearing as well?

因为最初的 howmanytime0 随着字母 "t" 出现在字符串中而继续增加。它在循环开始时不是 4 或超过 4

如果你想在最后显示全部,只需将 if 语句移出循环体。

var mywords = "this is the for loop wiTh break and continue.".toLowerCase();

var howmanytime = 0;
var myletter = 'e';

for (var i = 0; i <= mywords.length; i++) {
  if (mywords[i] === myletter) {
    howmanytime++;
    if(howmanytime >= 4) break;
  }  
}

if (howmanytime >= 4) {
    console.log('more than 3');
  }
  
else if (howmanytime <= 4) {
    console.log('less than 3');
  }
console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the senetnce');

howmanytime >= 4 将为集合 [4,5,6,7,8,9...]

true

howmanytime <= 4 将为集合 [4,3,2,1,0,-1...]

true

如果你想看看 howmanytime 是否至少 3 你应该做 howmanytime >= 3else 应该是 howmanytime < 3

请查看下面的交互示例:

function checkLetter(mywords, myletter) {
  var howmanytime = 0;
  for (var i = 0; i <= mywords.length; i++) {
    if (mywords[i] === myletter) {
      howmanytime++;
    }

    if (howmanytime >= 3) {
      break;
    } else if (howmanytime < 3) {
      continue;
    }
  }

  if (howmanytime >= 3) {
    console.log('more than 3');
    console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the senetnce');
  } else {
    console.log('less than 3');
  }
}

var mywords = "this is the for loop wiTh break and continue.".toLowerCase();

document.querySelectorAll("button").forEach(function(dom) {
  dom.addEventListener("click", function() {
    console.clear();
    checkLetter(mywords, dom.getAttribute("letter"));
  });
});
<button letter="a">Check 'A'</button>
<button letter="e">Check 'E'</button>
<button letter="t">Check 'T'</button>

一些注意事项:

  • JavaScript 的命名约定是 camel-case,这意味着您应该将变量命名为 myWords 而不是 mywords
  • else if (howmanytime < 3) 部分是不必要的。在这种情况下,一个简单的 else 可以完成相同的任务。

您可以交换条件并省略 else 部分,方法是对第一部分使用 continue,对第二个条件使用 break。

var mywords = "this is the for loop wiTh break and continueeee.".toLowerCase(),
    howmanytime = 0,
    myletter = 'e',
    i;

for (i = 0; i < mywords.length; i++) { // go only until smaller length,
    if (mywords[i] === myletter) {     // the last is length - 1
        howmanytime++;
        if (howmanytime < 3) {
            console.log('less than 3');
            continue;
        }
        if (howmanytime >= 4) {
            console.log('more than 3');
            console.log('the letter ' + myletter.toUpperCase() + ' appearing ' + howmanytime + ' times in the sentence');
            break;
        } 
    }
}