<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
</style>
<title>끝말 잇기 게임</title>
</head>
<body>
<div><span id="order">번째 참가자</span></div>
<div>제시어: <span id="word"></span></div>
<input type="text">
<button>입력</button>
<script>
const number= Number(prompt('몇 명이 참가하나요?'));
const $button=document.querySelector('button');
const $input=document.querySelector('input');
const $word=document.querySelector('#word');
const $order=document.querySelector('#order');
let word;
let newWord;
const onClickButton=()=>{
if(!word||word[word.length-1]===newWord[0]){
word=newWord;
$word.textContent=word;
const order=Number($order.textContent);
if(order+1>number){
$order.textContent=1;
}else{
$order.textContent=order+1;
}
}else {
alert('올바르지 않은 단어입니다!');
}
$input.value=' ';
$input.focus();
};
const onInput=(e)=>{
newWord=e.target.value;
};
$button.addEventListener('click', onClickButton);
$input.addEventListener('input', onInput);
</script>
</body>
</html>