Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Rock_paper_scissors/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Rock,Paper and Scissor</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<script src="./main.js"></script>

</head>

<body>
<div class="container">
<div class="page-header">
<center>
<h1>Rock, Paper and Scissor</h1>
</center>
</div>
<div class="row">
<div class="col-sm-6">
<div class="h2" id="print_result">Let's Play</div>
</div>
<div class="col-sm-6">
<h3><center>Result</center></h3>
<table class="table">
<thead>
<tr>
<td>Win</td>
<td>Lose</td>
<td>Tie</td>
</tr>
</thead>
<tbody>
<tr>
<td id="win">0</td>
<td id="lose">0</td>
<td id="tie">0</td>
</tr>
</tbody>
</table>
</div>
</div>
<center>
<input type="button" id="button_rock" value="Rock" onclick="ABC('rock')" />
<input type="button" id="button_paper" value="Paper" onclick="ABC('paper')" />
<input type="button" id="button_scissor" value="scissor" onclick="ABC('scissor')" />
</center>
</div>
</body>

</html>
28 changes: 28 additions & 0 deletions Rock_paper_scissors/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const arr = ['rock', 'paper', 'scissor'];
let wins = 0;
let losses = 0;
let ties = 0;

function ABC(value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use a better naming convention that represent the property of you code:
instead of ABC , use gameState/ updateStatus ... etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry sir next time i will take care of this

var rand = arr[Math.floor(Math.random() * 3)];
document.getElementById("print_result").innerHTML = `You choose ${value}, computer choose ${rand}.<br/><br/>`;

if (value == 'rock' && rand == "scissor" ||
value == "paper" && rand == "rock" ||
value == "scissor" && rand == "paper") {
document.getElementById("win").innerHTML = wins += 1;
document.getElementById("print_result").innerHTML += `You Win`;

} else if (rand == 'rock' && value == "scissor" ||
rand == "paper" && value == "rock" ||
rand == "scissor" && value == "paper") {
document.getElementById("lose").innerHTML = losses += 1;
document.getElementById("print_result").innerHTML += `Computer Win`;

} else {
document.getElementById("tie").innerHTML = ties += 1;
document.getElementById("print_result").innerHTML += `it's a Tie`;

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using repetitive piece of code, store it in one variable and use it, here
update the code using ternary operator instead of if..else
what i can see is there are only three cases when user can win:
you_win = ( (you === "rock") && (opponent === "scissor") ||
(you === "paper") && (opponent === "rock") ||
(you === "scissor") && (opponent === "rock") ) // returns true for your win cases only.
.
.
so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;


}