Choose Your Own Adventure (redo)

Advertisements

I previously made a post about my issue on another account but promptly found out that I formatted the question incorrectly and did not provide enough information. In an attempt to "show the minimum amount of code" I excluded some code. I have now included more code. So I am here to do it correctly this time. I am new to both coding and to stackoverflow so my first attempt at a question was not very good.

I was assigned to do a "Choose Your Own Adventure" game using Javascript. I have two characters you are prompted to choose from at the beginning of the game. "SORA" and "KRATOS". For some reason, my "KRATOS" is not working.

When the player enters "KRATOS" they should then receive a new prompt that allows them to continue. This prompt is not showing up. However, when entering "SORA" the game works as intended.

My goal here is for players to be able to enter "KRATOS" and be able to continue on his adventure.

I believe I may have messed up with the IF statements but I am unsure. Thank you in advance for any help.

function startGame() {
  var game = prompt("Choose your character (KRATOS/SORA)");
  document.getElementById("answer").innerHTML = game;


  function SORA() {
    var game_s = prompt("You have chosen SORA! The wielder of the Keyblade! You must fight the mighty KRATOS! The god of war! How will you prepare? (TRAIN/ADVENTURE)");
    if (game_s == "TRAIN") {
      SORA_TRAIN();
    }

    if (game_s == "ADVENTURE") {
      SORA_ADVENTURE();
    }
  }

  function SORA_TRAIN() {
    var game_2_1 = prompt("You have chosen TRAIN! You train with your friends Donald and Goofy! You have become stronger in the ways of combat! The battle draws near! You feel prepared and confident for the fight. You may take one partner with you. Who do you bring? (DONALD/GOOFY) ");
    if (game_2_1 == "DONALD") {
      SORA_DONALD();
    }
    if (game_2_1 == "GOOFY"); {
      SORA_GOOFY();
    }

  }

  function SORA_DONALD() {
    alert("You have brought Donald! A powerful magician! He and you face Kratos on the battlefield. Donald casts an invincibility spell on you and you easily defeat Kratos! The end.");
  }

  function SORA_GOOFY() {
    alert("You have brought Goofy! A powerful warrior! He and you face Kratos on the battlefield. Goofy gets knocked out by Kratos in one blow! However, your training has kept you nimble! You dodge Kratos and all his attacks! You defeat Kratos! The end.");
  }

  function SORA_ADVENTURE() {
    var game_2_2 = prompt("You have chosen ADVENTURE! You adventure with your friends Mickey and Minnie! Through your exploration you have become stronger and wiser! You feel prepared and confident for the fight! You may take one partner with you. Who do you bring? (MICKEY/MINNIE)");

    if (game_2_2 == "MICKEY") {
      SORA_MICKEY();
    }
    if (game_2_2 == "MINNIE") {
      SORA_MINNIE();
    }


  }

  function SORA_MICKEY() {
    alert("You have brought Mickey! The King and fellow keyblade warrior! He and you face Kratos on the battlefield. Mickey and you rush to face Kratos and you easily overpower Kratos! The end.");
  }

  function SORA_MINNIE() {
    alert("You have brought Minnie! The powerful Queen and magician! She and you face Kratos on the battlefield. Minnie casts an invincibility spell on you and you easily defeat Kratos! The end.");
  }


  function KRATOS_1() {
    var game_3 = prompt("You have chosen KRATOS! The god of war! You must fight Sora! The legendary Keyblade wielder! How will you prepare? (HUNT/WAIT)");

    if (game_3 == "HUNT") {
      KRATOS_HUNT();
    }
    if (game_3 == "WAIT") {
      KRATOS_WAIT();
    }
  }

  function KRATOS_WAIT() {
    var game_4_1 = prompt("You have chosen WAIT! As the god of war you fear no mortal! The fight draws near. What weapon will you take into battle? (BOW/HAMMER)")

    if (game_4_1 == "BOW") {
      KRATOS_BOW();
    }

    if (game_4_1 == "HAMMER") {
      KRATOS_HAMMER();
    }

  }

  function KRATOS_BOW() {
    alert("You have chosen BOW! You bring your BOW to the battlefield. You pull back your string and let your arrow fly! You knock SORA's blade from his hand and he is unable to fight! You win! The end.")
  }

  function KRATOS_HAMMER() {
    alert("You have chosen HAMMER! You bring your HAMMER to the battlefield. You swing back your HAMMER and slam it to the ground making a tremendous earthquake. Your opponent cannot stand up! You win! The end.")
  }

  function KRATOS_HUNT() {
    var game_3_1 = prompt("You have chosen HUNT! You venture out into the forests to fight ferocious beasts to prepare! You feel that you have improved your strength. The battle draws near! Which weapon will you take to battle? (AXE/BLADES) ")

    if (game_3_1 == "AXE") {
      KRATOS_AXE();
    }

    if (game_3_1 == "BLADES") {
      KRATOS_BLADES();
    }

  }

  function KRATOS_AXE() {
    alert("You have chosen AXE! You enter the battlefield wielding your mighty AXE forged by the norse gods! You throw your weapon and are able to knock SORA's Keyblade from his grip! With no way for your opponent to fight. You win! The end.")
  }

  function KRATOS_BLADES() {
    alert("You have chosen BLADES! You enter the battlefield wielding your mighty BLADES forged by the greek gods! You swing at SORA but he is too quick! He is able to get behind you and knock you unconscious! You lose! The end. ")
  }

  if (game == "SORA") {
    SORA();
  }
  if (game_2 == "TRAIN") {
    SORA_TRAIN();
  }
  if (game_2 == "ADVENTURE") {
    SORA_ADVENTURE();
  }
  if (game_2_2 == "MICKEY") {
    SORA_MICKEY();
  }
  if (game_2_2 == "MINNIE") {
    SORA_MINNIE();
  }
  if (game_2_1 == "DONALD") {
    SORA_DONALD();
  }
  if (game_2_1 == "GOOFY"); {
    SORA_GOOFY();
  }

  if (game == "KRATOS") {
    KRATOS_1()
  }
  if (game_3 == "HUNT") {
    KRATOS_HUNT();
  }
  if (game_3 == "WAIT") {
    KRATOS_WAIT();
  }
  if (game_4_1 == "BOW") {
    KRATOS_BOW();
  }
  if (game_4_1 == "HAMMER") {
    KRATOS_HAMMER();
  }
  if (game_3_1 == "AXE") {
    KRATOS_AXE();
  }
  if (game_3_1 == "BLADES") {
    KRATOS_BLADES();
  }


}
<div id="Adventure Game">
  <button onclick="startGame()">Start Game</button>
  <p id="answer"></p>
</div>

I received some suggestions from my previous post and put them into action:

Line 2 and 6 both began with var games = and were changed. – Ghoul Fool

Changed variables in SORA to game_s for better organization. – Bogger

>Solution :

you are trying to use a variable (game_2) that you have not declared, and when the code hits the if statement using it, it breaks and "throws" an error message that you can read in the console log. the line of code is:

if (game_2 == "TRAIN") {

In other languages, a bug like this might be caught during compile time (it will not allow the program to begin at all), but JavaScript is an interpreted language, this bug is caught only when that line of code is executing while running (I think this blog post does a good job of explaining JavaScript’s quirks as an interpreted language).

This is why when you input "SORA", the buggy line of code is never hit, since the code instead calls SORA() from inside your if statement, and SORA() waits for your response for the line of code:

var game_s = prompt("You have chosen SORA! The wielder of the Keyblade! You must fight the mighty KRATOS! The god of war! How will you prepare? (TRAIN/ADVENTURE)");

Make sure to correctly name your variables to fix this immediate bug.

However, after you fix your variable names, you may have problems trying to access variable names that are scoped such that your code cannot read it. If you declare a variable (such as let game_2 = ...) inside of a function, in general you will not be able to use that same variable outside of the function. A rudimentary fix would be to declare these variables in your main method block, and then do game_2 = prompt(...) inside of the other functions. This way, your variables will be accessible to all of your code’s scope. Here is a beginner friendly description of variable scope for Javascript. Please note that this is not a good long-term pattern, to have variables created only in the global scope, but will be a "good enough" short-term fix.

Leave a ReplyCancel reply