-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (53 loc) · 1.98 KB
/
Copy pathscript.js
File metadata and controls
59 lines (53 loc) · 1.98 KB
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
const playerInput = document.getElementById("playerInput");
const addPlayerBtn = document.getElementById("addPlayerBtn");
const playerList = document.getElementById("playerList");
const truthBtn = document.getElementById("truthBtn");
const dareBtn = document.getElementById("dareBtn");
const message = document.getElementById("message");
let players = [];
let currentPlayerIndex = 0;
let questions = [];
addPlayerBtn.addEventListener("click", addPlayer);
truthBtn.addEventListener("click", showTruth);
dareBtn.addEventListener("click", showDare);
// Fetch questions from the JSON file
fetch('questions.json')
.then(response => response.json())
.then(data => {
questions = data;
})
.catch(error => {
console.error('Error loading questions:', error);
});
function addPlayer() {
const playerName = playerInput.value.trim();
if (playerName !== "" && players.length < 10) {
players.push(playerName);
const listItem = document.createElement("li");
listItem.textContent = playerName;
playerList.appendChild(listItem);
playerInput.value = "";
}
}
function showTruth() {
if (players.length === 0) {
message.textContent = "Please add at least one player to start the game.";
} else {
const randomIndex = Math.floor(Math.random() * questions.length);
const currentPlayer = players[currentPlayerIndex];
const currentQuestion = questions[randomIndex].question;
message.textContent = `${currentPlayer}, ${currentQuestion}`;
currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
}
}
function showDare() {
if (players.length === 0) {
message.textContent = "Please add at least one player to start the game.";
} else {
const randomIndex = Math.floor(Math.random() * questions.length);
const currentPlayer = players[currentPlayerIndex];
const currentQuestion = questions[randomIndex].question;
message.textContent = `${currentPlayer}, ${currentQuestion}`;
currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
}
}