I’m just starting to learn JavaScript.
I’ve been wracking my brain and searching online for possible mistakes I could have made, but I’m just not seeing where I went wrong. I think I’ve been looking at it too long.
I just learned about classes, so this is inside a method called fire. I’ve attached the fire method to a fire button. I ran the method in the button with the () and it ran on it’s own once. I removed the () and it doesn’t run at all.
const fireBtn = document.getElementById("fire");
fireBtn.textContent = "FIRE!";
class Ship {
constructor(hull, firepower, accuracy) {
this.hull = hull;
this.firepower = firepower;
this.accuracy = accuracy;
}
fire() {
if (this === alienShip) {
if (ussHelloWorld.hull > 0) {
if (Math.random() < this.accuracy) {
console.log("USS Hello World has been hit!");
ussHelloWorld.hull -= this.firepower;
} else {
console.log("Missed USS Hello World target");
}
} else {
console.log("You Loose");
}
}
if (this === ussHelloWorld) {
if (this.hull > 0) {
if (Math.random() < this.accuracy) {
console.log("Alien has been hit!");
alienShip.hull -= this.firepower;
if (alienShip.hull <= 0) {
console.log("Ship has gone down");
alienShip = generateShip();
}
} else {
console.log("Missed alien target");
}
} else {
console.log("You Loose");
}
}
}
}
fireBtn.addEventListener("click", ussHelloWorld.fire);
Any help is appreciated.