If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Very Simple Word Game Code Executes As Text :(

I am so sorry for yet another nooby thread, but I am just starting out and having a few teething problems.

I have tried a few things with the following, but nothing seems to work. I have tried <script> tags, and different browsers. A friend told me it executes fine.

Here is the code. Please don't judge. This is my first ever self-wrote code. I know it sounds crap (especially towards the end when I just wanted to put any text in to check the multiple if statement). Anyhow, here is my code:

Code:

<script>
var age = prompt("How old are you?");
if( age < 18)
{
alert("You are to young to play this game!");
}
else
{
alert("Enjoy the game");
.
}
alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
\
and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
\
to go?");
var choiceOne = prompt("left (press 1) or right (press 2)?");
if(choiceOne == "1")
{
alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision, but\
still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
var choiceTwo = promt("Investigate the laugh (press 1) or ignore (press 2)?")
if(choiceTwo = "1")
{
alert("out jumps a monster and eats you"); //fix this Mikey FFS!
}
else
{
alert("you win");
}
else
{
alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
}
</script>

Firstly, using alerts for this is ridiculous, even I have to admit. Better off using form elements and DOM for altering text, etc.

Secondly, you can't spread an alert across more than one line, AFAIK. If you need, declare a variable, and do:
thisVar = "blah blah blah";
thisVar += "more blah more blah";
thisVar += "EVEN MORE";
alert(thisVar);

I rarely use prompt, and I don't think keyboard 1 or 2 will affect anything.. just clicks.

^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

@Effexion, you don't need to aplogize for starting to learn code. We've all been there and stop saying

Please don't judge.

We all do, but don't always say anything. LOL

WolfShade is correct coding with alerts is a bad habit. We do use them in trouble shooting errors though.

You have left something out like brackets and semi-colons. Here's a working code:

Code:

<script>
var age = prompt("How old are you?");
if( age < 18){
alert("You are to young to play this game!");
}else{
alert("Enjoy the game");
}
alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
to go?");
var choiceOne = prompt("left (press 1) or right (press 2)?");
if(choiceOne == "1"){
alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision,\
but still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
}else{
alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
}
var choiceTwo = prompt("Investigate the laugh (press 1) or ignore (press 2)?");
if(choiceTwo == "1"){
alert("out jumps a monster and eats you"); //fix this Mikey FFS!
}else{
alert("you win");
}
</script>

also, read what the guy said, he said he's new to the stuff, so don't expect him to know everything. WE don't all need to jump on him about the alert thing, he is just trying to create a conditional.

Originally Posted by sunfighter

@Effexion, you don't need to aplogize for starting to learn code. We've all been there and stop saying We all do, but don't always say anything. LOL

WolfShade is correct coding with alerts is a bad habit. We do use them in trouble shooting errors though.

You have left something out like brackets and semi-colons. Here's a working code:

Code:

<script>
var age = prompt("How old are you?");
if( age < 18){
alert("You are to young to play this game!");
}else{
alert("Enjoy the game");
}
alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
to go?");
var choiceOne = prompt("left (press 1) or right (press 2)?");
if(choiceOne == "1"){
alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision,\
but still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
}else{
alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
}
var choiceTwo = prompt("Investigate the laugh (press 1) or ignore (press 2)?");
if(choiceTwo == "1"){
alert("out jumps a monster and eats you"); //fix this Mikey FFS!
}else{
alert("you win");
}
</script>

If you want your code to be readable, and you want to use long strings, just break them up and then concatenate them:

Code:

alert( "You're walking down the street and come to a fork.\n"
+ "To the right you see beautiful streams and rolling countryside.\n"
+ "To the left, dead trees, rain, sleet, and misery.\n"
+ "Which way would you like to go?" );

And, yes, alert() should be avoided. But the above would still be one way to create long strings. Perhaps:

Code:

var current = document.getElementById("currentLocation");
...
current.innerHTML =
"You're walking down the street and come to a fork.<br/>"
+ "To the right you see beautiful streams and rolling countryside.<br/>"
+ "To the left, dead trees, rain, sleet, and misery.<br/>"
+ "Which way would you like to go?";

Also, using prompts and alerts and tons of "if" statements as you are, the game coding is going to get really ugly, really fast. Even if prompts and alerts looked good, which they don't, you need to find another way to represent the game to make the coding easier and make it easier to read and repair it when something goes wrong.

Many, many thanks Old Pedant. I have been learning using Codecademy. This is the practice they sort of teach so I was just adapting what I had learned to blindly make that game. It isn't anything serious, just a little practice run for me until I learn further. I really appreciate your help there man, I will be sure to take it in to account and learn from it.