JavaScript | Code is bugged. Please help me debug


#1

I’m having trouble on having my forest(); function work, as well as the ‘no’ option that leads to the bridgeFight(); function.
I’ve debugged this whole code for many hours now, and there’s still that I still can’t figure out.
I did use boardwhiz94314’s encounter(); as a template for my bridgeFight function. His code works, but mine doesn’t even though I followed some of his code closely. If you can, please point out some of my mistakes.
Here is a link for his code: https://www.codecademy.com/JoshuaDouce/codebits/416Nnr
Thank you all for your help.


function Character(name, health, position, attack, defense, speed){
	this.name = name;
	this.health = health;
	this.position = position;
	this.attack = attack;
	this.defense = defense;
	this.speed = speed;
	this.strike = function(enemy){
		if (enemy.position === "foe"){
		enemy.health = enemy.health - this.attack;
		alert(this.name + " dealt " + this.attack + " damage to" + enemy.name);
		}
	};
}



var survivor;
var weathers = ["hail-storm", "tornado", "hurricane", "earthquake", "sand-storm"];
var shelters = ["abandoned-house", "cave", "car"];
var gameRunning = true;
var menBridge = "men";
var laceCounter = 0;

var badMen = new Character("Thugs", 8, "foe", 6, 7, 6);
var men = badMen;


function gameStart(){ 
	openingSequence();
	while (gameRunning === true){
		if (survivor.health > 0){
			advance(); 
		}
		else {
			alert("Game Over! Everything is done for!");
			gameRunning = false;
		}
	}
}


function openingSequence(){
	var answer = prompt("Are you ready to begin your journey through north? \n yes or no?");
	if (answer === "yes"){
		createCharacter();
		alert("The fate of the human existence hinges on your jouney to find out the cause of the apocolypse");
		alert("No one but you, " + survivor.name + " is willing to risk your life in finding the truth");
		alert("The air around you seems to change...The clouds and the sky seem malevolent...yet, something doesn't feel right about all of this");
		weather = randomWord(weathers);
		alert("A sudden " + weather + " just appeared!");
		
		shelter = randomWord(shelters);
		alert("You seek refuge in a(n) " + shelter + " and wait until it subsides");
		alert("The weather returns to normal and you continue your quest.");
		alert("Inside the " + shelter + " you find a map that guides your journey to the north");
		var ready = true;
		while (ready === true){
			var decision = prompt("The detailed map shows a route abruptly ended with skull at the end. Do you wish to continue the treacherous path? \n yes or no?");
			if (decision === "yes"){
				alert("You grab your belongings and begin your journey");
				ready = false;
			}
			else if(decision === "no"){
				alert("A small predicament in the beginning and you're already calling it quits? Humans have no future!");
				ready = false;
			}
			else {
				alert("please enter 'yes' or 'no'");
			}
		}
	}
	else { 
		alert("When you're ready to begin, press START");
	}
}


function advance(){
	alert("You keep moving on, passing desolated towns and ruins of civilizations");
	alert("You find yourself in a fork road leading to two different routes. Your map shows that both routes takes leads to the same \n destination. A route through a bridge or through the forest. Which route do you take?");
	var route = prompt("Do you wish to travel through the 'bridge' or through the 'forest'?").toLowerCase();
	if (route === "bridge"){
		bridge();
		beyondCross();
		gameRunning = false;
	}
	else if(route === "forest"){
		forest();
		beyondCross();
		gameRunning = false;
	}
	else {
		alert("Choose either 'bridge' or 'forest' when you're ready.");
	}
}

function bridge(){
	alert("The bridge seems long, and wide. There are many people are scavenging for food");
	alert("A group of 3 men stop you in your tracks. They ask you where you're headed and for your intentions");
	var ans = prompt("Do you agree to be honest with them? \n 'yes' or 'no'");
	if (ans === "yes"){
	bridgeHelp();
	}
	else if (ans === "no"){
	bridgeFight();
	}
	else{
	alert("Enter yes or no: ");
	bridge();
	}
}

function bridgeHelp(){
var help = prompt("For your time and honesty, two gifts are offered. Which would you prefer to take? A pair of boots or a machete? \n type 'boots' or 'machete'?");
switch(help){
  case 'machete':
      alert("You are given a machete, increasing your attack and defense by 2");
      break;
  case 'boots':
      alert("You are given a pair of boots, increasing your speed by 3");
      break;
  default:
      alert("Choose either boots or a machete");
    }
}


function bridgeFight(men){
    alert("The men grow suspicious at you. One of them tries to hold you down. You try to run but is faced with confronting them");
	var fighting = true;
	while (fighting === true){
		if (men.health > 0 && survivor.health > 0){
			if(survivor.speed > men.speed){
				alert("The " + men.name + " seem to move slow.");
				alert("You take your first punch on one of the men");
				survivor.strike(men);
				alert("One of them gets knocked out. Leaving a total health point of " + men.health);
				if (men.defense === survivor.defense){
					var react = prompt("Your defenses seem to be equal to those of " + men.name + "Do you wish to continue fighting? \n yes or no?");
					if (react === "yes"){
						survivor.strike(men);
						men.defense = men.defense - 1;
						alert("You've dwindled down their defenses to " + men.defense + " and their health to " + men.health);
						alert("You've won and saunter through them and keep heading north");
					}
					else {
						alert("You successfully flee from the " + men.name);
						fighting = false;
					}
				}
				else {
					alert("You've surprised them with your strength. Their shock gave you enough time to escape");
					fighting = false;
				}
			}
			else{
				alert("You are quickly engulfed by the 3 men...Faster than you could blink");
				var react2 = prompt("Do you wish to defend yourself? \n yes or no?");
				if (react2 === "yes"){
					men.strike(survivor);
					alert("You have been hit hard in the stomach. Your health drops down to " + survivor.health);
				}
				else {
					men.strike(survivor);
					alert("You have been hit too many times. Your health lowers to " + survivor.health +  "GAME OVER.");
					fighting = false;
				}
			} 
		}
		else {
			if (survivor.health <= 0){
				alert("You lose the power to persist. The fate of humans has ended");
				fighting = false;
			}
			else {
				alert("The " + men.name + " are defeated. You keep heading north");
				fighting = false;
			}	
		}
	}//while
}

function boatTree(){
	alert("You've eventually found the tree and brought it back to where you began.");
}

function lacing(){
	alert("You've proceded to tie the platform to the tree.");
	alert("After hours of lacing, you've finished");
}

function forest(){
	alert("You continue your journey through the forest.");
	alert("A loud sound of water echoes up ahead. There seems to be a 50 food gap between where you stood to the other side.");
	alert("A recollection of thoughts from the scouts taught you that a boat is needed to get across");
	alert("You take a few steps back decided to look for both east and west to find the boat-tree");
	var decision = prompt("Which direction do you wish to  first search? east or west?").toLowerCase();
	if (decision === "east"){
		alert("A few hundred feet within, tragedy struck");
		var rand = Math.floor((Math.random() * 3) + 1);
		switch(rand){
			case 1:
				alert("You fall into a quick-sand mud");
				survivor.health = survivor.heatlh - 1;
				alert("You manage to escape but it cost you some energy. \n Your health point is down by " + survivor.health); 
				boatTree();
				break;
			case 2:
				alert("A large tree branch falls off and damages your right shoulder");
				if (survivor.attack > 0){
					survivor.attack = survivor.attack - 1;
					alert("The damage does not allow you to hold your composure, therefore hindering your abilty to attack. \n Your attack goes down to " + survivor.attack);
					boatTree();
				};
				break;
			case 3:
				alert("A swarm of fire-ants injected with steroids began chewing on your feet!");
				if (survivor.speed > 0){
					survivor.speed = survivor.speed - 2;
					alert("You manage to escape from the ants by placing your feet into mud, relieving you from the ants. \n However, the damage has been done. Your inability to properly walk lowered your speed to " + survivor.speed);
					boatTree();
				};
				break;
			default:
				alert("Something went wrong");		
		}
	}
	if (decision === "west"){
		alert("There is an insidious atmosphere in the air.")
		alert("You stumble upon two small paths. You must choose one of the paths");
		var path = prompt("Which route do you choose to take? right or left?").toLowerCase();
			if (path === "right"){
				alert("An old witch stood at the end of the road, and behind her is the boat-tree");
				var name = prompt("Hi traveller, what is your name?");
				alert("Pleasure to meet you, " + survivor.name );
				alert("I know what your after...\nas all others before you had tried to obtain. \n I will give you the boat-tree if and only if you pass my test.");
				alert("You must choose my favorite workout from when I was a youngling. Then and only then will you obtain the boat-tree.\nIf you don't succeed, I will turn you into a protein shake.");
				var play = prompt("Do you wish to proceed?\nyes or no?");
					if (play === "yes"){
						var swoleWitch = [
    						'benchpress',
    						'squats',
    						'deadlifts'
						];
    					wordGuess();
    					alert("You bring the boat tree to where you've started and devised a plan.");
					}//if
					else if(play === "no"){
						var ans = prompt("Do you wish to go back to the east direction instead? \n yes or no?");
						if (ans === "yes"){
							forest();
						}
						else {
							continue;
						}
					}
					else{
						alert("You must choose either 'yes' or 'no'");
					}				
			}
			else if(path === "left"){
				alert("Fortunately for you, the boat-tree stood at the end of the path, guarded by no one");
				alert("You take the tree back with you from whence you came");
			}
			else {
				alert("Please choose either 'left' or 'right'");
			}
	}//if
}//forest();


function tieDaTree(){
	alert("With the boat-tree in hand, you realize that a platform is needed to stand on");
	alert("Fortunately, there is a large metal board near you.");
	var travel = prompt("The platform can be tied on the boat-tree. \n Do you wish to tie the metal board to the tree or just wing it and try \n to balance yourself on the tree? 'yes' for attaching the platform and \n 'no' for winging it?");
	if (travel === "yes"){
		do {
			laceCounter = laceCounter + 1;
			lacing();
		}
		while (laceCounter < 1);
		alert("You proceed to the river and get across safely");
		beyondCross();
	}
	else if (travel === "no"){
		alert("You've positioned yourself onto the tree");
		alert("You've made it to the other side\nThe ride was very wobbly, fish came jumping out of the water and slapped you in the face");
		alert("You've suffered minor scratches and -1 point in dignity");
		beyondCross();
	}
	else {
		alert("Choose either 'yes' or 'no'");
		tieDaTree();
	}
}

function beyondCross(){
	alert("After many months of long nights, long days and obstacles along the way. You've came to the end of the end to where the cross lie.");
	alert("You stumble upon a sign that read: \n 'Fooled ya!! Everyone you met was in on it..the world hasn't ended. :-)");	
}

function createCharacter(){
	alert("Create your character!");
	var name = prompt("What is your name?");
	if (name === ""){
	alert("Please input a name: ");
	createCharacter();
	}
	var newSurvivor = new Character(name, 5, null, 8, 7, 6);
	survivor = newSurvivor;
}

function randomWord(list){
	return list[Math.floor(Math.random() * list.length)];
}

function wordGuess(){
    favLift = randomWord(swoleWitch);
    var guess;
    do  {
        guess = prompt("'benchpress', 'squats' or 'deadlifts?'");
        if (favLift != guess){
            alert("Nope, not it. Keep guessing!");
        }
    }
    while (guess != favLift);
    alert("You got it! I had the best " + favLift + " in my prime.");
    alert("You are free to take the boat-tree.");
}