Javascript Error.. Basic stuff! Need help

Javascript Error.. Basic stuff! Need help
0

#1

Hello guys!
Making this system to attack and lower a players health.

this is what i’ve come up with, But im getting an error at line 247 “unexpected identifier”. Help? ? Thanks! <3

Line 247 = logPlayerAttack.push(damage);

var playerName;
var playerHealth;
var attackType;
var damage;
var logPlayerAttack;
var logSpiderAttack;
var flagPlayerDead;
var flagSpiderDead;


function mageFireBall() { //Fireball spell! Damage is 1 - 45.
	var attackChance;
	var attackDamage = 0;
	attackChance = Math.floor(Math.random() * 100); // Chance calculater, Returns a number from 0 - 100
	if (attackChance < 30) { // If number returned from attackChance is < 30 Runs Low damage
		/* do low damage */
		attackDamage = Math.round(Math.random() * 10 + 1); // Math.Round rounds the number to a whole, Math.random gives a random number from 0 to 10, + 1.
	} else if(attackChance >= 30 && attackChance < 70) { // If chance is bigger or = to 30 && smaller then 70, Run medium damage.
		/* do medium damage */
		attackDamage = Math.round(Math.random() * (20 - 10) + 10); // Same as low just higher values
	} else {
		/* do high damage */
		attackDamage = Math.round(Math.random() * (45 - 30) + 30); // Same as medium and low just higer values.
	}
	return attackDamage;
}
function mageIceBolt() { //ice Bolt spell! Damage is 5 - 38.
	var attackChance;
	var attackDamage = 0;
	attackChance = Math.floor(Math.random() * 100); // Chance calculater, Returns a number from 0 - 100
	if (attackChance < 30) { // If number returned from attackChance is < 30 Runs Low damage
		/* do low damage */
		attackDamage = Math.round(Math.random() * 10 + 5); // Math.Round rounds the number to a whole, Math.random gives a random number from 0 to 10, + 1.
	} else if(attackChance >= 30 && attackChance < 70) { // If chance is bigger or = to 30 && smaller then 70, Run medium damage.
		/* do medium damage */
		attackDamage = Math.round(Math.random() * (20 - 15) + 10); // Same as low just higher values
	} else {
		/* do high damage */
		attackDamage = Math.round(Math.random() * (45 - 35) + 30); // Same as medium and low just higer values.
	}
	return attackDamage;
}

// Spiders Attack
function spiderBasicAttack () {
  var attackChance;
  var attackDamage = 0;
  attackChance = Math.floor(Math.random() * 100);
  if (attackChance < 30) {
    attackDamage = Math.round(Math.random() * 15 + 5);
  } else if (attackChance >= 30 && attackChance < 60) {
    attackDamage = Math.round(Math.random() * ( 30 - 15) + 15);
  } else {
    attackDamage = Math.round(Math.random() * (50 - 30) + 30);
  }
  return attackDamage;
}
// Default variable values, Variables we're announced at the start of the code.

var playerHealth = 100;
var spiderHealth = 100;
var damage = 0;
var logPlayerAttack = [];
var logSpiderAttack= [];
var flagPlayerDead = false;
var flagSpiderDead = false;
var flagReplay = false;

alert('Welcome to Fireball game '); // Alerts that we entered the game
var playerName = prompt('Enter your name ', "Player one");
alert('Welcome to the fireball game, You\'l need to press attack lol ');

do {
  attackType = Number(prompt(playerName + 'Choose an Attack! \n 1- FireBall \n 2- IceBolt'));
  while( attackType != 1 && attackType != 2) {
   attackType = Number(prompt('Attack is unknown! please choose an attack\n 1- FireBall \n 2- IceBolt'));
  }
switch ( attackType ) {
  case 1:
    damage = fireBall();
    spiderHealth -= damage;
    /* control spider is alive if it's dead set the flag to true */
    if (spiderHealth <= 0) {
      flagSpiderDead = true;
    }
    break;
  case 2:
  damage = iceBolt();
  spiderHealth -= damage;
  /* control spider is alive if it's dead set the flag to true */
  if (spiderHealth <= 0) {
    flagSpiderDead = true;
  }
  break;
} }
 logPlayerAttack.push(damage);
if (flagSpiderDead == true) {
  alert('You defeted the Spider! Congrats');
  break;
} else {
  alert('You did ' + damage + ' Damage! Spider has ' + spiderHealth + ' Health remaining');
}
damage = spiderAttack();
 logSpiderAttack.push(damage);
if (playerHealth == false) {
  alert('You have been killed');
} else {
  playerHealth -= damage;
  alert('Spider did ' + damage + ' You have ' + playerHealth + ' Health left');
} while (flagPlayerDead != true);

#2

Hi @elfraim

I think you might of got mixed up with your braces around your do while loop which is causing the error.

The syntax for a do while is:

do {
  // code that will always evaluate once...
} while (// ...then continue to evaluate until this expression is false)

EDIT: Looking at the code again, I suspect what you were trying to do is, run the code in do, then loop around then if the user provides incorrect input, loop in the while until they do. Like so:

attackType = Number(prompt(playerName + 'Choose an Attack! \n 1- FireBall \n 2- IceBolt'));

while( attackType != 1 && attackType != 2) {
  attackType = Number(prompt('Attack is unknown! please choose an attack\n 1- FireBall \n 2- IceBolt'));
}

Also, you have a break statement here:

if (flagSpiderDead == true) {
  alert('You defeted the Spider! Congrats');
  break;
}

break is only allowed to be use inside either a switch or loop (for / while).


#3

Hey, Thanks for the clear up.
But that still doesn’t answer my specific error im getting with the logPlayerAttack(damage) on line 247 or maybe im not understanding something.


#4

unexpected identifier means there’s a syntax error basically; it was displaying it at logPlayerAttack(damage) because that line is just after the do while loop where the syntax error is and codepen / JS doesn’t know exactly what line the error came from, just that there is something wrong around that point.

Sorry, should of been clearer in my original answer.


#5

Ah im still not able to correct it!
So, i went through my Do loops and they all seem to be right… At least to my untrained eyes… Can you spot the exact place where its wrong? Im just not getting it…


#6

Your use of do…while loops is invalid, see documentation

It should look something like this though I think a for loop would be easier to use in this case

   do {
    attackType = Number(prompt(playerName + 'Choose an Attack! \n 1- FireBall \n 2- IceBolt'));
    if (attackType != 1 && attackType != 2) {
        alert('Attack is unknown! please choose an attack\n 1- FireBall \n 2- IceBolt');
        return;
    }

    switch (attackType) {
        case 1:
            damage = mageFireBall();
            spiderHealth -= damage;
            break;
        case 2:
            damage = mageIceBolt();
            spiderHealth -= damage;
            break;
    }
   
    alert('You did ' + damage + ' Damage! Spider has ' + spiderHealth + ' Health remaining');
    if (spiderHealth <= 0) {
        alert('you win');
        return;
    }
    damage = spiderBasicAttack();
    logSpiderAttack.push(damage);
    playerHealth -= damage;
    alert('Spider did ' + damage + ' You have ' + playerHealth + ' Health left');
    
    if (playerHealth <= 0) {
        alert('you dead');
        return;
    }

} while (playerHealth > 0 && spiderHealth > 0);