Classes and syntax

Hi guys Im needing help with the balloonAttack - JS classes challenge ;
https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-22-javascript-syntax-part-iii/modules/wdcp-22-practice-javascript-syntax-classes/articles/fecp-javascript-practice-classes

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
const balloonAttack = function (p1, p2) {

  const player1Shots = p1.hitsPerMinute * 10;
  const player2Shots = p2.hitsPerMinute * 10;

  p1.balloonCount = p1.balloonCount - player2Shots;

  p2.balloonCount = p2.balloonCount - player1Shots;

  if(p1.balloonCount > p2.balloonCount) {
    return 'p1'; 
  } else if(p2.balloonCount > p1.balloonCount) {
    return 'p2';
  } else {
    return 'tie';
  }
    };


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

const p1 = new Player('p1', 11);
const p2 = new Player('p2', 12);

console.log(balloonAttack(p1, p2));

When I click check answer I keep getting -: Tested with balloonAttack(p1, p2) where p1.hitsPerMinute = 11 and p2.hitsPerMinute = 12 . Result was not 'p2'

Even though my tests are saying it is p2, also the see solution is not working on the task either so makes it hard. Would really appreciate any help.

kind Regards,

Jacksen

1 Like

My solution for context:

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) => {
  let balloonsLeftp1 = p1.balloonCount - p2.hitsPerMinute*10
  let balloonsLeftp2 = p2.balloonCount - p1.hitsPerMinute*10
  if (balloonsLeftp1 === balloonsLeftp2) return 'Tie'
  if (balloonsLeftp1 > balloonsLeftp2) return p1.name
  return p2.name
}

const p1 = new Player('p1', 11);
const p2 = new Player('p2', 12);
 
console.log(balloonAttack(p1, p2));

Maybe it got caught up when you returned a hard-coded ‘p2’ instead of the name property of the p2 object.

2 Likes

Thank you so much, that’s exactly what it was ! Seems obvious now but in the moment when I was seeing the correct output I was to focused on that and not how I achieved the output. Thanks again, Really appreciate your help !

Maybe it got caught up when you returned a hard-coded ‘p2’ instead of the name property of the p2 object.

Thanks! I did forgot to put the .name and kept getting the error message.

function balloonAttack(one, two) {
if (one.hitsPerMinute === two.hitsPerMinute) {
return ‘Tie’;
} else if (one.hitsPerMinute > two.hitsPerMinute) {
return one.name;
} else {
return two.name;
}
}

1 Like