From c4e98293abff3ff30f24f2aaad9fed61c08a9e7f Mon Sep 17 00:00:00 2001 From: Userbit <34487074+Userbit@users.noreply.github.com> Date: Thu, 13 Feb 2020 20:07:57 +0300 Subject: [PATCH] Fixes for the sake of clarity --- composite/composite.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/composite/composite.ts b/composite/composite.ts index c2cb181..10cde19 100644 --- a/composite/composite.ts +++ b/composite/composite.ts @@ -20,26 +20,21 @@ class Team implements ArmyObject { } addSoldier(newSoldier: ArmyObject) { - const soldiers = this._soldiers.filter((soldier: ArmyObject, index) => { - return soldier.seq === newSoldier.seq; - }) - if (soldiers.length < 1) { - console.log(`Soldier: ${newSoldier.seq} comes in ${this.seq}`); - this._soldiers.push(newSoldier); - } else { + const hasBeen = this._soldiers.includes(deadSoldier); + if (hasBeen) { console.log('The soldier is already in the team'); + return; } + + console.log(`Soldier: ${newSoldier.seq} comes in ${this.seq}`); + this._soldiers.push(newSoldier); } soldierGone(deadSoldier: ArmyObject) { - const deads = this._soldiers.map((soldier: ArmyObject, index) => { - if (soldier.seq === deadSoldier.seq) { - return index; - } - }) - if (deads.length > 0) { + const deadIndex = this._soldiers.indexOf(deadSoldier); + if (deadIndex > -1) { console.log(`Soldier: ${deadSoldier.seq} died in the fight`); - this._soldiers.slice(deads[0], 1); + this._soldiers.splice(deadIndex, 1); } else { console.log('No one dies'); }