This is a javascript homework exercise. The problem is a combination of a syntax error on line 20 that looks correct to me, and the code not executing when I click button 1 in the browser. The code actually partially executed earlier until I started trying to match my curly braces. The different scenes didn’t show but the scenes numbers displayed. This is an if and else if statements in which a button is clicked to take the user to the next scene when the button is clicked. I’ve been at it for about 20 hours yesterday and today. Any suggestions? Here’s the code:
<!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>The mysterious road</title>
<script type="text/javascript">
var curScene = 0;
var option = 0;
function changeScene(option){
var message = "";
if (curScene === 0) {
curScene=1;
message = "Your journey begins at fork in the road.";
else if(curScene ==1){ //This line is the syntax error
if(option==1){
curScene = 2;
message = "You have arrived at a cute little house in the woods.";
}
else{
curScene = 3;
message = "You are standing on the bridge overlooking a peaceful stream";
}
end curScene 1
else if (curScene ==2){
if(option==1){
curScene = 4;
message = "Peeking through the window, you see a witch inside the house.";
else{
curScene = 5;
} message = "Sorry, a witch lives in the house and you just become part of her stew.";
}
}//ends curScene ==2
else if (curScene ==3){
if(option==1){
curScene =6;
message = "Sorry, a troll lives on the other side of the bridge and you just became his lunch";
else{
curScene = 7;
message = "Your stare is interupted by the arrival of a huge troll.";
}
}
}//ends curScene 3
else if (curScene ==4){
if(option==1){
curScene = 8;
else{
curScene = 5;
message = "Sorry, you became part of the witche's stew.";
}
}
}//ends curScene==4
else if (curScene ==5){
curScene = 0;
}
else if (curScene == 7){
if (option ==1){
curScene = 6;
message = "Sorry, you've become the troll's tasty lunch.";
}
else{
curScene = 9;
}
}
else if (curScene == 8){
message = "To be continued!";
}
else if (curScene == 9){
message = "To be continued!";
}
}
}//ends curScene 5
else if (curScene == 7){
curScene = 6;
if(option==1){
message = "Sorry, you became the troll's tasty lunch.";
}
}//end curScene 7
else {
curScene = 9;
}//end of curScene 7
}//end if
document.getElementById("sceneimg"). src = "scene0" + curScene + ".png;"
alert(message);
}//end of function
</script>
</head>
<body>
<div style="margin-top:100px; text-align:center">
<p><img id="sceneimg" src="sfa/scene0.png" alt="Stick Figure" /></p>
Enter here for a glorious adventure!
<input type="button" id="option" value="1" onclick="changeScene(1)" />
Enter this gate for the surpirse of your life!
<input type="button" id="option" value="2" onclick="changeScene(2)" />
</div>
</body>
</html>
Hint 1
Look at your browser’s error console for additional hints.
A syntax error means the code could not be interpreted because the parser found something it didn’t expect and can not recover from. Could you see anything the interpreter may expect but isn’t there?
Hint 2
If you can’t figure out that, double check your control characters again. Make sure things are closed where they are opened. They must be symmetrical.
Hint 3
Mouse over the block below for another hint.