I am missing a { somewhere!


#1

console.log("After raiding a nearby cave, you try to leave with the treasures virtually oozing out of your pockets. Unfortunately, the sound of gold coins clattering on the ground awakes a dragon! He says 'I happened to hear someone stealing my treasure! If you give it back, I might let you leave alive.");
var user = prompt("Do you FIGHT, PAY, OUTSMART, or RUN?").toLowerCase();
switch(user)
case'fight':
var sword = prompt("Did you bring your sword?")
var attack = prompt("Where do you want to attack?","HEAD, CHEST, BACK").toLowerCase();
if(sword && attack = "head")
{
console.log("You sneak on top of the dragon and stab it in the head! The drgon lets out a agonizing roar and falls over screaming.")
}
else if(sword && attack = "chest")
{
console.log("You slide underneath the dragon and strike the dragon square in the heart! The dragon almost falls over and crushes you, but you roll out of the way!")
}
else if(sword && attack = "back")
{
console.log("You climb up the dragons tail and slice down it's back! The dragon whips you off and almost eats you, but he bleeds out right as you get a perfect picture of his jaw")
}
else
{
console.log("You can't decide where to strike! The dragon eats you, and you don't leave with your treasure or your life!")
}
break;
case'pay':
var treasure = prompt("Are you willing to give up your treasure?")
if(treasure)
{
console.log("You take up the dragons offer and give the dragon's treasure back. He decides to let you leave. You may not have the treasure, but at least you're alive!")
}
else
{
console.log("The desire to keep the treasure is too great! The dragn grows too impatiant to wait and burns you too a crisp!")
}
break;
case'outsmart':
var fakeTreasure = prompt("Do you have anything else to give the dragon?")
if(fakeTreasure)
{
console.log("You take out some gold-PLATED silver coins and throw them on the ground for the dragon. By the time he realizes he was tricked, you escape filthy rich!")
}
else
{
console.log("Your fake treasure doesn't fool the dragon for a second. He smacks you straight into the wall and takes the treasure from your dead body!")
}
break;
case'run':
var speed = prompt("Are you fast?")
var headStart = prompt("Did you get a head start?")
if(speed && headStart)
{
console.log("Before the dragon even realizes it, you start running straight out of the cave! The dragon chases you the best he can, but he can't catch up!")
}
else
{
console.log("You try to get away, but the dragon catches you and stomps you to the ground!")
}
break;
default:
console.log("You can't decide! Uh oh....")
When I try to run this code, it says "Syntaxerror: Expected { " But I can't find where I am missing it! Help!


#2

I figured it out! Ignore this topic.


#3

Well, now a new problem has arisen....BUT IT'S NOT TELLING ME!!!
It just says there is an error in my syntax
console.log("After raiding a nearby cave, you try to leave with the treasures virtually oozing out of your pockets. Unfortunately, the sound of gold coins clattering on the ground awakes a dragon! He says 'I happened to hear someone stealing my treasure! If you give it back, I might let you leave alive.");
var user = prompt("Do you FIGHT, PAY, OUTSMART, or RUN?").toLowerCase();
switch(user) {
case 'fight':
var sword = prompt("Did you bring your sword?")
var attack = prompt("Where do you want to attack?","HEAD, CHEST, BACK").toLowerCase();
if(sword && attack = "head")
{
console.log("You sneak on top of the dragon and stab it in the head! The drgon lets out a agonizing roar and falls over screaming.")
}
else if(sword && attack = "chest")
{
console.log("You slide underneath the dragon and strike the dragon square in the heart! The dragon almost falls over and crushes you, but you roll out of the way!")
}
else if(sword && attack = "back")
{
console.log("You climb up the dragons tail and slice down it's back! The dragon whips you off and almost eats you, but he bleeds out right as you get a perfect picture of his jaw")
}
else
{
console.log("You can't decide where to strike! The dragon eats you, and you don't leave with your treasure or your life!")
}
break;
case 'pay':
var treasure = prompt("Are you willing to give up your treasure?")
if(treasure)
{
console.log("You take up the dragons offer and give the dragon's treasure back. He decides to let you leave. You may not have the treasure, but at least you're alive!")
}
else
{
console.log("The desire to keep the treasure is too great! The dragn grows too impatiant to wait and burns you too a crisp!")
}
break;
case 'outsmart':
var fakeTreasure = prompt("Do you have anything else to give the dragon?")
if(fakeTreasure)
{
console.log("You take out some gold-PLATED silver coins and throw them on the ground for the dragon. By the time he realizes he was tricked, you escape filthy rich!")
}
else
{
console.log("Your fake treasure doesn't fool the dragon for a second. He smacks you straight into the wall and takes the treasure from your dead body!")
}
break;
case 'run':
var speed = prompt("Are you fast?")
var headStart = prompt("Did you get a head start?")
if(speed && headStart)
{
console.log("Before the dragon even realizes it, you start running straight out of the cave! The dragon chases you the best he can, but he can't catch up!")
}
else
{
console.log("You try to get away, but the dragon catches you and stomps you to the ground!")
}
break;
default:
console.log("You can't decide! Uh oh....")
};


#5

Any of these that you've left blank I'm pretty sure need to go to

if(fakeTreasure === "yes")

Like here if(sword === "yes" && attack === "chest");

because you are taking a user input. Use 3 = not 1, 1 makes attack become "chest" wheres as === checks if the input from the user for attack was chest.


#6

My guess is that the problem lies with the sword.

var sword = prompt("Did you bring your sword?")
var attack = prompt("Where do you want to attack?","HEAD, CHEST, BACK").toLowerCase();
 if(sword && attack = "head")
{
  console.log("You sneak on top of the dragon and stab it in the head! The drgon lets out a agonizing roar and falls over screaming.")
}

The user-provided string answer to the prompt will always evaluate as a True value. You should provide an allowance for "Yes" or "No" (string) answers like their example does:

var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();

switch(troll) {
  case 'FIGHT':
    var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
    var smart = prompt("Are you smart?").toUpperCase();
if(strong === 'YES' || smart === 'YES') {
console.log("You only need one of the two! You beat the troll--nice work!");
} else {
console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
}
break;

#7

Oh yes and remember to use the === sign. That means the type (ie: a string) and value (the content in a string) must correspond exactly. Using = means you're assigning or reassigning a value.


#8

You were partly right, turning items like (fakeTreasure) into (fakeTreasure === "yes") didn't matter, but turning items like(attack = "chest") into (attack === "chest") worked to fix the program. It works perfectly now! Thanks!


#9

Wow, this program is troublesome :stuck_out_tongue:
I am programming a hydra battle, and I can't think of a working way to do it.
Here is how it looks:
var hydraHealth = 8
var playerHealth = 5
var attackDamage1 = Math.floor(Math.random * 5 + (attack + 2))
var attackDamage2 = Math.floor(Math.random * 5 - (defense - 2))
var attackChance = Math.floor(Math.random * 6)
hydraHealth is supposed to get hydraHealth minus attackDamage1(The damage you deal)
hydraHealth = hydraHealth - attackDamage1
the formula is the same for the hydra dealing damage to you
playerHealth = playerHealth - attackDamage2
If you need to see the rest of the code for the hydra battle, tell me.