There used to be a popular type of pulp book for younger readers. They were called "Choose Your Own Adventure" novels. The idea was pretty simple. You'd start reading and then a few pages into the book, there would be a plot development that required a choice. The book might describe that you've entered a spooky house and the door has locked behind you. It would then ask you if you want to try to exit by going through the kitchen or exploring upstairs. If you chose to go into the kitchen, you'd turn to page 200, if you chose to go upstairs, you'd turn to page 43. This would repeat maybe five or six times though the book. You could re-read it a few times and try different outcomes. In principle this is the same as a bunch of if
statements.
But the stories were never very personal. We can do a lot more.
Your goal is to create a choose your own adventure story.
Start off by asking the user a few questions and storing these in an object called user
. That way you can always get the values from the user
object. So in your story you can write something like:
'You see your best friend, ' + user.bestFriendName + ', walking down the street.'
After you've stored some information to personalize the story, print out a few lines of text.
At each branch of the story ask the user a question that can be answered in a single word.
Example:
You are approaching broad street. Do you want to turn "left" or "right"?
Take the feedback and use it to print out a few more lines of your story, and then ask the user another question.
Use what you've learned about if...else
statements to provide new questions based on how the user answered the previous one.
For extra credit, use loops to make sure that you don't get weird answers. So if the you need the user to choose to drive or walk and they type in "bus" you should re-prompt them with the same question and make sure they don't go on without a valid answer. This will need to be done for every question. It is a significant enhancement to the program.
Note: This assignment has an extra credit component. To earn extra credit you must first earn a perfect score on the base requirements for the assignment.
Your story needs to have at least three (3) turning points. It can have more but no fewer. If the user answers three questions, this will result in eight (8) possible outcomes for your story. To keep from becoming confused you will have to keep your code well formatted and organized.
You don't need to write a novel. A sentence or two between each question is totally fine.
To understand this program, you must understand that when a condition in an "if" test fails, then the entire block is skipped and all the code that is contained in it will not be evaluated or run. If there is an associated "else" branch, then that code will be run instead. When the "if" condition is true, all the code in the "else" branch will be skipped and will not be evaluated or run. You should review this idea carefully and make sure it is clear to you before reading on...
This program will require you to format your code carefully. Use indentation rules the way I have demonstrated in class and lecture notes. You will need to place if/else blocks within other if/else blocks for this program to work. For example:
if (firstAnswer === 'left') {
// Ask another question
if (secondAnswer === 'yes') {
// Ask another question. } else {// ask a different questions
// Ask a different question. } } else {}
This example shows a single level of nesting. Your program must have two levels of nesting. In the simplest case, there should be a total of 7 if
keywords and 7 else
keywords.
if/else
blocks put the opening curly brace on the line that contains the beginning statement.if/else
blocks or loops on another line (not on the line stating the block).===
to ==
when checking for equality.Good example:
if (name === 'Bob') {
console.log('Bob is cool');
} else {console.log('You are not cool');
} while (bobAge < 40) {}
console.log('Bob is still cool');
Bad example:
if (name == 'Bob')
{
console.log('Bob is cool');
}else{console.log('You are not cool');
} while (bobAge < 40) {console.log('Bob is still cool');}
Here is an example of a how the program might work...
Enter your favorite color: red
Enter your favorite food: apples
Enter your favorite animal: tiger
You are walking down the street eating some apples when a red tiger runs up and takes your lunch and then runs away...
Do you want to "chase" the tiger or "run away"? > run awayYou run away from the tiger but it is a bad idea to run from an animal. It turns and decides to try to eat you. You see a bus blocking your path up ahead. Do you "get on" the bus or "go around"?
> go around You go around the bus only to see a subway entrance. Do you "go in" or "keep running"? > go in There is a zoo keeper in the subway. He captures the tiger and you are saved.The End.
But then if you ran it again it might do this...
Enter your favorite color: red
Enter your favorite food: apples
Enter your favorite animal: tiger
You are walking down the street eating some apples when a red tiger runs up and takes your lunch and then runs away...
Do you want to "chase" the tiger or "run away"? > chaseThe tiger makes a runs towards downtown. You need to prevent it from attacking people in the crowd. Do you call the "fire department" or the "police"?
> fire department The fire department shows up and sprays the tiger with hose. It stops running. Do you offer it a towel, "yes" or "no"? > yes The tiger appreciates the gesture and you have a new pet.The End.
Get Free Quote!
350 Experts Online