[英]Can't find where the error is in this JavaScript code. Not very familliar with 'class' syntax
這是代碼。 這是一個隨機陌生人的任務,因為他在IG上看到了我的帖子,因此要求我為他解決。
// Player class
class Player {
constructor(name, strength = 2, weapons) {
this.name = name;
this.health = 10;
this.strength = strength;
this.weapons = [...weapons];
}
applyDamage(int) {
this.health -= int;
}
isAlive() {
return this.health > 0;
}
attackWith() {
let randomNum = Math.floor(Math.random() * 8);
return this.weapons[randomNum];
}
}
// Weapon class
class Weapon {
constructor(name) {
this.name = name;
this.damage = Math.ceil(Math.random() * 5);
}
attack(player, enemy) {
if (player.isAlive() && player.isAlive()) {
let dmg = player.strength * this.damage;
enemy.applyDamage(dmg);
if (!enemy.isAlive()) {
return;
} else {
enemy.attack(player);
}
}
}
}
// Enemy class
class Enemy {
constructor(name = "Enemy", health = 5, strength = 2) {
this.name = name;
this.health = health;
this.strength = strength;
}
applyDamage(int) {
this.health -= int;
}
isAlive() {
return this.health > 0;
}
attack(player) {
player.applyDamage(this.strength);
}
}
// BattleSimulation class
class BattleSimulation {
constructor() {
this.players = [];
this.enemies = [];
}
createEnemies() {
for (let i = 0; i < 20; i += 1) {
this.enemies[i] = new Enemy();
}
}
createPlayers() {
// Weapons
let pencil = new Weapon("Pencil");
let book = new Weapon("Book");
let screwdriver = new Weapon("Screwdriver");
let theOneRing = new Weapon("Sauron's Ring");
let mustardGass = new Weapon("Mustard Gass");
let bigBoy = new Weapon("A Nuke");
let love = new Weapon("Power of Love");
let theForce = new Weapon("The Force");
let weaponsCache = [
pencil,
book,
screwdriver,
theOneRing,
mustardGass,
bigBoy,
love,
theForce
];
// Players
let luke = new Player("Luke", 5, weaponsCache);
let baldingCoder = new Player("DraciVik", 10, weaponsCache);
let trump = new Player("Trump", 1, weaponsCache);
let kikiMakarena = new Player("Kiki Makarena", 5, weaponsCache);
let johnWick = new Player("John Wick", 2, weaponsCache);
this.players = [luke, baldingCoder, trump, kikiMakarena, johnWick];
}
run() {
console.log("Simulating Battle");
this.createEnemies();
this.createPlayers();
while (this.players.length !== 0 || this.enemies.length !== 0) {
let randomPlayerIndex = Math.floor(Math.random() * this.players.length);
let randomPlayer = this.players[randomPlayerIndex];
let randomEnemyIndex = Math.floor(Math.random() * this.enemies.length);
let randomEnemy = this.enemies[randomEnemyIndex];
let weapon = randomPlayer.attackWith();
weapon.attack(randomPlayer, randomEnemy);
if (!randomPlayer.isAlive()) {
this.players.splice(randomPlayerIndex, 1);
}
if (!randomEnemy.isAlive()) {
this.enemies.splice(randomEnemyIndex, 1);
}
}
console.log(this.players);
if (this.players.length > 0) {
return "Congratulations, you have defeated Scarlet Byle";
}
return "Sorry, Scarlet Byle has defeated you and conquered the free world";
}
}
let battle = new BattleSimulation();
battle.run();
任何人都可以看到錯誤在哪里? 我收到返回錯誤'enemy.applyDamage(dmg)'
未定義。
這個錯誤是什么,我需要更多的寫作而不僅僅是代碼? 我應該給一些信件發送垃圾郵件嗎?
這里的錯誤實際上只是在您的while
循環條件下:
while(this.players.length !== 0 || this.enemies.length !== 0)
您的條件是在至少有一名玩家或至少有一名敵人時循環。 所以只要其中一個數組不為空,它就會繼續循環。
但是當你第一次創建this.players
和this.enemies
,它們的大小不同。 然后,當您從每個數組中刪除一個條目時,最終其中一個數組在另一個數組之前為空。
那么你的代碼有var randomEnemyIndex = Math.floor(Math.random() * this.enemies.length);
當數組為空時,它將評估為 0。 當你執行this.enemies[0]
,它返回 undefined。 當undefined
像weapon.attack(randomPlayer, undefined)
一樣傳遞給weapon.attack
,它會嘗試在undefined
上調用applyDamage(dmg)
,這會引發您的異常。
如果您修改代碼以具有以下控制台日志,您將看到問題:
while (this.players.length !== 0 || this.enemies.length !== 0) {
console.log("PLAYERS: " + this.players.length.toString());
console.log("ENEMIES: " + this.enemies.length.toString());
...
你會看見:
'Simulating Battle'
'PLAYERS: 5'
'ENEMIES: 20'
'PLAYERS: 5'
'ENEMIES: 20'
'PLAYERS: 5'
[...]
'PLAYERS: 4'
'ENEMIES: 1'
'PLAYERS: 4'
'ENEMIES: 0'
error: Uncaught TypeError: Cannot read property 'applyDamage' of undefined
因此,要解決此問題,您需要將條件更改為:
while (this.players.length !== 0 && this.enemies.length !== 0) {
或者您需要以相同的大小啟動兩個陣列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.