Skip to content

Commit

Permalink
add coloring to output text depending on outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
oddtO committed Feb 3, 2023
1 parent 41689b3 commit 151981e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
31 changes: 23 additions & 8 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
let playerScore = 0;
let computerScore = 0;

let lastOutcome = '';
let lastOutcome = {resultString: '', hasWon: null};

let playerScoreElement = document.querySelector('#playerScore');
let computerScoreElement = document.querySelector('#computerScore');
Expand All @@ -100,7 +100,24 @@
playerScoreElement.textContent = playerScore;
computerScoreElement.textContent = computerScore;

choiceOutcomeElement.textContent = lastOutcome;
choiceOutcomeElement.textContent = lastOutcome.resultString;

switch(lastOutcome.hasWon)
{

case null:
choiceOutcomeElement.className = 'draw';
break;
case true:
choiceOutcomeElement.className = 'won';
break;
case false:
choiceOutcomeElement.className = 'lost';
break;

}



}

Expand All @@ -109,10 +126,7 @@
{
let computerChoice = getComputerChoice();

let outcome = playRound(playerChoice, computerChoice);

lastOutcome = outcome.resultString;
return outcome.hasWon;
lastOutcome = playRound(playerChoice, computerChoice);

}

Expand All @@ -127,7 +141,8 @@
{
playerScore = computerScore = 0;
}
switch(startRound(event.target.dataset.choice))
startRound(event.target.dataset.choice);
switch(lastOutcome.hasWon)
{

case null:
Expand All @@ -143,7 +158,7 @@

if(isScoreBreached())
{
lastOutcome = (playerScore > computerScore ? "Player" : "Computer") + " wins!";
lastOutcome.resultString = (playerScore > computerScore ? "Player" : "Computer") + " wins!";
}

render();
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>Rock Paper Scissors</h1>
<div id="scores">Score: <div><span id="playerScore">0</span> : <span id="computerScore">0</span></div></div>

<output id="choiceOutcome" for="playerChoiceButtons">
Sample output!
Pick something
</output>


Expand Down
20 changes: 20 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ body h1
font-size: 30px;
}

#choiceOutcome[class]
{
font-weight: 900;
}

.lost#choiceOutcome
{
color: red;
}

.won#choiceOutcome
{
color: green;
}

.draw#choiceOutcome
{
color: orange;
}


#playerChoiceButtons button
{
Expand Down

0 comments on commit 151981e

Please sign in to comment.