Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions boolean-expressions-and-conditional-statements.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
/*

Objective:
You will practice creating and combining boolean expressions
to drive logic and outcomes in you program.

Instructions:
If you are not familiar with the concept of a text-based adventure game,
let's set the scene...
Example: "You wake up in a dark forest. There are two paths ahead of you:
one leading to the mountains and one to a village.
Your choices will determine your fate!"

Define the Requirements: You must:
- Write conditional statements to handle player choices.
- Use boolean expressions to combine multiple conditions.
- Include at least one use of logical operators (&&, ||, !).

Starter Code:
- Run the following command in your terminal to install the readline-sync module:
npm install readline-sync

Paste the following code into your editor:

*/

const readline = require('readline-sync');

// Inventory
const hasTorch = true;
const hasMap = false;
const hasSword = true;
const hasCompass = false;

console.log("You wake up at the edge of a dense forest. In front of you are two paths: one leads to the mountains, the other to a mysterious village.");

const firstChoice = readline.question("Do you go to the 'mountains' or the 'village'? ");

if (firstChoice === "mountains") {
if (hasTorch) {
console.log("You safely navigate the dark and narrow mountain paths.");
const mountainChoice = readline.question("You find a cave. Do you want to 'enter' or 'pass' by it? ");

if (mountainChoice === "enter") {
if (hasSword) {
console.log("Inside the cave, a wild beast attacks! You fight bravely with your sword and survive.");
} else {
console.log("You are attacked by a beast and have no weapon to defend yourself. Game over.");
}
} else {
console.log("You pass the cave and eventually find a beautiful mountain lake.");
}

} else {
console.log("It's too dark to proceed into the mountains. You turn back.");
}

} else if (firstChoice === "village") {
if (hasMap || hasCompass) {
console.log("Using your navigation tools, you find your way to the village.");
const villageChoice = readline.question("In the village, do you visit the 'market' or the 'tavern'? ");

if (villageChoice === "market") {
console.log("You find a mysterious merchant selling magical items.");
} else if (villageChoice === "tavern") {
console.log("You hear rumors of treasure hidden deep in the forest.");
} else {
console.log("You wander around aimlessly and miss out on interesting encounters.");
}

} else {
console.log("You get lost in the woods trying to reach the village. Try again!");
}

console.log("You see two paths: one leads to the mountains, the other to the village.");
const choice = readline.question("Do you go to the 'mountains' or the 'village'?");

if (choice === "mountains" && hasTorch) {
console.log("You safely navigate through the dark mountains.");
} else if (choice === "mountains" && !hasTorch) {
console.log("It's too dark to proceed. You decide to turn back.");
} else if (choice === "village" || hasMap) {
console.log("You find your way to the village.");
} else {
console.log("You get lost and wander aimlessly.");
console.log("Unable to decide, you sit down and the adventure passes you by.");
}

/*

Add Customization and expand the game:
- Add more choices and scenarios.
- Include additional items (e.g., a sword, a compass).
- Use nested conditionals and logical operators to create complex outcomes.

*/