Search code examples
javascriptfor-loopbreakcontinue

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


I just returning my self to JAVASCRIPT after not coding for serval years and trying to refresh my memory on how to use "FOR", "BREAK", "CONTINUE" the loop.

What I trying to achieve is to find how many times a specific letter is appearing in the sentence and console.log it, so basically

1. I created a new variable "mywords" that contains my sentence

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

2. than a variable "myletter" than containing the value of what letter to look for

var myletter ='t';    

3. and a variable "howmanytime" that counting how many time the letter appears in the sentence

var howmanytime = 0;

4. next I created a for loop that counting the length of the sentence, keeps looping and adding + 1 to "howmanytime" using i++ until i is equal to the sentence length

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

*so now howmanytime holding a value 4 and I know that the letter t is appearing 4 times in the sentence

5. next I created if statement inside my for loop that checking if the letter appears more than 4 times or less in case it's appearing more than 4 times display in console.log the number that the letter appearing in the sentence + "more than 3" and if so than break the loop in case it's less than 4 than display only "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;
 }    

now the problem: 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

enter image description here

but when it's more than 4 times lets go back to letter "t" it's displaying "more than 3" and "less than 3"

enter image description here

why the "less than 3" is appearing as well?

My full code

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


Solution

  • 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

    Because the other text you are displaying inside the if block if (howmanytime >= 4). For the letter "e" it never gets true so the other text is not logged.

    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?

    Because the initially howmanytime is 0 goes on increasing as the letter "t" appears in string. Its not 4 or more than 4 in the start of loop.

    If you want to display that all in the end just move if statements out of loop body.

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