FAQ: Classes - Review: Classes

You’re welcome! Anytime you see a ‘something’ unexpected here, the first place to look is at what comes before the ‘something’. Happy Coding!

1 Like

Hi, I’m going through the Practice JavaScript Syntax: Classes code challenges and my code doesn’t seem to be fulfilling the conditions for it.

Here’s the challenge:

A video game consists of two players floating using 100 helium balloons. The players shoot pellets at each other’s balloons and after 10 minutes the player with the most balloons left wins.

Write a game function balloonAttack that takes two Player instances, calculates the balloons left for each player after 10 minutes (using the hitsPerMinute property) and returns the name of the winner. If the result is a tie, return the string 'Tie' .


I’m getting an error message saying:
Tested with balloonAttack(p1, p2) where p1.hitsPerMinute = 5 and p2.hitsPerMinute = 2 . Result was not 'p1'

Despite getting this error, the code below logs p1 to the console. What am I doing wrong?

Here’s my code:

class Player {
  constructor(name, hitsPerMinute) {
    this.name = name;
    this.hitsPerMinute = hitsPerMinute;
    this.balloonCount = 100;
  }

  status() {
    console.log(`Player: ${this.name} -- Balloons Left: ${this.balloonCount}`)
  }
}

// Write function below
function balloonAttack(a, b) {
  let aHits = a.hitsPerMinute*10;
  let bHits = b.hitsPerMinute*10;

  a.balloonCount = a.balloonCount-bHits;
  b.balloonCount = b.balloonCount-aHits;

  a.status();
  b.status();

  if (a.balloonCount > b.balloonCount) {
    console.log(a.name);
  }
  else if (b.balloonCount > a.balloonCount) {
    console.log(b.name);
  }
  else if (a.balloonCount === b.balloonCount) {
    console.log("It's a tie!");
  }
}

const p1 = new Player('p1', 5);
const p2 = new Player('p2', 2);
 
balloonAttack(p1, p2);

Could you provide the exercise url?

Hi, here’s the exercise URL:
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-iii/modules/fecp-practice-javascript-syntax-classes/articles/fecp-javascript-practice-classes

The exercise says:

Write a game function balloonAttack that takes two Player instances, calculates the balloons left for each player after 10 minutes (using the hitsPerMinute property) and returns the name of the winner. If the result is a tie, return the string 'Tie' .

your balloonAttack function does not return anything. You log the result inside the function, but do not return it

Ohhh I see! That makes sense, thank you very much!

Hello. I’m finding myself stuck with this exercise. My code is:

class Player {

  constructor(name, hitsPerMinute) {

    this.name = name;

    this.hitsPerMinute = hitsPerMinute;

    this.balloonCount = 100;

  }

  status() {

    console.log(`Player: ${this.name} -- Balllons Left: ${this.balloonCount}`)

  }

}

// Write function below

const balloonAttack = (p1, p2) => {

  

if (p1.hitsPerMinute > p2.hitsPerMinute){

  return `The winner is:${p1.name}`

} else if(p2.hitsPerMinute > p1.hitsPerMinute){

  return `The winner is: ${p2.name}`

} else {

  return `Tie`

}

}

Can someone explain me what am I doing wrong? Please.
Thanks

Which exercise is this? This is the FAQ for a review exercise. Running your code does not result in any error, butt that seems because no functions are executed.

Can you describe the problem you are facing?

p1 and p2 look like they need to be Player instances.

const him = new Player('Him', 10)
const her = new Player('Her', 20)

Then, as @stetim94 advised, the balloonAttack() function needs to be invoked.

console.log(balloonAttack(him, her))

Hello mtf,

First of all thanks for your answer. This is the link to the exercise https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-iii/modules/fecp-practice-javascript-syntax-classes/articles/fecp-javascript-practice-classes
Somehow it still doesn’t work. When I hit the “Check answer” button the program returns this: Tested with balloonAttack(p1, p2) where p1.hitsPerMinute = 5 and p2.hitsPerMinute = 2 . Result was not 'p1' : expected ‘The winner is

And that’s actually what the console prints wwhen I hit the “run” button. So I don’t know what I am doing wrong.
Thanks.
David

Hello Stetim94

I have answered what is the problem on my coment before when answering mtf. I really don’t get what I am doing wrong hehehe. I’m just a beginner and maybe I’m missing something, also I am spanish and might be something I don’t understand or reading wrong.

Thanks.
David

the exercise very likely does a string comparison, and is expected the name of the player only, no extra (string) content

Oh man!! hahahahahaha That’s exactly what happened I’m such a dumb hahahahaha thank you very much!