Dragutescu Alexandru
Dragutescu Alexandru

Reputation: 175

For loop not looping and can't figure out why

So I'm doing some programming exercises and I got stuck on this one. It's a simple search for the E's in a phrase . I don't get why the for dosen't loop, and the result just goes to the confirm that it did not find any E's in my phrase. Can anyone see anything wrong with my for loop?

/*
 * Function that counts E's in a user-enter'd phrase
 **/
function countE() {
  var phrase = prompt("Which phrase would you lke to examine ?");
  if( typeof(phrase) != "string" ) {
    alert("That is not a valid entry!");
    return false;
  }  else {
    for(var eCount = 0; eCount < phrase.length; eCount++) {
      if( phrase.charAt(eCount) == 'E' || phrase.charAt(eCount) == 'e' ) {
        alert("There are " + eCount + " E's in \"" + phrase + "\".");
        return true;
      } else {
        var report = confirm("I did not find any E's in your phrase. Would you like to try again?");
        if(report == true) {
          return countE();
        } else {
          alert("Ok maybe next time!");
          return false;
        }
      }
    }
  }
}

countE();

Upvotes: 0

Views: 4475

Answers (4)

Onur Topal
Onur Topal

Reputation: 3061

You are pretty much there.

Instead of counting you just display the index of first found E. Try below code

function countE() {

    var phrase = prompt("Which phrase would you lke to examine ?");

    if( typeof(phrase) != "string" ) {

        alert("That is not a valid entry!");

        return false;
    }  else {
        var realCountE = 0;
        var efound = false;
        for(var eCount = 0; eCount < phrase.length; eCount++) {

            if( phrase.charAt(eCount) == 'E' || phrase.charAt(eCount) == 'e' ) {
                realCountE++;
            }
        }
        if (realCountE > 0) {
           alert("There are " + realCountE + " E's in \"" + phrase + "\".");
        }
        else {
            var report = confirm("I did not find any E's in your phrase. Would you like to try again?");

            if(report == true) {

                return countE();

            } else {

                alert("Ok maybe next time!");

                return false;
            }
        }


    }
}



countE();

Upvotes: 0

Sinstein
Sinstein

Reputation: 909

Pseudocode for what you are trying to accomplish:

var count = 0
for (var eCount 0 through phrase.length)
    if( if( phrase.charAt(eCount) == 'E' || phrase.charAt(eCount) == 'e' )
        count = count + 1;    
if(count == 0)
    print (COULD NOT FIND ANY E's);
else
    print (Found <count> no of E's);

Upvotes: 0

Meng Zhang
Meng Zhang

Reputation: 337

It just checks the first character in your phrase.

Upvotes: -1

user27414
user27414

Reputation:

You're returning for the first character no matter what. Further, you're reporting the position in the string, not the number of e's.

This should get you started in the right direction:

    var eCount = 0;
    for(var i = 0; i < phrase.length; i++) {

        if( phrase.charAt(i) == 'E' || phrase.charAt(i) == 'e' ) {

            eCount++;

        }
    }

        if( eCount > 0 ) {

            alert("There are " + eCount + " E's in \"" + phrase + "\".");
            return true;

        } else {
            var report = confirm("I did not find any E's in your phrase. Would you like to try again?");

            if(report == true) {

                return countE();

            } else {

                alert("Ok maybe next time!");

                return false;
            }
        }

I removed the return statements from the loop (which were causing it to stop), and moved the report of the count to after the loop completes. I also created a separate variable for the count, replacing eCount with i for the loop.

Upvotes: 2

Related Questions