Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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.
    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");
            break;
        case 'quest' :
            
            break;
        case 'stats' :
            
            break;
        default:
            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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 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 06: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
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    game is a function, but your switch thinks it's a string.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    In other words, you needed to do
    Code:
    switch( game("SHOP", "QUEST", "STATS") ) {
    or maybe
    Code:
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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