6/6 My inefficient code


#1

Hi all,

How can I improve this code so I don't have to repeat the same code if different areas? I feel like I am doing everything the "long way"...

var start = alert("ENCOUNTER WITH THE ELF KING");

var x=-1;
while(x<0){
if (x==-1) {
skill1 = prompt("Please choose your first skill. Type one of the following: \n 1) SWORDPLAY \n 2) SPELLS \n 3) UNARMED COMBAT \n 4) CHARMS \n 5) CUNNING \n 6) FOLKLORE").toUpperCase();
}
if( skill1.toUpperCase() == "SWORDPLAY" ) x = 1;
if( skill1.toUpperCase() == "SPELLS" ) x = 1;
if( skill1.toUpperCase() == "UNARMED COMBAT" ) x = 1;
if( skill1.toUpperCase() == "CHARMS" ) x = 1;
if( skill1.toUpperCase() == "CUNNING" ) x = 1;
if( skill1.toUpperCase() == "FOLKLORE" ) x = 1;
if(x==-1){alert("That\'s not right!");}
}
while(x<2){
if (x==1) {
skill2 = prompt("Please choose your second skill. Type one of the following: \n 1) SWORDPLAY \n 2) SPELLS \n 3) UNARMED COMBAT \n 4) CHARMS \n 5) CUNNING \n 6) FOLKLORE").toUpperCase();
}
if( skill2.toUpperCase() == "SWORDPLAY" ) x = 2;
if( skill2.toUpperCase() == "SPELLS" ) x = 2;
if( skill2.toUpperCase() == "UNARMED COMBAT" ) x = 2;
if( skill2.toUpperCase() == "CHARMS" ) x = 2;
if( skill2.toUpperCase() == "CUNNING" ) x = 2;
if( skill2.toUpperCase() == "FOLKLORE" ) x = 2;
if(skill1 === skill2){x = 1;}
if(x==1){alert("That\'s not right!");}
}
while(x<3){
if (x==2) {
skill3 = prompt("Please choose your third skill. Type one of the following: \n 1) SWORDPLAY \n 2) SPELLS \n 3) UNARMED COMBAT \n 4) CHARMS \n 5) CUNNING \n 6) FOLKLORE").toUpperCase();
}
if( skill3.toUpperCase() == "SWORDPLAY" ) x = 3;
if( skill3.toUpperCase() == "SPELLS" ) x = 3;
if( skill3.toUpperCase() == "UNARMED COMBAT" ) x = 3;
if( skill3.toUpperCase() == "CHARMS" ) x = 3;
if( skill3.toUpperCase() == "CUNNING" ) x = 3;
if( skill3.toUpperCase() == "FOLKLORE" ) x = 3;
if(skill1 === skill3){x = 2;}
if(skill2 === skill3){x = 2;}
if(x==2){alert("That\'s not right!");}
}
console.log('You chose: ' + skill1 + "," + " " + skill2 + " " + "and"+ " " + skill3);

var combatSkills = skill1 + skill2 + skill3;
if (skill1 === "SWORDPLAY" && skill2 === "UNARMED COMBAT" || skill2 === "SWORDPLAY" && skill1 === "UNARMED COMBAT"){
combatSkills = 7;
}
else if (skill1 === "SWORDPLAY" && skill3 === "UNARMED COMBAT" || skill3 === "SWORDPLAY" && skill1 === "UNARMED COMBAT"){
combatSkills = 7;
}
else if (skill2 === "SWORDPLAY" && skill3 === "UNARMED COMBAT" || skill3 === "SWORDPLAY" && skill2 === "UNARMED COMBAT"){
combatSkills = 7;
}
else if (skill1 === "SWORDPLAY" || skill2 === "SWORDPLAY" || skill3 === "SWORDPLAY"){
combatSkills = 6;
}
else if (skill1 === "UNARMED COMBAT" || skill2 === "UNARMED COMBAT" || skill3 === "UNARMED COMBAT"){
combatSkills = 3;
}
else {
combatSkills = 2;
}

var arcaneSkills = skill1 + skill2 + skill3;
if (skill1 === "SPELLS" && skill2 === "CHARMS" || skill2 === "SPELLS" && skill1 === "CHARMS"){
arcaneSkills = 6;
}
else if (skill1 === "SPELLS" && skill3 === "CHARMS" || skill3 === "SPELLS" && skill1 === "CHARMS"){
arcaneSkills = 6;
}
else if (skill2 === "SPELLS" && skill3 === "CHARMS" || skill3 === "SPELLS" && skill2 === "CHARMS"){
arcaneSkills = 6;
}
else if (skill1 === "SPELLS" || skill2 === "SPELLS" || skill3 === "SPELLS"){
arcaneSkills = 4;
}
else if (skill1 === "CHARMS" || skill2 === "CHARMS" || skill3 === "CHARMS"){
arcaneSkills = 4;
}
else {
arcaneSkills = 2;
}

var witSkills = skill1 + skill2 + skill3;
if (skill1 === "CUNNING" && skill2 === "FOLKLORE" || skill2 === "CUNNING" && skill1 === "FOLKLORE"){
witSkills = 7;
}
else if (skill1 === "CUNNING" && skill3 === "FOLKLORE" || skill3 === "CUNNING" && skill1 === "FOLKLORE"){
witSkills = 7;
}
else if (skill2 === "CUNNING" && skill3 === "FOLKLORE" || skill3 === "CUNNING" && skill2 === "FOLKLORE"){
witSkills = 7;
}
else if (skill1 === "CUNNING" || skill2 === "CUNNING" || skill3 === "CUNNING"){
witSkills = 5;
}
else if (skill1 === "FOLKLORE" || skill2 === "FOLKLORE" || skill3 === "FOLKLORE"){
witSkills = 4;
}
else {
witSkills = 2;
}
var playerHealth = (Math.floor(Math.random()2)) + (2combatSkills) + (3*arcaneSkills) + (2*witSkills);
var speedStat = (Math.random()2) + (Math.random()) + (Math.random()) + (0.35combatSkills) + (0.3*arcaneSkills) + (0.2*witSkills);
var playerSpeed = Math.floor(speedStat);
console.log("Your HEALTH POINTS =" + " " + playerHealth);
console.log("Your COMBAT SKILL LEVEL =" + " " + combatSkills);
console.log("Your ARCANE SKILL LEVEL =" + " " + arcaneSkills);
console.log("Your WIT SKILL LEVEL =" + " " + witSkills);
console.log("Your SPEED =" + " " + playerSpeed);

var elfKingHealth = (Math.floor(Math.random()7)) + (Math.floor(Math.random()2)) + 27;
var elfKingCombatSkills = (Math.floor(Math.random()4) + 1) + (Math.floor(Math.random()3)) + 2;
var elfKingArcaneSkills = (Math.floor(Math.random()*4)) + 1 + 3;
var elfKingWitSkills = (Math.floor(Math.random()*4)) + 1 + 4;

console.log("Elf King's HEALTH POINTS =" + " " + elfKingHealth);
console.log("Elf King's COMBAT SKILL LEVEL =" + " " + elfKingCombatSkills);
console.log("Elf King's ARCANE SKILL LEVEL =" + " " + elfKingArcaneSkills);
console.log("Elf King's WIT SKILL LEVEL =" + " " + elfKingWitSkills);

var user = prompt("How will you fight the Elf King? \n 1) Use COMBAT skills. \n 2) Use ARCANE skills. \n 3) Use neither combat or arcane skills instead rely on your WIT. \n 4) RUN away!").toUpperCase();

switch(user) {
case "ARCANE":
var arcaneSwitch = alert("How strong is your magic against the Elf King?");
if (arcaneSkills > elfKingArcaneSkills){
alert("Your arcane abilities are superior. You should be able to hold your own againt the Elf King!");
}
else if (arcaneSkills == elfKingArcaneSkills){
alert("You are both equally skilled in the arcane arts. This will be a close fight!");
}
else {
alert("The Elf King's arcane abilities are superior. This will be a tough battle!");
}
function loop() {
while ((playerHealth > 0) && (elfKingHealth > 0)){
if (arcaneSkills > elfKingArcaneSkills) {
elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 10);
playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 5);
console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
console.log("Your HEALTH is now" + " " + playerHealth);
if (playerHealth > 0 && elfKingHealth <= 0) {
console.log("YOU WIN!");
}
else if (playerHealth <= 0 && elfKingHealth > 0) {
console.log("YOU LOSE!");
}
else if (playerHealth <= 0 && elfKingHealth <= 0) {
console.log("YOU KILLED EACH OTHER!");
}
}
else if (arcaneSkills == elfKingArcaneSkills) {
elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 10);
playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 10);
console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
console.log("Your HEALTH is now" + " " + playerHealth);
if (playerHealth > 0 && elfKingHealth <= 0) {
console.log("YOU WIN!");
}
else if (playerHealth <= 0 && elfKingHealth > 0) {
console.log("YOU LOSE!");
}
else if (playerHealth <= 0 && elfKingHealth <= 0) {
console.log("YOU KILLED EACH OTHER!");
}
}
else {
elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 5);
playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 10);
console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
console.log("Your HEALTH is now" + " " + playerHealth);
if (playerHealth > 0 && elfKingHealth <= 0) {
console.log("YOU WIN!");
}
else if (playerHealth <= 0 && elfKingHealth > 0) {
console.log("YOU LOSE!");
}
else if (playerHealth <= 0 && elfKingHealth <= 0) {
console.log("YOU KILLED EACH OTHER!");
}
}

}

}
loop();
break;

case "COMBAT":
    var combatSwitch = alert("You face off with the Elf King in melee combat!");
    if (combatSkills > elfKingCombatSkills){
        alert("Your combat abilities are superior. You should be able to hold your own againt the Elf King!");
    }
    else if (combatSkills == elfKingCombatSkills){
        alert("You are both equally skilled in combat. This will be a close fight!");
    }
    else {
        alert("The Elf King's combat abilities are superior. This will be a tough battle!");
    }
    function loop() {
    while ((playerHealth > 0) && (elfKingHealth > 0)){
    if (combatSkills > elfKingCombatSkills) {
        elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 8);
        playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 1);
        console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
        console.log("Your HEALTH is now" + " " + playerHealth);
        if (playerHealth > 0 && elfKingHealth <= 0) {
            console.log("YOU WIN!");
        }
        else if (playerHealth <= 0 && elfKingHealth > 0) {
            console.log("YOU LOSE!");
        }
        else if (playerHealth <= 0 && elfKingHealth <= 0) {
            console.log("YOU KILLED EACH OTHER!");
        }
    }
    else if (combatSkills == elfKingCombatSkills) {
        elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 8);
        playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 8);
        console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
        console.log("Your HEALTH is now" + " " + playerHealth);
        if (playerHealth > 0 && elfKingHealth <= 0) {
            console.log("YOU WIN!");
        }
        else if (playerHealth <= 0 && elfKingHealth > 0) {
            console.log("YOU LOSE!");
        }
        else if (playerHealth <= 0 && elfKingHealth <= 0) {
            console.log("YOU KILLED EACH OTHER!");
        }
    }
    else {
        elfKingHealth = elfKingHealth - ((Math.floor(Math.random()*5)) + 3);
        playerHealth = playerHealth - ((Math.floor(Math.random()*8)) + 8);
        console.log("Elf King's HEALTH is now" + " " + elfKingHealth);
        console.log("Your HEALTH is now" + " " + playerHealth);
        if (playerHealth > 0 && elfKingHealth <= 0) {
            console.log("YOU WIN!");
        }
        else if (playerHealth <= 0 && elfKingHealth > 0) {
            console.log("YOU LOSE!");
        }
        else if (playerHealth <= 0 && elfKingHealth <= 0) {
            console.log("YOU KILLED EACH OTHER!");
        }
    }
}

}
loop();
break;

case "WIT":
    var witSwitch = alert("You challenge the Elf King to a battle of wits!");
    if (witSkills > elfKingWitSkills){
        alert("Your knowledge and intellect is far superior than the Elf King's - perhaps even too good. You manage to humiliate the Elf King in front of his royal court. BAD MOVE! You don't see the elven arrow which pierces your heart...");
        console.log("YOU LOSE!");
    }
    else if (combatSkills == elfKingCombatSkills){
        alert("You are both equally knowledgeable; you manage to demonstrate your intellect without embarrassing the Elf King, which wins his respect. WELL PLAYED!");
        console.log("YOU WIN!");
    }
    else {
        alert("The Elf King scoffs at your attempt to challenge him in a battle of wits. You manage to anger the Elf King. BAD MOVE! You don't see the elven arrow which pierces your heart...");
        console.log("YOU LOSE!");    
    }
break;

case "RUN":
    if (playerSpeed > 6){
        alert("You prove too quick even for the elves. You dodge and weave through the volley of elven arrows and magical missiles, and manage to evade capture from the royal court guards as you focus on your escape. The Elf King looks on in shock as you avoid everything his royal court throws at you - triumphant, you disappear into the depths of the enchanted forest.");
        console.log("YOU ESCAPED!");
    }
    else if (playerSpeed <= 6 && playerSpeed > 4){
        alert("You prove too quick even for the elves. You manage to dodge and weave through the volley of elven arrows and magical missiles, but some of them hit their target and you stagger as you feel blood trickling down your back. Although wounded, you manage to escape and disappear into the depths of the enchanted forest.");
        console.log("YOU ESCAPED!");
    }
    else {
    alert("You try to run away; but, a volley of elven arrows pierce your body and you slam onto the forest floor with a woeful cry. Blood gushes out of the many entry wounds and your vision slowly darkens as the pain fades away...");
    console.log("YOU LOSE!");
    }
break;

default:
alert("I didn't understand your choice. Hit Run and try again, this time pick ARCANE, COMBAT, WIT, or RUN!");
}