-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (43 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// JavaScript Document
function namePlayers() {
var player1 = document.getElementById("player1").value;
var player2 = document.getElementById("player2").value;
var player1 = player1.toUpperCase();
var player2 = player2.toUpperCase();
document.querySelectorAll("p")[0].innerHTML = player1;
document.querySelectorAll("p")[1].innerHTML = player2;
var randomNumber1 = Math.floor(Math.random() * 6) + 1;
var randomDiceImage = "dice" + randomNumber1 + ".png";
var image1 = document.querySelectorAll("img")[0];
image1.setAttribute("src", randomDiceImage);
var randomNumber2 = Math.floor(Math.random() * 6) + 1;
var randomDiceImage = "dice" + randomNumber2 + ".png";
var image1 = document.querySelectorAll("img")[1];
image1.setAttribute("src", randomDiceImage);
if (randomNumber1 > randomNumber2) {
document.querySelectorAll("h1")[0].innerHTML = (player1 + " WINS 🚩");
} else if (randomNumber2 > randomNumber1) {
document.querySelectorAll("h1")[0].innerHTML = (player2 + " WINS 🚩");
} else if (randomNumber2 === randomNumber1){
document.querySelectorAll("h1")[0].innerHTML = ("Draw 🎌");
};
}
var form = document.getElementById("myForm");
function handleForm(event) { event.preventDefault(); }
form.addEventListener('submit', handleForm);
window.onload = function() {
// If sessionStorage is storing default values (ex. name), exit the function and do not restore data
if (sessionStorage.getItem('name') == "player1") {
return;
}
// If values are not blank, restore them to the fields
var name = sessionStorage.getItem('name');
if (name !== null) $('#player1').val(name);
var email = sessionStorage.getItem('email');
if (email !== null) $('#player2').val(email);
}
// Before refreshing the page, save the form data to sessionStorage
window.onbeforeunload = function() {
sessionStorage.setItem("name", $('#player1').val());
sessionStorage.setItem("email", $('#player2').val());
}