Help with using input values with variables along with if, else if code

I think you plan a kind of text adventure.

That’s very nice!

You might want to read the Wikipedia article about this.

I want to mention something:

You start here:

var Story =
["You wake up with the sun shining in your eyes. \nWhat do you do? \n\nA.Stand up \nB.Sit up \nC.Check yourself for injuries" 
];

Then you evaluate this

if (CurrentArea == "StarterBeach" && inputValue == "A")

		{
			Story("You stand up, and look around.");
			input.value("");
		}

		else if (CurrentArea == "StarterBeach;")
		{

			Story("Please type an answer into the box at the bottom and click the submit button");

		}

So after he chose A, B or C he / she is on anther screen (3 other screens in fact).

There he has to choose again A,B, C (for each of the 3 screens).

And again. And again. So it’s a big tree.

Your way to go there with if will lead into confusion. Try it out for a few hours. Good luck.

Idea

The reason is basically that you mix your data (your text) with the program logic (your ifs and elses).

It is wise to separate data and program logic.

Can you think of a data structure to help you with that?

I will suggest this:

Data structure:

Each sentence has a line number. Each sentence has 2 or 3 options to choose from. These lead in turn to (the line number of) new sentences.

program logic

You have ONE variable holding the current line number. Depending on the choosing of the user, we change the line number to the new line number and show the new sentence (for the new line number) and its 2-3 options.

Example

You can for each screen have ONE String in an array.

You can use the command split() to split at “#”. You get an arrayOneLine

Now the first item in arrayOneLine is the line number. The 2nd choice one. The 3rd line number to go to when choise one has been selected and so on:


"0#You wake up with the sun shining in your eyes. \nWhat do you do?# A.Stand up #1#B.Sit up #2#C.Check yourself for injuries#3"
"1#"You stand up, and look around.#....#4#....#5#"

(or simplify:
"0#Text \nwith choices\n.....#target line number 1#target line number 2#target line number 3"

)

See