我是JavaScript的新手,並試圖運行js文件。我的問題是我試圖在var「luckyplace」中保存randomItem()返回值並檢查if-else條件。當我運行該文件時,它沒有給出任何條件警報,我也沒有看到任何錯誤。請幫忙 !與JS問題,如果條件警報
/*jslint devel: true */
alert("You are away from home long time, and you forgot where you kept the key. You have to search few places to find the key and get in !");
var places = ["flower pot", "under mat", "side of window", "top of front door"];
alert("place you can search are listed below:" + " " + places);
function randomItem(range) {
"use strict";
return Math.round(Math.random() * range);
}
alert(places[randomItem(places.length - 1)]);
var luckyplace = randomItem();
if (luckyplace === "flower pot") {
alert("No key, no flowers !");
} else if (randomItem() === "under mat") {
alert("Try somewhere else.");
} else if (randomItem() === "side of window") {
alert("you are not lucky enough");
} else if (randomItem() === "top of front door") {
alert("great ! you found the key. Unlock the door.");
}
在此先感謝...
你隨機項函數返回數組 –