function askQuestion(){
var Prompt = prompt("What yes or no question do you have?", "Type it here...");
var number = Math.floor((Math.random() * 8) + 1);
if(Prompt != null){
if (number == 1){
alert("Signs point yo yes.");
}else if(number == 2){
alert("Yes.");
}else if(number == 3){
alert("Reply hazy, try agian.");
}else if(number == 4){
alert("Doubtful.");
}else if(number == 5){
alert("All signs point to no.");
}else if(number == 6){
alert("Most Likely.");
}else if(number == 7){
alert("Absolutely.");
}else if(number == 8){
alert("It doesn't look good.");
}
}else{
alert("Please re-ask the Magic 8 Ball.")
}
}
<body bgColor="Black">
<center><img src="8ball.png" onClick="askQuestion()" style="cursor:pointer;"></center>
</body>
这就是我所拥有的。我想知道的是,如何看到提示中输入的文字最后有一个问号。
答案 0 :(得分:2)
如果Prompt是一个字符串,那么它应该就像
一样简单{{1}}
答案 1 :(得分:2)
if (Prompt.slice(-1) === "?") {
...
}
答案 2 :(得分:2)
var lastChar = (Prompt.trim().substr(-1) === '?')
答案 3 :(得分:0)
您可以使用charAt()方法:
var lastChar = Prompt.charAt(Prompt.length-1);
if (lastChar === "?") {
// your logic here
}