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
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning javascript, what's the easiest way to do this?

    I am just learning and I figured creating a little game would be a good first experiment. I've been going through Code Academy and Code.org courses (any other better suggestions I'm all ears), so I'm still trying to grasp things.

    So I want to make a story game, where you tell a story, and the user can either choose this option, or this. And those options keep branching out to different storylines depending on the users decision.

    I can do basic stuff (at least all I know is alert, prompt and console.log, but if I want to put it in an app and make it look pretty if it goes well then it'll probably be different. So I'm definitely open to suggestions on that.)

    But there is so many branch offs I just want to know the best way to handle all this. Here is a quick example of what I can do:

    Code:
    <script>
    
    var userAnswer = prompt("Hello there! TYPE Hello or Shut up"); {
    
    if (userAnswer === "Hello") {
    var u = prompt("How are you doing?"); }
    
    else if (userAnswer === "Shut up") {
    var c = prompt("crap off then!");
     }
    
    else  {
    confirm("I don't understand that, read the question right!");  
    }
    
    if (u === "good") 
    {
    alert("Great to hear!");
    }
    else if (u === "bad") {
    alert("I'm sorry to hear that.");
    }
    else {
    alert("I don't understand that..");
    }
    }
    
    </script>

    Just a quick decision type of deal. Though it's not completely perfect since if I type something (or nothing) in the first question, it will tell me both else's in a row. But this is just practice of course lol.

    But if I were to go in where if they said bad, ask this question, then that goes to you wanna hangout later and watch a movie and cheer up to other stuff, and if they say good- thats good to hear, you wanna come to a party tonight since yo're in a good mood, then that going into other things (for example, this def not my game lol).

    Anyways you get the drift, I appreciate anyone's suggestions, I don't necessarily want an annoying alert/prompt game but it's all I know right now. Idk how you guys create actual games with it, with imagery and what not, but I'm eager to learn.

    Note I'm not learning to make games, I just started college and want to go into the IT area, and I figured I would like to start learning code as a hobby before I get to those classes. That's why it's not C++ cause I'm not going for a game designer, I just thought making a game would be a good and fun way to learn hands on.
    Thank you.

  • #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
    Be aware that prompts and alerts are regarded as obsolete and should be used only for testing purposes.

    As Javascript is case sensitive "Hello" != "hello". Use toLowerCase() to change all responses to lower case.

    But all text games of this kind have the weakness that the user's response may not be one that you expect.

    Better to do something like
    var userAnswer = prompt("Hello there! Type H for Hello or S for Shut up","");
    and then just check the first letter (to LowerCase()).

    This sort of thing is not really a very good way of learning Javascript as it uses a limited range of statements endlessly repeated. You would do better with something like the pizza menu script offering choices of pizzas at different prices, different toppings, drinks choices, final cost price plus tax etc.

    Some coaches would have poured fire on those stormy waters. - Commentator Radio 5 Live
    Last edited by Philip M; 05-23-2014 at 07:36 AM.

    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
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    For inputs you should use a form within the web page. You can then attach a submit event listener to the form to detect when your user makes an input.

    The simplest way to output text into the web page is to place an id on the element within the page where you want the text to appear and then use document.getElementById('theID').innerHTML to load your desired text into the page. There are better less obtrusive ways to do this but for a simple text game this is probably the simplest.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    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
    A good way to learn Javascript is felgall's tutorial site

    Introducing JavaScript

    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.


  •  

    Tags for this Thread

    Posting Permissions

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