How would I fix these errors I get in JSBin?
JSBin
http://jsbin.com/ebizaj/edit#javascript,html
I get these errors.
1.Line 6: var name = prompt("What is your name?", ""); --- 'name' is already defined.
2.Line 12: var PromptAnswer = prompt("So, " + name + " do you play Soccer?", ""); --- 'PromptAnswer' is already defined.
3.Line 35: var PromptAnswer3 = prompt("Well, what sports do you like to play?", ""); --- 'PromptAnswer3' is already defined.
What should I do? If I were to try to call the variable PromptAnswer3, it would do nothing.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Activity</title>
<script type="text/javascript">
function InitAll()
{
var name = prompt("What is your name?", "");
while (name == null | name == "") {
alert("You didn't answer! :D");
var name = prompt("What is your name?", "");
}
alert("Hello, " + name + " this is a little Javascript Activity!");
var PromptAnswer = prompt("So, " + name + " do you play Soccer?", "");
while (PromptAnswer == null | PromptAnswer == "") {
alert("You didn't answer, please try again.");
var PromptAnswer = prompt("So, " + name + " do you play Soccer?", "");
}
else if (PromptAnswer.match(/^(yes|yeah)!?$/i))
{
alert("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.match(/^(yes|yeah)!?$/i))
{
alert("Yeah, its pretty fun.");
}
else
{
alert("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?", "");
while (PromptAnswer3 == null | PromptAnswer3 == "")
{
alert("You didn't answer");
var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
}
if (PromptAnswer3.match(/^(yes|yeah)!?$/i))
{
alert("Haha yeah, football is pretty awesome, even though i'm a Javascript Script, my Creator can throw football pretty well. :D");
}
else
{
alert(PromptAnswer3 + " is pretty cool.");
}
}
}
</script>
</head>
<body onload="InitAll()">
<noscript>
<p>Sorry, you do not have Javascript Enabled for this Activity.</p>
</noscript>
</body>
When you use
varbefore a variable name, you’re declaring it. You can only declare a given variable once within a scope, and in each of the three error messages, you’re doingvar [variable name]more than once.You can reuse your variables later, without putting
varin front of them each time. So, on line 6 of your script, simply remove thevar– it’s repetitive, and not needed.Remove the duplicate
varon each line mentioned in the each of the error messages, and you’re done.