2014-04-25 141 views
0

所以我正在做一些编程练习,我被困在这一个。这是一个简单的搜索E的短语。我不明白为什么for无法循环,结果只是确认它没有在我的短语中找到任何E。任何人都可以看到我的for循环有什么问题吗?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(); 

回答

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语句,移动计数的报告,循环完成后。我还为计数创建了一个单独的变量,用循环的i替换eCount

+0

eCount状况在for循环还需要更改为我 – jing3142

+0

@ jing3142 - 谢谢 –

+0

return CountE()需要返回eCount – jing3142

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

因此,如果第一个字符不是'e',它会提示用户?这看起来不像他想要的。 –

+0

没有代码,你的代码像我的工作:) –

+0

ups,请检查更新。 http://jsfiddle.net/5qgY6/ –

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

这是什么外来语言? :) –

+0

@lostandfownd我不知道JavaScript。但是我可以在代码中看到问题。为了创建足够好的伪代码,我显然最终编写了PseudoJScriptC++。其实质是你需要一个单独的变量来存储你的计数。一旦你检查了字符串中的所有字符,只有这样你才能评论E的存在和数量。 – Sinstein