Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Hello, javascript text story problem!

Hello, ive been trying to make a text adventure game with javascript, and i have written some code, but it is not near done yet, but i have a problem, the code runs wierd, the prompt variable does not work, for expample, at the

var decision2= prompt("ROAD or CAVE");

when i type road, it says that im going inside the cabin

the whole code runs wierd, i think it has something to do with the placement of the code blocks, but i do not know how to fix it?

anyone who might know the problem?

heres the code

function Submit() {

  alert("Hello, you have choosed Story 1, we are now going on a adventure to find a diamond");
  alert("On the adventure there will come different tasks and challenge, where you have to think,     otherwise you might not find the diamond");
  alert("Okey, lets go");

  var Name = prompt("Type in your name");

  alert("Hello " + Name);

  var things = prompt("You are at a table, on the table there is 3 things, a hammer, a flower, and a shovel. Choose 1 item to bring on the adventure");

  alert("You choosed " + things);

  alert("You enter a dark forest " + " you can hear birds singing in the air, and the sound of air sweeping through the trees");

  alert("Suddenly, you arrive at a road, with 2 different ways, one to the left and one to the right " + "Choose wich way, you wanna take");

  var road = prompt("LEFT or RIGHT?");  

  if (road === "left") {
    alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in");

    alert("Far ahead of you, you see a wooden cabin");
    alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there");
    alert("You knock on the door 2 times " + "*knock* *knock*");
    alert("Noone opens the door");
    alert("Now you have to choose if you wanna go inside the cabin or continue on the road");
    var decision = prompt("ROAD or CABIN");
  } else if (road === "right") {
    alert("as you start walking on the right road, you stop hearing the sound of birds in the sky");
    alert("You walk deeper and deeper in to the forest");
    alert("You feel the cold wind blowing, it makes you shivver");

    alert("You glance with your eyes, to your right, and spots a entrance to a cave");
    alert("You must now choose if you wanna continue on the road, or go inside the cave");
    var decision2= prompt("ROAD or CAVE");
  }


    if (decision === "road") {
    alert("You continue to walk on the road");
    alert("The trees seems to disapear");
    alert("And the sun shines stronger than ever");
    alert("You hold your hands over your eyes, to avoid getting blend by the sun");
    alert("You slowly put your hands down, and as you do it, you look around");
    alert("Its a huge farm with alot of weath");
    alert("You see a man, feeding cows");
    alert("You now have to choose to walk up to the man or move on");
    var decision3 = prompt("MAN or MOVE ON");
 } else if (decision === "cabin"); {
   alert("You decide to go inside the cabin");
   alert("You extend your left arm and open the door");
   alert("As you open the old door, its starts screaking *screak* *screak*");
   alert("Inside the cabin, its all dusty. And theres a bunch of old furnitures");
   alert("You slowly walks around, in the cabin. the floor cracks as you put your weight on it");
   alert("You see a stair down to a basement");
   alert("You now have to decide if you wanna go down to the basement or continue searching the house");
   var decision4 = prompt("BASEMENT or CONTINUE");
 }



     if (decision2 === "road") {
    alert("You continue on the dark road");
    alert("It feels like the forest gets smaller, the trees are pushing up against you");
    alert("You feel afraid");
    alert("You start running forward");
    alert("The sticks from the tree hits you as you run past them");
    alert("You trip on a wooden stick on the ground");
    alert("And starts falling down a steep hill");
    alert(" *falling* *falling* ");
    alert("After a while you stop falling");
    alert("You stand up, and looks around the area");
    alert("The trees are yellow");
    alert("You wonder to yourself, *Where am i?*");
    alert("Ahead of you, you can see 2 road with different patterns");
    alert("One of the road as purple with bricks the other one is blue dirt");
    alert("Choose wich road you wanna take");
    var decision5 = prompt("PURPLE or BLUE");       
  } else if (decision2 === "CAVE") {
    alert("You walk up to the cave entrance");
    alert("You hear the sound of bats inside the cave, flying around");
    alert("You carefully enters the cave");
    alert("Its cold inside the cave");
    alert("As you walk deeper inside the cave, you start to hear the sound of water flushing nearby");
    alert("You walk around the corner");
    alert("You end up at a big open area, with a river in the middle");
    alert("You spot a boat at the edge of the river");
    alert("You walk up to the boat, and as you walk up to it, you see a man walking out from the boat");
    alert("you walk up to him");
    alert("- Hello there! says the Man");
    prompt(" *Speaking to the man* ");
    alert("What brings you down here?");
    prompt(" *speaking to the man* ");
    alert("oh i see, well i need to fix this boat, you wanna help?");
    alert("do you happen to have a hammer with you?");
  }

    if (things === "hammer") {
      alert("you give him your hammer");
      alert(" - Great! says the man");
      alert(" - Now we can fix the boat!");
      alert("The man fixes the boat with the hammer");
      alert(" *bank* *bank* ");
      alert(" - How can i thank you? says the man");
      alert("WAIT! i know, the man walks back to his boat");
      alert("He comes back with a map in his hands");
      alert(" - Here take it! its a treasure map");
      alert("Do you take the map? YES or NO");
      var decision6 = prompt("YES / NO");

    } else {

      alert("you did not bring a hammer");
      alert("I only got a " + things);
      alert(" - oh well, i guess we cant get this thing up and running then");
      alert(" - theres a exit over the bridge there! *the man points at a bridge* ");
      alert("you walk over the bridge");
      alert("the bridge moves a little bit, but you manage to go over it");
      alert("You walk out from the cave, and enters the forest again");
      alert("you find the main road again, and you walk home");        
  }






   if (decision3 === "man") {
    alert("you walk up to the man");
    alert("He turns around and looks at you.");
    alert(" - GO AWAY! get out from  my property!! ");
    alert(" - GET OOOOOOOOOOOOOOOOUT!!!!!! ");
    alert(" you start to run back where you came from");

  } else if (decision === "move on") {
    alert("You ignore walking up to the man, and you move on");
    alert("The grass is much greener and as you walk, the flowers appear by the road");

  }

}
Daniel Cunningham
Daniel Cunningham
21,109 Points

could it have anything to do with the fact that after " else if (decision === "cabin"); " is a semi-colon? without testing the code, I'd guess that placing the semi colon there separates the if statement from the block of code which could tell it to continue running.

hope that helps!

Hello, thanks for replying,

yeah by removing the semi-colon, i didnt have the problem with the cabin thing,

but know when i choose the right road first, then choose walk into the cave, it says you give him the hammer,

so it skips all the alert and prompts after "if decision2 === "cave",

can you see the problem there? :S

Daniel Cunningham
Daniel Cunningham
21,109 Points

your response is case sensitive, and "CAVE" is the only instance where it is fully upper cased. you may want to downcase all answers to make sure there are no formatting concerns. Theres also no boolean statement that would break the code if the person doesnt choose road or cave, so it makes sense that would jump to the hammer if it doesn't recognize any of the answers. You may want to force it to loop until one of the two answers is provided.

how and where should i add a boolean statement? im new to javascript

i put the "CAVE" to lowercase letters and now it works,

Daniel Cunningham
Daniel Cunningham
21,109 Points

try changing the code to a loop and create a variable like "answer = false". Then create a 'case' (or multiple elseifs) statement that would turn answer into true when either road or cave is entered... when that's true, allow it to run the next code. That's a way to force the user to input correct data into the prompts. It's a bit of a redesign to the structure of the code, but you might even be able to recycle some of the variables instead of creating a completely new decision variable for each path.

like this?

    var answer = false;

    if (decision === "road") {
    var answer = true;
    alert("You continue to walk on the road");
    alert("The trees seems to disapear");
    alert("And the sun shines stronger than ever");
    alert("You hold your hands over your eyes, to avoid getting blend by the sun");
    alert("You slowly put your hands down, and as you do it, you look around");
    alert("Its a huge farm with alot of weath");
    alert("You see a man, feeding cows");
    alert("You now have to choose to walk up to the man or move on");
    var decision3 = prompt("MAN or MOVE ON");
 } else if (decision === "cabin"); {
   var answer = false;
   alert("You decide to go inside the cabin");
   alert("You extend your left arm and open the door");
   alert("As you open the old door, its starts screaking *screak* *screak*");
   alert("Inside the cabin, its all dusty. And theres a bunch of old furnitures");
   alert("You slowly walks around, in the cabin. the floor cracks as you put your weight on it");
   alert("You see a stair down to a basement");
   alert("You now have to decide if you wanna go down to the basement or continue searching the house");
   var decision4 = prompt("BASEMENT or CONTINUE");
 }

and if the decision is road, the answer gets "true" and then i do this

if (answer = true) {
    alert("You continue on the dark road");
    alert("It feels like the forest gets smaller, the trees are pushing up against you");
    alert("You feel afraid");
    alert("You start running forward");
    alert("The sticks from the tree hits you as you run past them");
    alert("You trip on a wooden stick on the ground");
    alert("And starts falling down a steep hill");
    alert(" *falling* *falling* ");
    alert("After a while you stop falling");
    alert("You stand up, and looks around the area");
    alert("The trees are yellow");
    alert("You wonder to yourself, *Where am i?*");
    alert("Ahead of you, you can see 2 road with different patterns");
    alert("One of the road as purple with bricks the other one is blue dirt");
    alert("Choose wich road you wanna take");
    var decision5 = prompt("PURPLE or BLUE");       
  } else if (answer = false) {
    alert("You walk up to the cave entrance");
    alert("You hear the sound of bats inside the cave, flying around");
    alert("You carefully enters the cave");
    alert("Its cold inside the cave");
    alert("As you walk deeper inside the cave, you start to hear the sound of water flushing nearby");
    alert("You walk around the corner");
    alert("You end up at a big open area, with a river in the middle");
    alert("You spot a boat at the edge of the river");
    alert("You walk up to the boat, and as you walk up to it, you see a man walking out from the boat");
    alert("you walk up to him");
    alert("- Hello there! says the Man");
    prompt(" *Speaking to the man* ");
    alert("What brings you down here?");
    prompt(" *speaking to the man* ");
    alert("oh i see, well i need to fix this boat, you wanna help?");
    alert("do you happen to have a hammer with you?");

do you mean i should do like this? ive changed the if decision statements

Daniel Cunningham
Daniel Cunningham
21,109 Points

Not quite, the best thing I can do is recommend that you look into the 'loops' portion of the treehouse javascript course. Additionally, look into the concept of arrays. The logic you might want to apply is "If answer is one of the two available answers, make answer = true and continue running the story. It would essentially require putting a layer of code above each prompt to continue. <br> <br> http://teamtreehouse.com/library/javascript-loops-arrays-and-objects

oh okey, well, im currently on java basic course, but when im done with it, im gonna check out the one you linked:)

you can make the comment an answer, so i can mark it as solved if you want

Thanks for helping me with this problem:)

Sorry to bother you again, but i have a issue,

when i choose MOVE in this code

var decision3 = prompt("MAN or MOVE");

it comes to the hammer again, the same thing happens when i choose "purple" on decision5 variable :|

function Submit() {

  alert("Hello, you have choosed Story 1, we are now going on a adventure to find a diamond");
  alert("On the adventure there will come different tasks and challenge, where you have to think,     otherwise you might not find the diamond");
  alert("Okey, lets go");

  var Name = prompt("Type in your name");

  alert("Hello " + Name);

  var things = prompt("You are at a table, on the table there is 3 things, a hammer, a flower, and a shovel. Choose 1 item to bring on the adventure");

  alert("You choosed " + things);

  alert("You enter a dark forest " + " you can hear birds singing in the air, and the sound of air sweeping through the trees");

  alert("Suddenly, you arrive at a road, with 2 different ways, one to the left and one to the right " + "Choose wich way, you wanna take");

  var road = prompt("LEFT or RIGHT?");  

  if (road === "left") {
    alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in");

    alert("Far ahead of you, you see a wooden cabin");
    alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there");
    alert("You knock on the door 2 times " + "*knock* *knock*");
    alert("Noone opens the door");
    alert("Now you have to choose if you wanna go inside the cabin or continue on the road");
    var decision = prompt("ROAD or CABIN");
  } else if (road === "right") {
    alert("as you start walking on the right road, you stop hearing the sound of birds in the sky");
    alert("You walk deeper and deeper in to the forest");
    alert("You feel the cold wind blowing, it makes you shivver");

    alert("You glance with your eyes, to your right, and spots a entrance to a cave");
    alert("You must now choose if you wanna continue on the road, or go inside the cave");
    var decision2= prompt("ROAD or CAVE");
  }

 if (decision === "road") {
    alert("You continue to walk on the road");
    alert("The trees seems to disapear");
    alert("And the sun shines stronger than ever");
    alert("You hold your hands over your eyes, to avoid getting blend by the sun");
    alert("You slowly put your hands down, and as you do it, you look around");
    alert("Its a huge farm with alot of weath");
    alert("You see a man, feeding cows");
    alert("You now have to choose to walk up to the man or move on");
    var decision3 = prompt("MAN or MOVE");
 } else if (decision === "cabin") {
   alert("You decide to go inside the cabin");
   alert("You extend your left arm and open the door");
   alert("As you open the old door, its starts screaking *screak* *screak*");
   alert("Inside the cabin, its all dusty. And theres a bunch of old furnitures");
   alert("You slowly walks around, in the cabin. the floor cracks as you put your weight on it");
   alert("You see a stair down to a basement");
   alert("You now have to decide if you wanna go down to the basement or continue searching the house");
   var decision4 = prompt("BASEMENT or CONTINUE");
 }

   if (decision3 === "man") {
    alert("you walk up to the man");
    alert("He turns around and looks at you.");
    alert(" - GO AWAY! get out from  my property!! ");
    alert(" - GET OOOOOOOOOOOOOOOOUT!!!!!! ");
    alert(" you start to run back where you came from");

  } else if (decision === "move") {
    alert("You ignore walking up to the man, and you move on");
    alert("The grass is much greener and as you walk, the flowers appear by the road");
    alert("Suddenly as you are walking on the road, you notice a shop at the side of the road");
    alert("You walk up to the door at the shop");
    alert("It has a sign saying CLOSED");
    alert("You look around, and notice that the road has ended");
    alert("at the side of the road in a ditch, you notice a car");
    alert("You now have to choose to take the car or walk in the terrain");
    var decision7 = prompt("CAR or WALK");

  }


  if (decision2 === "road") {
    alert("You continue on the dark road");
    alert("It feels like the forest gets smaller, the trees are pushing up against you");
    alert("You feel afraid");
    alert("You start running forward");
    alert("The sticks from the tree hits you as you run past them");
    alert("You trip on a wooden stick on the ground");
    alert("And starts falling down a steep hill");
    alert(" *falling* *falling* ");
    alert("After a while you stop falling");
    alert("You stand up, and looks around the area");
    alert("The trees are yellow");
    alert("You wonder to yourself, *Where am i?*");
    alert("Ahead of you, you can see 2 road with different patterns");
    alert("One of the road as purple with bricks the other one is blue dirt");
    alert("Choose wich road you wanna take");
    var decision5 = prompt("PURPLE or BLUE");       
  } else if (decision2 === "cave") {
    alert("You walk up to the cave entrance");
    alert("You hear the sound of bats inside the cave, flying around");
    alert("You carefully enters the cave");
    alert("Its cold inside the cave");
    alert("As you walk deeper inside the cave, you start to hear the sound of water flushing nearby");
    alert("You walk around the corner");
    alert("You end up at a big open area, with a river in the middle");
    alert("You spot a boat at the edge of the river");
    alert("You walk up to the boat, and as you walk up to it, you see a man walking out from the boat");
    alert("you walk up to him");
    alert("- Hello there! says the Man");
    prompt(" *Speaking to the man* ");
    alert("What brings you down here?");
    prompt(" *speaking to the man* ");
    alert("oh i see, well i need to fix this boat, you wanna help?");
    alert("do you happen to have a hammer with you?");
  }

    if (things === "hammer") {
      alert("you give him your hammer");
      alert(" - Great! says the man");
      alert(" - Now we can fix the boat!");
      alert("The man fixes the boat with the hammer");
      alert(" *bank* *bank* ");
      alert(" - How can i thank you? says the man");
      alert("WAIT! i know, the man walks back to his boat");
      alert("He comes back with a map in his hands");
      alert(" - Here take it! its a treasure map");
      alert("Do you take the map? YES or NO");
      var decision6 = prompt("YES or NO");

    } else {

      alert("you did not bring a hammer" + " you brought a " + things);
      alert("I only got a " + things);
      alert(" - oh well, i guess we cant get this thing up and running then");
      alert(" - theres a exit over the bridge there! *the man points at a bridge* ");
      alert("you walk over the bridge");
      alert("the bridge moves a little bit, but you manage to go over it");
      alert("You walk out from the cave, and enters the forest again");
      alert("you find the main road again, and you walk home");        
  }

  if (decision5 === "purple") {
    alert("You start to walk on the purple road");
    alert("The trees turns into rainbow colors");
    alert("and when you walk, you hear wierd noises");
    alert("you hear a strange sound in the sky");
    alert("You look up, and spots a bird, circling over you");
    alert("its flies down to you " + "and lands on a rock next to you");
    alert(" - Hello! says the bird");
    prompt("*Speaking to the bird*");
    alert(" - Welcome to Dandorg, whats your name?");
    alert(Name);
    alert("So " + Name + " what brings you here to Dandorg? ");
    prompt("*Speaking to the bird*");
    alert("okey " + "well if you continue this road, you will end up in the middle of nowhere, and    into the shadow");
    alert("you decide to listen to the bird, and you walk home");   
  } else if (decision5 === "blue") {
    alert("you start walking on the blue road");
    alert("you stop, because the road is broken, and you cant pass it");
    alert("you walk home");

  } else {
    window.location.replace("www.google.com");
  }

  if (decision6 === "yes") {
    alert("You extend your arm and grabs the map");
    alert("you examine the map with your eyes");
    alert("you make your way out to the cave");
    alert("you look at the map, and sees that the treasure is far away from you");
    alert("you start to walk");
    alert(" * 3 hours later * ");
    alert(" you arrive at a large mountain");
    alert("The map says that the treasure is up on the mountain");
    alert("You spot a small road up to the mountain");
    alert("you walk on the road, up to the mountain");
    alert(" * 1 hour later * you arrive at the top");
    alert(" you see a rock that shines at the top");
    alert("you walk up to it");
    alert("ITS THE DIAMOND!");
    alert("you grap the diamond");
    alert("You walk down the mountain");
    alert("and you run home");

    document.write("CONGRATULATIONS!!!! YOU FOUND THE DIAMOND!");

  } else if (decision6 === "no") {
    alert(" - DONT TAKE IT THEN!! ");
    alert(" - GO AWAY! says the man");
    alert("you walk home, looking sad");
  } else {
    window.location.replace("www.google.com");
  }

}

ive looked through it 5-10 times, and i cant find the problem, maybe a couple of new eyes can spot the problem

Daniel Cunningham
Daniel Cunningham
21,109 Points

"else if (decision === "move") {" it looks like it should be decision3 rather than decision.

1 Answer

Daniel Cunningham
Daniel Cunningham
21,109 Points

It looks like you'll get the hammer story branch when selecting purple because that's how you organized the code. Definitely get beyond the Javascript Basics into the courses on loops arrays and objects. As it stands, all of your code is going to run in the order it is written because you haven't put enough conditions in to block code from running. I would suggest that all parts of your story be written in separate functions and be conditionally 'called' based on the selections. It would clean up a lot of the code and make debugging it much easier. It would also allow you to add many more branches to your story. This is a great start, but you are making more work for yourself in debugging by putting so many lines of code into the module. Good luck and keep at it! :-) <br> <br> As another reference, consider the mozilla developer network for checking on the syntax of creating Functions. <br> https://developer.mozilla.org/en-US/docs/Glossary/Function <br>

Take your 'alerts' for each story branch and put them into a function. example: for your 'left' road decision: <br> <br> function roadLeft() {

alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in"); alert("Far ahead of you, you see a wooden cabin"); alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there"); alert("You knock on the door 2 times " + "knock knock"); alert("Noone opens the door"); alert("Now you have to choose if you wanna go inside the cabin or continue on the road"); } <br> <br> Then, when you create the if statements in THIS text, you should call the function by writing "roadLeft();" <br> This would separate the many alerts from the conditional logic that you're working on creating, and it would allow you to recycle the story lines without writing excess code if you intend on having certain story lines occur regardless of what branch you take. I hope that helps!

Okey, thanks for replying,

so like this?

 function roadLeft() {
alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in");

    alert("Far ahead of you, you see a wooden cabin");
    alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there");
    alert("You knock on the door 2 times " + "*knock* *knock*");
    alert("Noone opens the door");
    alert("Now you have to choose if you wanna go inside the cabin or continue on the road");
    var decision = prompt("ROAD or CABIN");
}

function roadRight() {
alert("as you start walking on the right road, you stop hearing the sound of birds in the sky");
    alert("You walk deeper and deeper in to the forest");
    alert("You feel the cold wind blowing, it makes you shivver");

    alert("You glance with your eyes, to your right, and spots a entrance to a cave");
    alert("You must now choose if you wanna continue on the road, or go inside the cave");
    var decision2= prompt("ROAD or CAVE");
}


 if (road === "left") {
    function roadLeft();
  } else if (road === "right") {
    function roadRight();
} else {

}
Daniel Cunningham
Daniel Cunningham
21,109 Points

Exactly. That should help you keep track of your logic as you move forward. Good luck!

Okey thanks, ill fix the code, right away!

thanks for helping me with this problem!

i appreciate it

Kind regards.

// erdrag

Hey, i have changeed the entire code, so the alert is now functions, but now some things wont run in the code, when i look at the syntax highlight at workspace, the "road" variable in the if statement, is the only one who is being highlighted, the other ones do not work:/

heres the code

function Submit() {

  alert("Hello, you have choosed Story 1, we are now going on a adventure to find a diamond");
  alert("On the adventure there will come different tasks and challenge, where you have to think,     otherwise you might not find the diamond");
  alert("Okey, lets go");

  var Name = prompt("Type in your name");

  alert("Hello " + Name);

  var things = prompt("You are at a table, on the table there is 3 things, a hammer, a flower, and a shovel. Choose 1 item to bring on the adventure");

  alert("You choosed " + things);

  alert("You enter a dark forest " + " you can hear birds singing in the air, and the sound of air sweeping through the trees");

  alert("Suddenly, you arrive at a road, with 2 different ways, one to the left and one to the right " + "Choose wich way, you wanna take");

  var road = prompt("LEFT or RIGHT?");  


  function roadLeft() {
    alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in");   
    alert("Far ahead of you, you see a wooden cabin");
    alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there");
    alert("You knock on the door 2 times " + "*knock* *knock*");
    alert("Noone opens the door");
    alert("Now you have to choose if you wanna go inside the cabin or continue on the road");
    var decision = prompt("ROAD or CABIN");
  }

  function roadRight() {
    alert("as you start walking on the right road, you stop hearing the sound of birds in the sky");
    alert("You walk deeper and deeper in to the forest");
    alert("You feel the cold wind blowing, it makes you shivver");    
    alert("You glance with your eyes, to your right, and spots a entrance to a cave");
    alert("You must now choose if you wanna continue on the road, or go inside the cave");
    var decision2= prompt("ROAD or CAVE");
  }



  if (road === "left") {
    roadLeft();
  } else if (road === "right") {
    roadRight();
  } else {
    throw new Error("you did not find the diamond!"); 
  }


   function continueRoad(){
    alert("You continue to walk on the road");
    alert("The trees seems to disapear");
    alert("And the sun shines stronger than ever");
    alert("You hold your hands over your eyes, to avoid getting blend by the sun");
    alert("You slowly put your hands down, and as you do it, you look around");
    alert("Its a huge farm with alot of weath");
    alert("You see a man, feeding cows");
    alert("You now have to choose to walk up to the man or move on");
    var decision3 = prompt("MAN or MOVE");
  }

  function cabin() {
   alert("You decide to go inside the cabin");
   alert("You extend your left arm and open the door");
   alert("As you open the old door, its starts screaking *screak* *screak*");
   alert("Inside the cabin, its all dusty. And theres a bunch of old furnitures");
   alert("You slowly walks around, in the cabin. the floor cracks as you put your weight on it");
   alert("You see a stair down to a basement");
   alert("You now have to decide if you wanna go down to the basement or continue searching the house");
   var decision4 = prompt("BASEMENT or CONTINUE");
  }


   if (decision === "road") {
      continueRoad();
   } else if (decision === "cabin") {
      cabin();
   } else {
     throw new Error("you did not find the diamond!"); 
   }






   function road3() {   
    alert("You continue on the dark road");
    alert("It feels like the forest gets smaller, the trees are pushing up against you");
    alert("You feel afraid");
    alert("You start running forward");
    alert("The sticks from the tree hits you as you run past them");
    alert("You trip on a wooden stick on the ground");
    alert("And starts falling down a steep hill");
    alert(" *falling* *falling* ");
    alert("After a while you stop falling");
    alert("You stand up, and looks around the area");
    alert("The trees are yellow");
    alert("You wonder to yourself, *Where am i?*");
    alert("Ahead of you, you can see 2 road with different patterns");
    alert("One of the road as purple with bricks the other one is blue dirt");
    alert("Choose wich road you wanna take");
    var decision5 = prompt("PURPLE or BLUE"); 
  }

  function cave() {
    alert("You walk up to the cave entrance");
    alert("You hear the sound of bats inside the cave, flying around");
    alert("You carefully enters the cave");
    alert("Its cold inside the cave");
    alert("As you walk deeper inside the cave, you start to hear the sound of water flushing nearby");
    alert("You walk around the corner");
    alert("You end up at a big open area, with a river in the middle");
    alert("You spot a boat at the edge of the river");
    alert("You walk up to the boat, and as you walk up to it, you see a man walking out from the boat");
    alert("you walk up to him");
    alert("- Hello there! says the Man");
    prompt(" *Speaking to the man* ");
    alert("What brings you down here?");
    prompt(" *speaking to the man* ");
    alert("oh i see, well i need to fix this boat, you wanna help?");
    alert("do you happen to have a hammer with you?"); 
  }

   if (decision2 === "road") {
      road3();     
   } else if (decision2 === "cave") {
      cave();
   }


   function man() {
    alert("you walk up to the man");
    alert("He turns around and looks at you.");
    alert(" - GO AWAY! get out from  my property!! ");
    alert(" - GET OOOOOOOOOOOOOOOOUT!!!!!! ");
    alert(" you start to run back where you came from");
   }

   function move() {
    alert("You ignore walking up to the man, and you move on");
    alert("The grass is much greener and as you walk, the flowers appear by the road");
    alert("Suddenly as you are walking on the road, you notice a shop at the side of the road");
    alert("You walk up to the door at the shop");
    alert("It has a sign saying CLOSED");
    alert("You look around, and notice that the road has ended");
    alert("at the side of the road in a ditch, you notice a car");
    alert("You now have to choose to take the car or walk in the terrain");
    var decision7 = prompt("CAR or WALK"); 

   }



     if (decision3 === "man") {
      man();

    } else if (decision3 === "move") {
      move();

    } 



  function basement() {
   alert("You walk down the basement, as you walk down you hear the wooden steps squeeking");
   alert("You can hear rats, on the floor running.");
   alert("You look around in the basement");
   alert("And you spot a pile of dirt");
   alert("and something sticks out under it");
   alert("If you have a shovel you can dig it out");
  }

  function continues() {
    alert("You continue to search the cabin");
    alert("But you cant find anything");
    alert("You take a last glance inside the cabin");
    alert("then decides to go home, and study instead");
    throw new Error("you did not find the diamond!");
  }


    if(decision4 === "basement") {
      basement();
  } else if(decision4 === "continue") {
      continues();    
  }


  if(things === "shovel") {
    alert("you dig out the item under the pile");
    alert("its a football");
    alert("you take it and run back home, feeling happy");
    throw new Error("you did not find the diamond!");
  } else {
    alert("You brough a " + things + "you cant dig it out");
    alert("You walk home sad");
    throw new Error("you did not find the diamond!");    
  }






    if (things === "hammer") {
      alert("you give him your hammer");
      alert(" - Great! says the man");
      alert(" - Now we can fix the boat!");
      alert("The man fixes the boat with the hammer");
      alert(" *bank* *bank* ");
      alert(" - How can i thank you? says the man");
      alert("WAIT! i know, the man walks back to his boat");
      alert("He comes back with a map in his hands");
      alert(" - Here take it! its a treasure map");
      alert("Do you take the map? YES or NO");
      var decision6 = prompt("YES or NO");

    } else {

      alert("you did not bring a hammer" + " you brought a " + things);
      alert("I only got a " + things);
      alert(" - oh well, i guess we cant get this thing up and running then");
      alert(" - theres a exit over the bridge there! *the man points at a bridge* ");
      alert("you walk over the bridge");
      alert("the bridge moves a little bit, but you manage to go over it");
      alert("You walk out from the cave, and enters the forest again");
      alert("you find the main road again, and you walk home");   
      throw new Error("you did not find the diamond!");
  }


  function map() {
    alert("You extend your arm and grabs the map");
    alert("you examine the map with your eyes");
    alert("you make your way out to the cave");
    alert("you look at the map, and sees that the treasure is far away from you");
    alert("you start to walk");
    alert(" * 3 hours later * ");
    alert(" you arrive at a large mountain");
    alert("The map says that the treasure is up on the mountain");
    alert("You spot a small road up to the mountain");
    alert("you walk on the road, up to the mountain");
    alert(" * 1 hour later * you arrive at the top");
    alert(" you see a rock that shines at the top");
    alert("you walk up to it");
    alert("ITS THE DIAMOND!");
    alert("you grab the diamond");
    alert("You walk down the mountain");
    alert("and you run home");

    alert("CONGRATULATIONS!!!! YOU FOUND THE DIAMOND!"); 

  }

  function notMap() {
    alert(" - DONT TAKE IT THEN!! ");
    alert(" - GO AWAY! says the man");
    alert("you walk home, looking sad");
    throw new Error("you did not find the diamond!");
  }


  if (decision6 === "yes") {
    map();

  } else if (decision6 === "no") {
    notMap();

  } else {
    throw new Error("you did not find the diamond!");
  }




   function purple() {

    alert("You start to walk on the purple road");
    alert("The trees turns into rainbow colors");
    alert("and when you walk, you hear wierd noises");
    alert("you hear a strange sound in the sky");
    alert("You look up, and spots a bird, circling over you");
    alert("its flies down to you " + "and lands on a rock next to you");
    alert(" - Hello! says the bird");
    prompt("*Speaking to the bird*");
    alert(" - Welcome to Dandorg, whats your name?");
    alert(Name);
    alert("So " + Name + " what brings you here to Dandorg? ");
    prompt("*Speaking to the bird*");
    alert("okey " + "well if you continue this road, you will end up in the middle of nowhere, and    into the shadow");
    alert("you decide to listen to the bird, and you walk home");   
    throw new Error("you did not find the diamond!");

    }


   function blue() {
     alert("you start walking on the blue road");
     alert("you stop, because the road is broken, and you cant pass it");
     alert("you walk home");
     throw new Error("you did not find the diamond!"); 

    }

    if (decision5 === "purple") {
      purple();
  } else if (decision5 === "blue") {
      blue();

  }



}
Daniel Cunningham
Daniel Cunningham
21,109 Points

Your variable declarations are contained inside the functions. What's probably happening is an issue with "scope" in your code. You are declaring the variables within the function, but that does not mean that they will be recognized outside of the function. Your options to fix this would be either: <br>

A. Remove the variable declarations from the functions and declare them beneath the functions within the if statements (example: if (road ==="left") { <br> roadLeft(); <br> var decision = prompt("ROAD or CABIN"); <br> <br>

B. You could try to declare the variables at the beginning of the code and then set the values within the function. declare "var decision;" (and others) at the top of the code and just set the value when the decision point arrives. When you do that, they shouldnt necessarily need to leave the function, but you wont type "var" in front of them. <br> <br>

Also, I dont think you need to write "function" in front of roadLeft(); and roadRight() when you are calling them within the IF statements. (in the code if (road === "left") { function roadLeft(); } else if (road === "right") { function roadRight(); } else { )

<br> Good luck!

Hey, i did as you said, but the code still runs wierd, if you try this code out in a workspace. you will see, for example when you choose the right road first, then choose "cave" that the code will stop running, the same goes for, when you take the left road first, then go inside the cabin and choose basement. i havent checked more places this problem occurs, but i believe it happens after some questions.

function Submit() {

  alert("Hello, you have choosed Story 1, we are now going on a adventure to find a diamond");
  alert("On the adventure there will come different tasks and challenge, where you have to think,     otherwise you might not find the diamond");
  alert("Okey, lets go");

  var Name = prompt("Type in your name");

  alert("Hello " + Name);

  var things = prompt("You are at a table, on the table there is 3 things, a hammer, a flower, and a shovel. Choose 1 item to bring on the adventure");

  alert("You choosed " + things);

  alert("You enter a dark forest " + " you can hear birds singing in the air, and the sound of air sweeping through the trees");

  alert("Suddenly, you arrive at a road, with 2 different ways, one to the left and one to the right " + "Choose wich way, you wanna take");

  var road = prompt("LEFT or RIGHT?");  


  function roadLeft() {
    alert("as you start walking on the left road, the forest starts to get brighter, and you hear more animals in the background making noises, and the sun shines in");   
    alert("Far ahead of you, you see a wooden cabin");
    alert("You walk up to the doorstep of the cabin, and glance through the window to see if someone is there");
    alert("You knock on the door 2 times " + "*knock* *knock*");
    alert("Noone opens the door");
    alert("Now you have to choose if you wanna go inside the cabin or continue on the road");
  }

  function roadRight() {
    alert("as you start walking on the right road, you stop hearing the sound of birds in the sky");
    alert("You walk deeper and deeper in to the forest");
    alert("You feel the cold wind blowing, it makes you shivver");    
    alert("You glance with your eyes, to your right, and spots a entrance to a cave");
    alert("You must now choose if you wanna continue on the road, or go inside the cave");
  }



  if (road === "left") {
    roadLeft();
    var decision = prompt("ROAD or CABIN");
  } else if (road === "right") {
    roadRight();
    var decision2= prompt("ROAD or CAVE");
  } else {
    throw new Error("you did not find the diamond!"); 
  }


   function continueRoad(){
    alert("You continue to walk on the road");
    alert("The trees seems to disapear");
    alert("And the sun shines stronger than ever");
    alert("You hold your hands over your eyes, to avoid getting blend by the sun");
    alert("You slowly put your hands down, and as you do it, you look around");
    alert("Its a huge farm with alot of weath");
    alert("You see a man, feeding cows");
    alert("You now have to choose to walk up to the man or move on");
  }

  function cabin() {
   alert("You decide to go inside the cabin");
   alert("You extend your left arm and open the door");
   alert("As you open the old door, its starts screaking *screak* *screak*");
   alert("Inside the cabin, its all dusty. And theres a bunch of old furnitures");
   alert("You slowly walks around, in the cabin. the floor cracks as you put your weight on it");
   alert("You see a stair down to a basement");
   alert("You now have to decide if you wanna go down to the basement or continue searching the house");
  }


   if (decision === "road") {
      continueRoad();
     var decision3 = prompt("MAN or MOVE");
   } else if (decision === "cabin") {
      cabin();
     var decision4 = prompt("BASEMENT or CONTINUE");
   } else {
     throw new Error("you did not find the diamond!"); 
   }






   function road3() {   
    alert("You continue on the dark road");
    alert("It feels like the forest gets smaller, the trees are pushing up against you");
    alert("You feel afraid");
    alert("You start running forward");
    alert("The sticks from the tree hits you as you run past them");
    alert("You trip on a wooden stick on the ground");
    alert("And starts falling down a steep hill");
    alert(" *falling* *falling* ");
    alert("After a while you stop falling");
    alert("You stand up, and looks around the area");
    alert("The trees are yellow");
    alert("You wonder to yourself, *Where am i?*");
    alert("Ahead of you, you can see 2 road with different patterns");
    alert("One of the road as purple with bricks the other one is blue dirt");
    alert("Choose wich road you wanna take");
  }

  function cave() {
    alert("You walk up to the cave entrance");
    alert("You hear the sound of bats inside the cave, flying around");
    alert("You carefully enters the cave");
    alert("Its cold inside the cave");
    alert("As you walk deeper inside the cave, you start to hear the sound of water flushing nearby");
    alert("You walk around the corner");
    alert("You end up at a big open area, with a river in the middle");
    alert("You spot a boat at the edge of the river");
    alert("You walk up to the boat, and as you walk up to it, you see a man walking out from the boat");
    alert("you walk up to him");
    alert("- Hello there! says the Man");
    prompt(" *Speaking to the man* ");
    alert("What brings you down here?");
    prompt(" *speaking to the man* ");
    alert("oh i see, well i need to fix this boat, you wanna help?");
    alert("do you happen to have a hammer with you?"); 
  }

   if (decision2 === "road") {
      road3();     
     var decision5 = prompt("PURPLE or BLUE"); 
   } else if (decision2 === "cave") {
      cave();
   } else {
     throw new Error("you did not find the diamond!"); 
   }


   function man() {
    alert("you walk up to the man");
    alert("He turns around and looks at you.");
    alert(" - GO AWAY! get out from  my property!! ");
    alert(" - GET OOOOOOOOOOOOOOOOUT!!!!!! ");
    alert(" you start to run back where you came from");
   }

   function move() {
    alert("You ignore walking up to the man, and you move on");
    alert("The grass is much greener and as you walk, the flowers appear by the road");
    alert("Suddenly as you are walking on the road, you notice a shop at the side of the road");
    alert("You walk up to the door at the shop");
    alert("It has a sign saying CLOSED");
    alert("You look around, and notice that the road has ended");
    alert("at the side of the road in a ditch, you notice a car");
    alert("You now have to choose to take the car or walk in the terrain"); 

   }



     if (decision3 === "man") {
      man();

    } else if (decision3 === "move") {
      move();
      var decision7 = prompt("CAR or WALK");
    } 



  function basement() {
   alert("You walk down the basement, as you walk down you hear the wooden steps squeeking");
   alert("You can hear rats, on the floor running.");
   alert("You look around in the basement");
   alert("And you spot a pile of dirt");
   alert("and something sticks out under it");
   alert("If you have a shovel you can dig it out");
  }

  function continues() {
    alert("You continue to search the cabin");
    alert("But you cant find anything");
    alert("You take a last glance inside the cabin");
    alert("then decides to go home, and study instead");
    throw new Error("you did not find the diamond!");
  }


    if(decision4 === "basement") {
      basement();
  } else if(decision4 === "continue") {
      continues();    
  }


  if(things === "shovel") {
    alert("you dig out the item under the pile");
    alert("its a football");
    alert("you take it and run back home, feeling happy");
    throw new Error("you did not find the diamond!");
  } else {
    alert("You brough a " + things + "you cant dig it out");
    alert("You walk home sad");
    throw new Error("you did not find the diamond!");    
  }






    if (things === "hammer") {
      alert("you give him your hammer");
      alert(" - Great! says the man");
      alert(" - Now we can fix the boat!");
      alert("The man fixes the boat with the hammer");
      alert(" *bank* *bank* ");
      alert(" - How can i thank you? says the man");
      alert("WAIT! i know, the man walks back to his boat");
      alert("He comes back with a map in his hands");
      alert(" - Here take it! its a treasure map");
      alert("Do you take the map? YES or NO");
      var decision6 = prompt("YES or NO");

    } else {

      alert("you did not bring a hammer" + " you brought a " + things);
      alert("I only got a " + things);
      alert(" - oh well, i guess we cant get this thing up and running then");
      alert(" - theres a exit over the bridge there! *the man points at a bridge* ");
      alert("you walk over the bridge");
      alert("the bridge moves a little bit, but you manage to go over it");
      alert("You walk out from the cave, and enters the forest again");
      alert("you find the main road again, and you walk home");   
      throw new Error("you did not find the diamond!");
  }


  function map() {
    alert("You extend your arm and grabs the map");
    alert("you examine the map with your eyes");
    alert("you make your way out to the cave");
    alert("you look at the map, and sees that the treasure is far away from you");
    alert("you start to walk");
    alert(" * 3 hours later * ");
    alert(" you arrive at a large mountain");
    alert("The map says that the treasure is up on the mountain");
    alert("You spot a small road up to the mountain");
    alert("you walk on the road, up to the mountain");
    alert(" * 1 hour later * you arrive at the top");
    alert(" you see a rock that shines at the top");
    alert("you walk up to it");
    alert("ITS THE DIAMOND!");
    alert("you grab the diamond");
    alert("You walk down the mountain");
    alert("and you run home");

    alert("CONGRATULATIONS!!!! YOU FOUND THE DIAMOND!"); 

  }

  function notMap() {
    alert(" - DONT TAKE IT THEN!! ");
    alert(" - GO AWAY! says the man");
    alert("you walk home, looking sad");
    throw new Error("you did not find the diamond!");
  }


  if (decision6 === "yes") {
    map();

  } else if (decision6 === "no") {
    notMap();

  } else {
    throw new Error("you did not find the diamond!");
  }




   function purple() {

    alert("You start to walk on the purple road");
    alert("The trees turns into rainbow colors");
    alert("and when you walk, you hear wierd noises");
    alert("you hear a strange sound in the sky");
    alert("You look up, and spots a bird, circling over you");
    alert("its flies down to you " + "and lands on a rock next to you");
    alert(" - Hello! says the bird");
    prompt("*Speaking to the bird*");
    alert(" - Welcome to Dandorg, whats your name?");
    alert(Name);
    alert("So " + Name + " what brings you here to Dandorg? ");
    prompt("*Speaking to the bird*");
    alert("okey " + "well if you continue this road, you will end up in the middle of nowhere, and    into the shadow");
    alert("you decide to listen to the bird, and you walk home");   
    throw new Error("you did not find the diamond!");

    }


   function blue() {
     alert("you start walking on the blue road");
     alert("you stop, because the road is broken, and you cant pass it");
     alert("you walk home");
     throw new Error("you did not find the diamond!"); 

    }

    if (decision5 === "purple") {
      purple();
  } else if (decision5 === "blue") {
      blue();

  }



}

7