Team Stats - Added some extras but cn't get them to work

const team = { _games: [{opponent: "Botherington WMC", teamPoints: 17, opponentPoints: 12}, {opponent: "Pissville WI", teamPoints: 14, opponentPoints: 22}, {opponent: "Some unruly urchins", teamPoints: 31, opponentPoints: 9}], _players: [{firstName: "Alex", lastName: "Gordon", age: 28}, {firstName: "Lig", lastName: "Boonig", age: 32}, {firstName: "Jesse", lastName: "Single", age: 35}], get games () { return this._games }, get players () { return this._players }, addPlayer (firstName, lastName, age) { let player = { firstName: firstName, lastName: lastName, age: age, } this._players.push(player) }, addGame (opponent, teamPoints, opponentPoints) { let game = { opponent: opponent, teamPoints: teamPoints, opponentPoints: opponentPoints, } this._games.push(game) } } team.addPlayer("Steph", "Curry", 28) team.addPlayer("Lisa", "Leslie", 44) team.addPlayer("Bugs", "Bunny", 76) team.addGame("Blithely cum Wye", 36, 20) const results = () => { let winLose = "" for (let x = 0; x < team._games.length; x++) { if (team._games[x].teamPoints < team._games[x].opponentPoints) { let winLose = "lost" } if (team._games[x].teamPoints > team._games[x].opponentPoints) { let winLose = "won" } else { let winLose = "drew" }; console.log(`You ${winLose} against ${team._games[x].opponent}. You scored ${team._games[x].teamPoints} and they scored ${team._games[x].opponentPoints}.`) } } results()

The above code is the normal Team Stats project plus an arrow function called “results” that I added at the end. When called, “results” is supposed to print “You [won/lost/drew/] against [team]. You scored [points] and they scored [points].” for all available games. However there seems to be an issue with reassigning the winLose variable as what’s printed is actually “You [BLANK] against [team]. You scored [points] and they scored [points].”. The winLose variable is remaining as the empty string that I originally defined it as.

I’ve been staring at this for an hour or so now, any help would be much appreciated, thanks!

Don’t initialize the winlose variable in the if blocks.
You already initialized it at the top of the function scope.
Just remove the let keyword in order to reassign it and it will log.

Amazing, thanks!

There’s another problem now though :disappointed_relieved:

The output is:
You won against Botherington WMC. You scored 17 and they scored 12.
You drew against Pissville WI. You scored 14 and they scored 22.
You won against Some unruly urchins. You scored 31 and they scored 9.
You won against Blithely cum Wye. You scored 36 and they scored 20.

For some reason winLose isn’t accepting “lost” when teamPoints is less than opponentPoints. Any ideas?

Yes. The variable will be reassigned if team._games[x].teamPoints > team._games[x].opponentPoints is false. Because you have two independent if/else blocks.

if(true) {
winLose = "win"
}
console.log(winLose) // "win"
if(1===2) {
winLose = "lose"
}
else {
winLose = "drew"
}
console.log(winLose) // "drew"
hint

use

if(){
// some code
}
else if(){
// some code
}
else {
// some code
}

Ah, of course! Both silly mistakes. Thanks for your help.

1 Like