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 😀 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
nullso you probably want to change this:to this:
The
returnstatement will finish the execution of that function, returning control back to whatever called the function.That will check
PromptAnswer2fornull,"",undefinedand several other things too.Secondly, statements like this:
must be written like this:
or perhaps even better to remove the different case possibilities:
or using regular expressions:
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.