我有一個關於其結論,因爲這一個codeschool.com功能鍛鍊的問題...的JavaScript的typeof
function countE() {
var phrase = prompt("Which phrase would you like to examine?");
if (typeof(phrase) != "string") {
alert("This is not a valid entry!");
return false;
} else {
var eCount = 0;
for (var i = 0; i < phrase.length; i++) {
if (phrase.charAt(i) === 'e' || phrase.charAt(i) === 'E') {
eCount++;
}
}
alert(eCount);
return true;
}
}
countE()
所以..我想考的是不是一個字符串,我希望得到警報「這不是有效的條目!「。
但是,如果一個提示只返回一個字符串,那麼爲什麼這個 < <如果(typeof(phrase)!=「string」)>>包含在函數中?
對不起,在這裏提出這個基本問題,codeschool討論頁沒有給我一個答案,我很想知道。
謝謝。 J
嘗試按該提示中止。認爲只是檢查它是否爲空會更簡單。 PS:typeof不需要() – GottZ
@Tushar不會不會 – PeeHaa
這個問題告訴我們寫評論有多重要。 –