create dice game javascript
JavaScript
<script>
// Player name
var player1 = "Player 1";
var player2 = "Player 2";
// Function to change the player name
function editNames() {
player1 = prompt("Change Player1 name");
player2 = prompt("Change player2 name");
document.querySelector("p.Player1").innerHTML = player1;
document.querySelector("p.Player2").innerHTML = player2;
}
// Function to roll the dice
function rollTheDice() {
setTimeout(function () {
var randomNumber1 = Math.floor(Math.random() * 6) + 1;
var randomNumber2 = Math.floor(Math.random() * 6) + 1;
document.querySelector(".img1").setAttribute("src",
"dice" + randomNumber1 + ".png");
document.querySelector(".img2").setAttribute("src",
"dice" + randomNumber2 + ".png");
if (randomNumber1 === randomNumber2) {
document.querySelector("h1").innerHTML = "Draw!";
}
else if (randomNumber1 < randomNumber2) {
document.querySelector("h1").innerHTML
= (player2 + " WINS!");
}
else {
document.querySelector("h1").innerHTML
= (player1 + " WINS!");
}
}, 2500);
}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content
="width=device-width, initial-scale=1.0">
<title>Dice Game</title>
<style>
.container {
width: 70%;
margin: auto;
text-align: center;
}
.dice {
text-align: center;
display: inline-block;
margin: 10px;
}
body {
background-color: #042f4b;
margin: 0;
}
h1 {
margin: 30px;
font-family: "Lobster", cursive;
text-shadow: 5px 0 #232931;
font-size: 4.5rem;
color: #4ecca3;
text-align: center;
}
p {
font-size: 2rem;
color: #4ecca3;
font-family: "Indie Flower", cursive;
}
img {
width: 100%;
}
.bottom {
padding-top: 30px;
}
.butn {
background: #4ecca3;
font-family: "Indie Flower", cursive;
border-radius: 7px;
color: #ffff;
font-size: 30px;
padding: 16px 25px 16px 25px;
text-decoration: none;
}
.butn:hover {
background: #232931;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Let's Play</h1>
<div class="dice">
<p class="Player1">Player 1</p>
<img class="img1" src="dice6.png">
</div>
<div class="dice">
<p class="Player2">Player 2</p>
<img class="img2" src="dice6.png">
</div>
</div>
<div class="container bottom">
<button type="button" class="butn"
onClick="rollTheDice()">
Roll the Dice
</button>
</div>
<div class="container bottom">
<button type="button" class="butn"
onClick="editNames()">
Edit Names
</button>
</div>
<script>
// Player name
var player1 = "Player 1";
var player2 = "Player 2";
// Function to change the player name
function editNames() {
player1 = prompt("Change Player1 name");
player2 = prompt("Change player2 name");
document.querySelector("p.Player1")
.innerHTML = player1;
document.querySelector("p.Player2")
.innerHTML = player2;
}
// Function to roll the dice
function rollTheDice() {
setTimeout(function () {
var randomNumber1 = Math.floor(Math.random() * 6) + 1;
var randomNumber2 = Math.floor(Math.random() * 6) + 1;
document.querySelector(".img1").setAttribute("src",
"dice" + randomNumber1 + ".png");
document.querySelector(".img2").setAttribute("src",
"dice" + randomNumber2 + ".png");
if (randomNumber1 === randomNumber2) {
document.querySelector("h1").innerHTML = "Draw!";
}
else if (randomNumber1 < randomNumber2) {
document.querySelector("h1").innerHTML
= (player2 + " WINS!");
}
else {
document.querySelector("h1").innerHTML
= (player1 + " WINS!");
}
}, 2500);
}
</script>
</body>
</html>
Also in JavaScript:
- Title
- javascript base64 decode
- Category
- JavaScript
- Title
- javascript ajouter une donnée à une list
- Category
- JavaScript
- Title
- array shift javascript
- Category
- JavaScript
- Title
- Encoding and Decoding Base64 Strings in Node.js
- Category
- JavaScript
- Title
- allow cross origin node
- Category
- JavaScript
- Title
- call javascript function after page load complete
- Category
- JavaScript
- Title
- how to access array datat in class component react
- Category
- JavaScript
- Title
- get element by class
- Category
- JavaScript
- Title
- deprecationwarning: mongoose
- Category
- JavaScript
- Title
- javascript check if empty
- Category
- JavaScript
- Title
- fivem trigger javascript function
- Category
- JavaScript
- Title
- increase-memory-limit not working node
- Category
- JavaScript
- Title
- javascript check if element has class
- Category
- JavaScript
- Title
- background-image url not working
- Category
- JavaScript
- Title
- array of objects javascript
- Category
- JavaScript
- Title
- javascript Arranging Coins
- Category
- JavaScript
- Title
- how to find length of array in javascript without using length method
- Category
- JavaScript
- Title
- define all jsdoc typedef in a seperate file
- Category
- JavaScript
- Title
- generate random ip address javascript
- Category
- JavaScript
- Title
- how to add class to button in javascript
- Category
- JavaScript
- Title
- angular @Input()
- Category
- JavaScript
- Title
- how to check if 2 sprites are touching js
- Category
- JavaScript
- Title
- how to send a command in js
- Category
- JavaScript
- Title
- window location in react
- Category
- JavaScript
- Title
- how to calculate each row with on change table <td><input> jquery
- Category
- JavaScript
- Title
- email regex javscript
- Category
- JavaScript
- Title
- javascript check if value in array
- Category
- JavaScript
- Title
- 'react-scripts' n’est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes.
- Category
- JavaScript
- Title
- get id of element javascript
- Category
- JavaScript
- Title
- document ready js
- Category
- JavaScript
- Title
- angular delete from array by name
- Category
- JavaScript
- Title
- javascript base64 encode string
- Category
- JavaScript
- Title
- ExoPlayer with auto linking react native
- Category
- JavaScript
- Title
- express validator always return error
- Category
- JavaScript
- Title
- go to another page using javascript
- Category
- JavaScript
- Title
- how to remove an object from array in react native
- Category
- JavaScript
- Title
- checked unchecked through js
- Category
- JavaScript
- Title
- how to write a program that shows a random number between 1 and 100 in your browser
- Category
- JavaScript
- Title
- array javascript
- Category
- JavaScript
- Title
- javascript class click event
- Category
- JavaScript
- Title
- change title react
- Category
- JavaScript
- Title
- binary search in js
- Category
- JavaScript
- Title
- javascript check if is array
- Category
- JavaScript
- Title
- detox scroll to element
- Category
- JavaScript
- Title
- how to create response time router node js
- Category
- JavaScript
- Title
- functional component react
- Category
- JavaScript
- Title
- iis express gzip
- Category
- JavaScript
- Title
- Hoisting in JavaScript MDN
- Category
- JavaScript
- Title
- how to log to the console javascript
- Category
- JavaScript
- Title
- array every javascript
- Category
- JavaScript
- Title
- how to stringify json in js
- Category
- JavaScript
- Title
- get contents between tags javascript
- Category
- JavaScript
- Title
- how to multiple loop in javascript using for
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- Javascript check if undefinded
- Category
- JavaScript
- Title
- javascript check if objects are equal
- Category
- JavaScript
- Title
- array data structure javascipt interview questions
- Category
- JavaScript
- Title
- click doesn't work on ajax loaded data
- Category
- JavaScript
- Title
- conditional rendering with radio buttons
- Category
- JavaScript
- Title
- console trace js
- Category
- JavaScript
- Title
- array of in javascript
- Category
- JavaScript
- Title
- grepper not showing
- Category
- JavaScript
- Title
- how to create 3d gallery using javascript
- Category
- JavaScript
- Title
- ascii to char in javascript
- Category
- JavaScript
- Title
- angular trigger input change
- Category
- JavaScript
- Title
- how to name a javascript variable
- Category
- JavaScript
- Title
- how to display an image on text click using jQuery
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- convert data into json format in javascript
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- en eternal gloden braid
- Category
- JavaScript
- Title
- invalid left-hand side in assignment
- Category
- JavaScript
- Title
- $pull mongoose
- Category
- JavaScript
- Title
- fix a navbar to top of page js
- Category
- JavaScript
- Title
- insert into specific array index that has a value existing on that index
- Category
- JavaScript
- Title
- check array values equal js
- Category
- JavaScript
- Title
- get last char javascript
- Category
- JavaScript
- Title
- how to loop audio in js
- Category
- JavaScript
- Title
- change width in js
- Category
- JavaScript
- Title
- double question mark javascript
- Category
- JavaScript
- Title
- javascript check if string contains character
- Category
- JavaScript
- Title
- fill array with values javascript
- Category
- JavaScript
- Title
- hackerrank plus minus javascript
- Category
- JavaScript
- Title
- declare function javascript
- Category
- JavaScript
- Title
- Input Text to Enable Button with jQuery when all fields
- Category
- JavaScript
- Title
- electron specify minimum size
- Category
- JavaScript
- Title
- discord.js start code
- Category
- JavaScript
- Title
- angular form validation
- Category
- JavaScript
- Title
- brute force search javascript
- Category
- JavaScript
- Title
- create element javascript with class
- Category
- JavaScript
- Title
- checking if a character is an alphabet in js
- Category
- JavaScript
- Title
- form validation in react functional component
- Category
- JavaScript
- Title
- javascript atualize array
- Category
- JavaScript
- Title
- devexpress custom text column field
- Category
- JavaScript
- Title
- install react native
- Category
- JavaScript
- Title
- conditional jsx property
- Category
- JavaScript
- Title
- how to make a property important in javascript
- Category
- JavaScript
- Title
- how to check if item is in list js
- Category
- JavaScript
- Title
- check undefined object javascript one liner set to emtpy
- Category
- JavaScript
- Title
- javascript appendchild at index
- Category
- JavaScript