JSFiddle - React, Tailwind, and code Playground
by thomasa88
HTML
Antal spelare: <input type="number" id="playerCount" value="4" style="width: 2em;">
<h2 id="player">
</h2>
<h1 id="word" style="height: 1.5em;">
</h1>
<input id="next" value="Nästa" type="button" onclick="next()" style="display: none;">
<br>
<br>
<br>
<input id="newgame" value="Nytt spel" type="button" onclick="newGame()">
JavaScript
WORDS = null
word = '';
playerCount = 4;
fakePlayer = 0;
currentPlayer = 0;
showWord = true;
nextBtn = document.getElementById('next');
wordDiv = document.getElementById('word');
playerDiv = document.getElementById('player');
countInput = document.getElementById('playerCount');
function randInt(max) {
return Math.floor(Math.random() * max);
}
function next() {
nextBtn.value = 'hej';
playerDiv.innerText = `Spelare ${currentPlayer + 1}`;
if (showWord) {
if (currentPlayer == fakePlayer) {
wordDiv.innerText = 'Inget ord';
} else {
wordDiv.innerText = word;
}
nextBtn.value = 'Nästa spelare (dölj)'
if (currentPlayer == playerCount - 1) {
nextBtn.style.display = 'none';
}
currentPlayer++;
} else {
wordDiv.innerText = ' ';
nextBtn.value = 'Visa';
}
showWord = !showWord;
}
function newGame() {
fakePlayer = randInt(playerCount);
word = WORDS[randInt(WORDS.length)];
showWord = false;
playerCount = parseInt(countInput.value);
currentPlayer = 0;
nextBtn.style.display = 'block';
next();
}
WORDS =...