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

Problem with JavaScript text game!

Hello, i have tried to make a JavaScript text game, from what i know. and i have run into some problems on the way. the code is running wierd, for example in this code

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");
    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");
   decision4 = prompt("BASEMENT or CONTINUE");
  }


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

When i choose cabin, the code just stops executing, and i have no idea why. and the same problems occurs on other places in the game too.

heres the full 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?");  
  var decision;
  var decision2;
  var decision3;
  var decision4;
  var decision5;
  var decision6;
  var decision7;


  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");
    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");
    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");
    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");
   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");
    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");
    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");
      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();

  }



}

Any help is very appreciated, thanks! :) Kind Regards.

// erdrag

Hi Erdrag,

I pasted in your original code here and I was able to make all the way to the point where an error was thrown and I didn't find the diamond.

I had to make sure though that I was always entering commands in all lowercase letters because this is what the conditionals in your code are expecting.

When you're testing yourself and it terminates too early are you by chance not entering all lowercase letters for commands?

jason phillips answer will solve that problem for you but you need to add the .toLowerCase() method to all of your prompt calls.

Hello, this is my code when i added the lowercase method, but after i have choosed "Right" at the first question, and then when i have to choose between "ROAD OR CAVE" (function "road3" and "cave" none of them works, they just stop 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?").toLowerCase();  
  var decision;
  var decision2;
  var decision3;
  var decision4;
  var decision5;
  var decision6;
  var decision7;


  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");
    decision = prompt("ROAD or CABIN").toLowerCase();
  }

  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");
    decision2 = prompt("ROAD or CAVE").toLowerCase();
  }



  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");
    decision3 = prompt("MAN or MOVE").toLowerCase();
  }

  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");
   decision4 = prompt("BASEMENT or CONTINUE").toLowerCase();
  }


   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");
    decision5 = prompt("PURPLE or BLUE").toLowerCase(); 
  }

  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");
    decision7 = prompt("CAR or WALK").toLowerCase(); 

   }



     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");
      decision6 = prompt("YES or NO").toLowerCase();

    } 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();

  }



}

Jason Anello you are correct I should have mentioned road3, this was a rename because there are 2 functions named road and wouldn't work with the switch, road3 will never get called and aside from that, that case will never get called because it is the same as the case before it. Basically this was a place to start. I also mentioned that it looked like there was still some work to do.

3 Answers

Hi Erdrag,

I think I've figured out the problem with your code.

I'll trace through what's happening after 'LEFT' or 'RIGHT' but it's a general problem throughout the code.

When you come out of either the roadLeft() or roadRight() function the next code that gets executed is this:

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

The problem is that suppose you had gone right. This means the variable decision will be undefined in the code above because it gets set in the roadLeft() function which never ran.

Since decision is undefined you will end up executing the else clause and your program ends prematurely.

So you want to make sure that code only runs if you had gone left.

Probably the least obtrusive way to fix your code would be to wrap that code in an if block where you check if the variable is not equal to undefined

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

That way this code will be skipped if you had gone right. You'll need to go through your code and put this on all the if/else if/else blocks being sure to change the variable name.

Give us an update on how that turns out.

The first question, where i choose to go "Left" all the code after that works fine, but when i choose "Right" at the first question, and the come up to the question where i have to choose "Road" or "cave" and when i choose one, the code stops running, thats wierd, because all the code works when i choose left at the beginning of the game. but not right.

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");
    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();
   }

So this might help a little: is there a reason the keep history of the users choice? It might be easier to track down the errors if you overwrite decision every time you get a response from the user. So then we can have:

var decision;
...

decision = prompt().toLowerCase();

if(decision ==="one") {
  one();
} else if(decision ==="two") {
  two();
} else {
  console.log(decision); //so you can see the response
  doElse();
}

The thing is, that i dont understand why all the code works when i choose left in the beginning, but after i choosed "right" and then cave or road, the code stops executing.

can you try the code in a workspace or anything, so you can see what i mean.

ok I think I know specifically why this block is not working:

in the function roadLeft you set a variable called decision and in roadRight you set decision2

so the problem is if you choose right, decision does not get set at all. Later in the code you do a check for decision which is undefined:

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");
    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");
    decision2 = prompt("ROAD or CAVE");
  }

...

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

This is what I was talking about earlier. If you just define one variable for the decision and just reset it every time you ask the user for prompt, then you only ever need to check the value of decision.

Oh i see, but how can i fix this?:/

and this is the block that stops the whole code

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");
    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();
   }
var decision;

...

decision = prompt("ROAD or CABIN").toLowerCase();

...

decision = prompt("ROAD or CAVE").toLowerCase();

...

decision = prompt("MAN or MOVE").toLowerCase();

...

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

...

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

This is my full code, So if i get this right, should i change evrey variable to "decision" instead of making every variable have a different name?

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?").toLowerCase();  
  var decision;
  var decision2;
  var decision3;
  var decision4;
  var decision5;
  var decision6;
  var decision7;


  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");
    decision = prompt("ROAD or CABIN").toLowerCase();
  }

  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");
    decision2 = prompt("ROAD or CAVE").toLowerCase();
  }



  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");
    decision3 = prompt("MAN or MOVE").toLowerCase();
  }

  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");
   decision4 = prompt("BASEMENT or CONTINUE").toLowerCase();
  }


   if (decision === "road") {
      continueRoad();
   } else if (decision === "cabin") {
      cabin();
   } else {
     console.log(decision);
     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");
    decision5 = prompt("PURPLE or BLUE").toLowerCase(); 
  }

  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");
    decision7 = prompt("CAR or WALK").toLowerCase(); 

   }



     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");
      decision6 = prompt("YES or NO").toLowerCase();

    } 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();

  }



}

Yes. Also, what might help in the long run, is to have a function that does all the stepping. So instead of having this:

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

multiple times throughout your code, something like this:

function step() {
  switch(decision) {
    case 'road' :
      road();
      break;
    case 'cave':
      cave();
      break;
    case 'cabin':
      cabin();
      break;
    default:
      console.log('error');
      break;
  }
}

then you can replace anywhere you have:

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

with this

step();

Hello, i changed all my variables to "decision" but when i choose cave, it still wont run, and now when i choose "road" instead of "cave" it runs the question if i had chossed Road when i was at the cabin,

It looks like you still have some work to do, but this is what i have so far. Couple of things, I think that it makes more sense to call the step function at the end of each of the decision functions, but you need to make sure that you are setting decision again so that there is something to do. Not all these set the decision variable.

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 decision = 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");
    decision = prompt("ROAD or CABIN").toLowerCase();
  }

  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");
    decision = prompt("ROAD or CAVE").toLowerCase();
  }



step();


   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");
    decision = prompt("MAN or MOVE").toLowerCase();
  }

  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");
   decision = prompt("BASEMENT or CONTINUE").toLowerCase();
  }


step();






   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");
    decision = prompt("PURPLE or BLUE").toLowerCase(); 
  }

  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?"); 
  }

step();


   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");
    decision = prompt("CAR or WALK").toLowerCase(); 

   }



step();



  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!");
  }


step();


  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");
      decision = prompt("YES or NO").toLowerCase();

    } 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!");
}

  step();

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!"); 

} 

step();


function step() {

  switch(decision) {
    case 'left':
      roadLeft();
      break;
    case 'right':
      roadRight();
      break;
    case 'purple':
      purple();
      break;
    case 'blue':
      blue();
      break;
    case 'yes':
      map();
      break;
    case 'no':
      notMap();
    case 'road':
      continueRoad();
      break;
    case 'road':
      road3();
      break;
    case 'cave':
      cave();
      break;
    case 'cabin':
      cabin();
      break;
    case 'man':
      man();
      break;
    case 'move':
      move();
      break;
    case 'basement':
      basement();
      break;
    case 'continue':
      continues();
      break;
  }

}

Oh okey, now it works, thanks for helping.

one thing i wonder, is how the code for the switching decision works?, i havent learned about that yet. can you explain that? if you have time ofcourse, I have already asked so much:)

OK so you have decision declared and it gets set in each of these decision functions(left, right, cave, etc.)

The step function is doing a switch bases on the value of the variable. I often use a switch instead of chaining if else if else. I think it is cleaner and easier to read this way, especially when you have so many cases.

Okey, thanks for explaining. Yeah it is a bit cleaner than the way i did it, it was so much that i lost track of everything, i hope Treehouse teaches about the switching abit more so i can learn it fully, because it seems very useful.

Thanks for cleaning my code up, it was necessary :D

And my apologies if it was alot of questions.

Thanks.

Kind Regards // erdrag

Erdrag,

Did you try out my suggestion in my answer? I didn't see any mention of it.

The program really should be refactored because there's a lot of repetition here and easy for something to go wrong.

Are you positive that it's working right now? I'm not sure that it is with that switch statement.

jason phillips ,

I'm not sure how the switch statement fixed it. There are a few commands in this game that can be entered more than once depending on where you're at and a different function should be called. "road" is one of them.

You have these case statements:

case 'road':
      continueRoad();
      break;
    case 'road':
      road3();
      break;

I'm not seeing how the road3() function could ever be called with the break statements in place. And if you remove the first break then wouldn't both functions be called anytime the road command was issued?

Also, I think there's no guard against entering a different command from the 2 choices given and being able to instantly jump to another part of the story.

I might be wrong though.

Erdrag, see if you can get to the point in the story where the road3() function would be called.

From what I can see you rely on the user to type the responses exactly the same as your check in your code. I would suggest if you are using strings to compare you should change them to a lower case instead.

"Test"==="test" //this will return false

so what you can do is :

decision = prompt("ROAD or CABIN").toLowerCase();
if(decision=="road") {}
else if(decision === "cabin"){}

Hey, the cave questions works now, but this block of code does not work, when you choose right at the first question and then choose "ROAD or CAVE" non of them works, the code stops running

  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");
    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();
   }

can you see the problem? :/

Do the same thing as before. Make sure you are setting the value to lower case so you can test against it properly:

 decision = prompt("ROAD or CABIN").toLowerCase();
if(decision === "road") {} else if(decision === "cabin") {}

Another thing to mention is that the browsers have a developer console for javascript, that should tell you if and where you have errors.

Hello thanks for answering, in the originial code i posted, the program worked fine if i choosed left at the beginning, but if i choosed right, and "ROAD or CAVE" after, the code just stopped working. And i still dont know why, in my view it looked like it was supposed to work,

But the switch statement fixed the issue, and i can now choose Road or Cave when i choose right. Yeah there were a problem with the 2 road keywords, when i choosed "Road" at the "ROAD or CAVE" question, it displayed as i had choosed Left in the beginning, I fixed it by changing the keyword.

Jason Anello if you have any idea to fix the original code, so that the code works on both direction, please tell me, because i would like the original code as it was, because it was alot easier to understand without the "Switch" statement, due to that i have not learned about it yet.

Thanks!

Kind Regards.

Erdrag

Hi Erdrag,

Please review my answer where I explain why the direction "right" didn't work. And I gave a code sample for what I think will work. I only tested it for the earlier part of your story and it seems to work.

What I didn't explain I think is why it worked when you went left.

Not all of your if/else if blocks have an else clause where you throw an error. This inconsistent code leads to inconsistent results when running the program. I assumed you put those in there to handle the case of an invalid choice. It would cause the program to end early since you don't have a way to handle bad choices yet.

Going over both possibilities (left or right) -

Right -

If you went right (didn't work) then the variable decision is undefined because you never run the goLeft() function.

You reach this code:

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

Since decision is undefined, you execute the else block and this stops your program prematurely.

Left-

If you went left instead then the variable decision2 is undefined because you never run the goRight() function. This should lead to the exact same problem.

However, you reach this code:

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

Notice that this one does not have an else clause throwing an error. Since decision2 is undefined, you end up falling through this code and you continue to execute the code afterwards as if nothing went wrong.

The solution I tried to show in my answer was to basically not run these code blocks if the corresponding decision variable is undefined. Wrap each one in an if condition that checks it.

Examples:

if (decision !== undefined) {
  // only run this code if the decision variable has been defined
  if (decision === "road") {
      continueRoad();
   } else if (decision === "cabin") {
      cabin();
   } else {
     throw new Error("you did not find the diamond!"); 
   }
}

if (decision2 !== undefined) {
  // only run this code if the decision2 variable has been defined
  if (decision2 === "road") {
      road3();     
   } else if (decision2 === "cave") {
      cave();
   }
}

Also, you'll probably want to have consistency on the else clause and make sure you put them in everywhere.

My solution is not good in the sense that it continues to make your program more repetitive and harder to maintain. However, I think it's the minimum amount of code needed to fix the program without changing it too much.

Hey, i changed my original code, to the suggestion you commented, like this, but the code runs wierd,

for example, if i choose "right" in the beginning then choose "Road" then choose "Purple" then a dialog appears " here it says i cant dig it out" that shouldnt be displayed there,

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?");  
  var decision;
  var decision2;
  var decision3;
  var decision4;
  var decision5;
  var decision6;
  var decision7;


  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");
    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");
    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");
    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");
   decision4 = prompt("BASEMENT or CONTINUE");
  }


  if (decision !== undefined) {
  // only run this code if the decision variable has been defined
  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");
    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 !== undefined) {
  // only run this code if the decision2 variable has been defined
  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");
    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");
      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();

  }