View Full Version : Learning javascript, what's the easiest way to do this?

May 22nd, 2014, 06:46 PM
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:


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


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.

Philip M
May 22nd, 2014, 08:30 PM
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

May 22nd, 2014, 10:33 PM
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.

Philip M
May 23rd, 2014, 07:57 AM
A good way to learn Javascript is felgall's tutorial site

Introducing JavaScript (http://javascriptexample.net/)