const rowsArray = Array.from(document.getElementsByClassName('row'));
const firstRowArray = Array.from(document.getElementsByClassName('1-row')),
secondRowArray = Array.from(document.getElementsByClassName('2-row')),
thirdRowArray = Array.from(document.getElementsByClassName('3-row'));
let wordBank = ['Table', 'Mouse', 'Regal', 'Floor', 'Horse', 'Pants', 'Water'];
let playersWord = '';
let hiddenWord = 'Regal';
let isWordWrong = false;
//let rowCount = 0;
function switchLetters(row) { // switch between letter inputs inside each row
for (let i = 0; i < row.length; i++) {
row[i].onkeyup = () => {
if (row[0].value !== '') {
row[1].focus();
}
if (row[1].value !== '') {
row[2].focus();
}
if (row[2].value !== '') {
row[3].focus();
}
if (row[3].value !== '') {
row[4].focus();
}
}
row[i].onkeydown = (e) => {
if (row[1].value === '' && e.key === 'Backspace') {
row[0].focus();
} else if (row[2].value === '' && e.key === 'Backspace') {
row[1].focus();
} else if (row[3].value === '' && e.key === 'Backspace') {
row[2].focus();
} else if (row[4].value === '' && e.key === 'Backspace') {
row[3].focus();
}
}
}
}
switchLetters(firstRowArray);
switchLetters(secondRowArray);
switchLetters(thirdRowArray);
function switchRows(lastRow, nextRow) { // switch between rows if the conditions are met
document.addEventListener('keydown', (e) => {
if (!lastRow.every(checkValue) && e.key === 'Enter') {
window.alert('Please fill the letters.');
} else if (lastRow.every(checkValue) && e.key === 'Enter') {
checkLetters(lastRow);
if (isWordWrong) {
return;
} else {
//rowCount++;
lastRow.forEach(letter => {
letter.setAttribute('disabled', '');
letter.style.borderColor = 'white';
})
nextRow.forEach(letter => {
letter.removeAttribute('disabled', '');
letter.style.borderColor = 'black';
})
nextRow[0].focus();
//console.log(rowCount)
}
}
})
}
switchRows(firstRowArray, secondRowArray);
switchRows(secondRowArray, thirdRowArray);
switchRows(thirdRowArray, null);
function checkValue(letter) { // checks if the letters are empty
return letter.value !== '';
}
function checkLetters(row) { // compares every letter to the hiddenWord
playersWord = row[0].value + row[1].value + row[2].value + row[3].value + row[4].value;
if (wordBank.includes(playersWord)) {
isWordWrong = false;
if (row[0].value === hiddenWord.charAt(0)) {
row[0].style.backgroundColor = 'green';
} else if (hiddenWord.includes(row[0].value)) {
row[0].style.backgroundColor = 'yellow';
} else if (row[0].value !== hiddenWord.charAt(0)) {
row[0].style.backgroundColor = 'red';
}
if (row[1].value === hiddenWord.charAt(1)) {
row[1].style.backgroundColor = 'green';
} else if (hiddenWord.includes(row[1].value)) {
row[1].style.backgroundColor = 'yellow';
} else if (row[1].value !== hiddenWord.charAt(1)) {
row[1].style.backgroundColor = 'red';
}
if (row[2].value === hiddenWord.charAt(2)) {
row[2].style.backgroundColor = 'green';
} else if (hiddenWord.includes(row[2].value)) {
row[2].style.backgroundColor = 'yellow';
} else if (row[2].value !== hiddenWord.charAt(2)) {
row[2].style.backgroundColor = 'red';
}
if (row[3].value === hiddenWord.charAt(3)) {
row[3].style.backgroundColor = 'green';
} else if (hiddenWord.includes(row[3].value)) {
row[3].style.backgroundColor = 'yellow';
} else if (row[3].value !== hiddenWord.charAt(3)) {
row[3].style.backgroundColor = 'red';
}
if (row[4].value === hiddenWord.charAt(4)) {
row[4].style.backgroundColor = 'green';
} else if (hiddenWord.includes(row[4].value)) {
row[4].style.backgroundColor = 'yellow';
} else if (row[4].value !== hiddenWord.charAt(4)) {
row[4].style.backgroundColor = 'red';
}
console.log(playersWord)
} else {
window.alert('That is not a word.');
isWordWrong = true;
}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 40%;
height: 50%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
}
.row {
width: 100%;
height: 20%;
display: flex;
}
.row:first-of-type input {
border-color: black;
}
input {
width: 20%;
height: 100%;
font-size: 3rem;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
</head>
<body>
<div class="container">
<div class="row">
<input type="text" class="1-row" maxlength="1" autofocus>
<input type="text" class="1-row" maxlength="1">
<input type="text" class="1-row" maxlength="1">
<input type="text" class="1-row" maxlength="1">
<input type="text" class="1-row" maxlength="1">
</div>
<div class="row">
<input type="text" class="2-row" maxlength="1" disabled>
<input type="text" class="2-row" maxlength="1" disabled>
<input type="text" class="2-row" maxlength="1" disabled>
<input type="text" class="2-row" maxlength="1" disabled>
<input type="text" class="2-row" maxlength="1" disabled>
</div>
<div class="row">
<input type="text" class="3-row" maxlength="1" disabled>
<input type="text" class="3-row" maxlength="1" disabled>
<input type="text" class="3-row" maxlength="1" disabled>
<input type="text" class="3-row" maxlength="1" disabled>
<input type="text" class="3-row" maxlength="1" disabled>
</div>
</div>
</body>
</html>
I’m building a small Wordle game and I’ve programmed most of the game, but there is a problem with calling the switchRows() functions. Whenever I click enter as to check if the values of the active row fulfill the conditions, it pops the window.alert() warning three times, as to one for each row. I tried to fix this by putting each function in a condition such as if (rowCount === 1) switchRows(secondRowArray, thirdRowArray) and so on, but then it doesn’t work at all. How could I fix this? Window.alert() should fire only once.