Thank you so much for your help. But thankfully I actually figured it out myself. The only thing is, now I have a new problem lol. Now I need to figure out how to show the scores and increment it when either player wins🤔
In JavaScript, there is a concept of truthy and falsy values. When used in place of booleans, all values except for false, 0, "" (empty string), null, undefined, and NaN will evaluate as true, because they are considered truthy.
In your code
if(playerWin){playerScoreCount++;}
the playerWin variable always evaluates to true because you've defined it as "You Win!", which is a non-empty string, which in turn is truthy. Same goes for the computerWin variable.
You should have boolean variables which are reset to false each round, and then one of them is set to true in the if-blocks, where you compare the inputs to determine who wins.
As to how to show the score, do it in the same way as you update the weapons each round.
Yep, that's the way to do it - first get the code working, then clean it up (also known as refactoring).
When refactoring, consider incrementing the score and updating the UI in one place only. That way, if you need to change anything about the score logic or the HTML elements change, you'll need to change it once, instead of 6 times.
This may not be as important for this throwaway project, but it's a well-known software development principle called DRY (Don't Repeat Yourself). Eliminating duplicate code leads to a codebase which is more readable and much less error-prone.
Anyways, congrats on figuring it out and good luck with your next challenges!
Well for the starters the function playerWeapon and selectedWeapons should return something, so that your vars can hold the value they return.
Why its returning "Tie" is because
Tip: create local variable inside your function, do not take them as argument and modify them (this is mutation and its bad)
Thank you so much for your help. But thankfully I actually figured it out myself. The only thing is, now I have a new problem lol. Now I need to figure out how to show the scores and increment it when either player wins🤔
In JavaScript, there is a concept of truthy and falsy values. When used in place of booleans, all values except for
false
,0
,""
(empty string),null
,undefined
, andNaN
will evaluate astrue
, because they are considered truthy.In your code
the
playerWin
variable always evaluates totrue
because you've defined it as "You Win!", which is a non-empty string, which in turn is truthy. Same goes for thecomputerWin
variable.You should have boolean variables which are reset to
false
each round, and then one of them is set totrue
in the if-blocks, where you compare the inputs to determine who wins.As to how to show the score, do it in the same way as you update the weapons each round.
Hope this helps!
Thank you for the help. But I've finally figured it out myself. Here is the solution I came up with:
Eventually I will remove all the console.log's and clean up the code a bit, once I have everything working the way it should.
Yep, that's the way to do it - first get the code working, then clean it up (also known as refactoring).
When refactoring, consider incrementing the score and updating the UI in one place only. That way, if you need to change anything about the score logic or the HTML elements change, you'll need to change it once, instead of 6 times.
This may not be as important for this throwaway project, but it's a well-known software development principle called DRY (Don't Repeat Yourself). Eliminating duplicate code leads to a codebase which is more readable and much less error-prone.
Anyways, congrats on figuring it out and good luck with your next challenges!
Thank you so much! I've finished my project and here's the end result
And yes I am very aware of DRY. I try to not have duplicate code.
Great job!
Thanks!