对于循环不循环,无法弄清楚原因

时间:2014-04-25 15:16:45

标签: javascript loops for-loop

所以我正在做一些编程练习,而我却陷入了困境。这是一个短语中E的简单搜索。我不明白为什么for没有循环,结果只是确认它在我的短语中没有找到任何E。任何人都可以看到我的for循环有什么问题吗?

/*
 * 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();

4 个答案:

答案 0 :(得分:2)

无论如何,你都会为第一个角色返回。此外,您要报告字符串中的位置,而不是e的数量。

这应该让你开始朝着正确的方向前进:

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

我从循环中删除了return语句(导致它停止),并在循环完成后将计数报告移动到了。我还为计数创建了一个单独的变量,将eCount替换为i循环。

答案 1 :(得分:0)

你几乎就在那里。

而不是计算你只显示第一个找到的索引E.尝试下面的代码

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

答案 2 :(得分:0)

您想要完成的伪代码:

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

答案 3 :(得分:-1)

它只会检查您词组中的第一个字符。