작성
·
271
0
제가 추가적으로 구현한건 아래와 같습니다.
끝말잇기 단어를 잘못 선택하면 탈락
기존에 썼던 단어를 또 사용하면 탈락
참가자가 한 명 남으면 게임이 끝남
=> 참가자는 number를 배열로 만들어서
틀리면 배열의 숫자를 제거하는걸로 구현했습니다.
const number = Number(prompt("몇 명이 참가하나요?"));
if (number) {
const $order = document.querySelector("#order");
const $word = document.querySelector("#word");
const $input = document.querySelector("input");
const $button = document.querySelector("button");
const numArray = Array(number)
.fill()
.map((v, i) => i + 1);
let word;
let newWord;
const compareArray = [];
let order = Number($order.textContent) - 1;
const onClickButton = () => {
if (
(!word || word[word.length - 1] === newWord[0]) &&
newWord.length === 3
) {
if (compareArray.includes(newWord) === false) {
word = newWord;
$word.textContent = word;
compareArray.push(word);
console.log(compareArray, numArray);
if (numArray[order] + 1 > numArray.length) {
$order.textContent = numArray[0];
order = 0;
} else {
$order.textContent = numArray[order + 1];
order += 1;
}
} else {
alert("이미 있는 단어입니다. 탈락입니다.");
numArray.splice(numArray.indexOf(numArray[order]), 1);
$order.textContent = numArray[order];
console.log(numArray, order);
}
} else if (newWord.length !== 3) {
alert("작성해야 할 단어는 총 3글자입니다.");
} else {
alert(
`잘못된 단어입니다. ${numArray[order]}번 참가자는 탈락입니다.`
);
numArray.splice(numArray.indexOf(numArray[order]), 1);
$order.textContent = numArray[order];
console.log(numArray, order);
}
if (numArray.length === 1) {
alert(
`게임이 종료되었습니다 승리자는${numArray[0]}번 참가자입니다`
);
}
$input.value = "";
$input.focus();
console.log(newWord);
};
const onInput = (event) => {
newWord = event.target.value;
};
$button.addEventListener("click", onClickButton);
$input.addEventListener("input", onInput);
}
답변 1
0
감사합니다! 제로초님 질문이 하나 더 있습니다!
prompt로 값을 받지 않고 input으로 값을 받고 싶습니다.
=> input으로 값을 받으려는 이유는 참가자를 받는 페이지와 게임을 하는 페이지를 다르게 하고 싶습니다.
=> 그런데 구글링을 어떻게 해야할지생각이 잘 나지 않아서 혹시 힌트라도 조금 얻을 수 있을까요? ex. 특정 기능구현을 구글링하면 좋다 (?)
늦은 시간에 답변주셔서 감사합니다!