Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Thanked 0 Times in 0 Posts

    My switch(variable) goes straight to the default?

    I just learned javascript last week and i have been practicing it by making a choose your own adventure type game. Here is the code.
    function Player(name, health, wallet, defense) {
        this.name = name;
        this.health = health;
        this.wallet = wallet;
        this.defense = defense;
    function Monster(name, health) {
        this.name = name;
        this.health = health;
    var game = function(choice1, choice2, choice3) {
        prompt("Now what shall we do?  " + choice1 + ", " + choice2 + ", or level up " + choice3 + "?").toLowerCase();
    var userName = prompt("Welcome player! What is your name?");
    var player = new Player(userName, 100, 0, 0);
    confirm("Well " + player.name + " here is 500 gold to get you started.");
    player.wallet = 500;
    confirm(player.wallet + " gold has been added to your wallet!");
    game("SHOP", "QUEST", "STATS");
    switch(game) {
        case 'shop' :
            confirm("It works");
        case 'quest' :
        case 'stats' :
            confirm("That is not an option!");
    at the top I have the function called game that I plan on using when a player does not choose one of the cases i can use that instead of retyping the message everytime to give them the options. Problem is when you type in one of the choices it goes straight to the default option and I cannot figure out why. I have played around with it for hours and nothing seems to work.

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,703 Times in 2,679 Posts
    There are quite a few errors in your script. Use your error console. One thing you need to know is that Javascript is case-sensitive, so SHOP != shop.
    Prompts and confirm boxes are considered to be obsolete. Don't waste time with that antiquated stuff. Rather than entering text (which at best may be mis-spelled or just incomprehensible ) use select lists for your user to choose play options.

    Confirm dialogs (if you must use them) take the form var ans = window.confirm ("Do you want to do X?"); then test the response true or false using an if statement to branch accordingly - if (ans = true) { do something, else do something else.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 08-25-2014 at 07:55 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  3. #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Thanked 626 Times in 605 Posts
    game is a function, but your switch thinks it's a string.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  4. #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    In other words, you needed to do
    switch( game("SHOP", "QUEST", "STATS") ) {
    or maybe
    var choices = game("SHOP", "QUEST", "STATS");
    switch( choices ) {
    But as Philip said, there are other errors in there. Hit the F12 key to bring up the error console and the JavaScript debugger. Learn to use both. (I prefer the versions found in the Chrome browser, but that's personal preference. All the others will work; you just have to learn to use them.)
    Be yourself. No one else is as qualified.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts