I want my code to halt/stop/break where i put the return;
and i want it to halt, i have tried using break
if anyone could help me that would be appreciated, i am pretty new to Javascript so don't be upset if i dont get everything right the first time :D anyhelp would be very appreciated.
function InitAll() {
var name = prompt("What is your name?", "");
document.write("Hello, " + name + " this is a little Javascript Activity!");
var PromptAnswer = prompt("So, " + name + " do you like to play Soccer?", "");
if (PromptAnswer == "yes" || "Yes" || "Yes!" || "yes!" || "yeah" || "yeah!" || "Yeah!") {
document.write("Cool, that's awesome, i play Soccer too!");
var PromptAnswer2 = prompt("Well, even some people play Soccer and don't like it, do you like soccer?", "");
if (PromptAnswer2 == "yes" || "Yes" || "Yes!" || "yes!" || "yeah" || "yeah!" || "Yeah!") {
document.write("Yeah, its pretty fun.");
}
if (PromptAnswer2 == "") {
return;
}
else {
document.write("Well, that's really too bad, but i guess if you don't like it, you dont like it. :D");
}
}
else {
var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
if (PromptAnswer3 == "football" || "Football") {
document.write("Haha yeah, football is pretty awesome, even though i'm a Javascript Script, my Creator can throw football pretty well. :D");
}
else {
document.write(PromptAnswer3 + " is pretty cool.");
}
}
}
There are several issues in your code.
The prompt function can return null
so you probably want to change this:
if (PromptAnswer2 == "") {
return;
to this:
if (!PromptAnswer2) {
return;
}
The return
statement will finish the execution of that function, returning control back to whatever called the function.
That will check PromptAnswer2
for null
, ""
, undefined
and several other things too.
Secondly, statements like this:
if (PromptAnswer == "yes" || "Yes" || "Yes!" || "yes!" || "yeah" || "yeah!" || "Yeah!")
must be written like this:
if (PromptAnswer == "yes" || PromptAnswer == "Yes" || PromptAnswer == "Yes!" || PromptAnswer == "yes!" || PromptAnswer == "yeah" || PromptAnswer == "yeah!" || PromptAnswer == "Yeah!")
or perhaps even better to remove the different case possibilities:
PromptAnswer = PromptAnswer.toLowerCase();
if (PromptAnswer == "yes" || PromptAnswer == "yes!" || PromptAnswer == "yeah" || PromptAnswer == "yeah!")
or using regular expressions:
if (PromptAnswer.match(/^(yes|yeah)!?$/i))
Third, you may want to know that document.write()
on a fully loaded document will clear the current document and start writing a new document. It is usually not what one wants to do in event handling javascript.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.