簡體   English   中英

For循環未循環,無法弄清原因

[英]For loop not looping and can't figure out why

所以我正在做一些編程練習,但我被困在了這一練習上。 這是一個短語中的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();

無論如何,您都將返回第一個角色。 此外,您要報告字符串中的位置,而不是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進行循環。

你幾乎在那里。

您無需顯示計數,只需顯示第一個找到的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();

您要完成的偽代碼:

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

它只是檢查您的短語中的第一個字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM