๐ html <form name="math"> <table> <!-- caption์ผ๋ก ์ ๋ชฉ๋ฌ๊ธฐ (<th> ์ฌ์ฉํ์ง ์์) --> <caption>๐ถ๐น Math Game ๐ธ๐ท</caption> <tr> <td colspan="2"> <!-- colspan์ ๊ฐ๋ก 2์นธ์ ํฉ์นจ(๊ฐ๋ก ๋ณํฉ) --> <input type="button" value="New Game" onClick="location.reload();" style="width:100%"> </td> </tr> - <tr> <td colspan="2"> <input type="button" value="Game Start" size="5" onClick="startgame()" style="width:100%"> </td> </tr> <!--text type์ ํตํด ํ์ด๋จธ ์ค์ (js์ฐ๊ฒฐ)--> <tr> <td> <strong>rest of time</strong> </td> <td> <input type="text" name="timer" size="10"> </td> </tr> <!--๋ฌธ์ ์ ์ (๋๋ค) --> <tr> <td><strong>Question</strong></td> <td> <input type="text" name="firstnum" size="7"> <input type="text" name="operator" size="2"> <!--์ฐ์ฐ์(+,-,x,/) --> <input type="text" name="secondnum" size="7"> </td> </tr> <!-- ๋ต ์ ์ถ --> <tr> <td><strong>Answer</strong></td> <td> <input type="text" name="answer" size="7"> </td> </tr> <tr class="answer"> <td colspan="2" style="text-align:center"> <input type="button" value="Submit and Check Answer" onClick="answerit();"> </td> </tr> <!--์ ์ ํ๊ธฐ --> <tr> <td><strong>Score</strong></td> <td> <input type="text" name="points" size="7" value="0"> </td> </tr> </table> </form>
๐ css table{ border:0; width:500px; height:300px; margin:auto; font-weight: var( --weight-bold); font-size: 20px; color:var(--point-color); padding-top:30px; } caption{ font-size:35px; text-align:center; color:whites; margin-top:30px; padding:5px; background-color: #054d65; border-radius: 30px; } td { text-align:center; padding: 8px; } .answer input{ width:100%; background-color: aquamarine; font-weight: var( --weight-bold); }
๐ javascript "use strict"; /* number game */ var time=5; // ๊ฒ์ ์๊ฐ 50์ด var started=0; /* timer */ function CountDown() { if(time>0) { document.math.timer.value=time; //form submit ํ ๋ ์๋ฒ์์ name๋ช ์ผ๋ก ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.(id์ฒ๋ผ ๊ธฐ๋ฅ) (name=timer), //input์ผ๋ก ๊ฐ์ ์ ๋ฌ(submit)ํ ๋ name์ ์ ๋ฌ ๋ฐ๋ ํ์ด์ง ์ ์ฅ์์ ๊ฐ(value)์ ๊บผ๋ผ ์ ์๋ ํค์ด๋ค. //๋ฐ๋ผ์ document.math.timer.value=time;์ time(50์ผ๋ก ์ค์ , time-1๋ก ์ค์ )์ ๊ฐ(value)์, ๋ณธ๋ฌธ math์์ name์ด timer์ธ ๊ณณ์ผ๋ก ์ ๋ฌ time=time-1; var gameTimer=setTimeout("CountDown()", 1000) } else if (time==0) { document.math.timer.value="0"; alert('Time Over'); // ์๊ฐ์ด ๋๋๊ณ (time=0) ๋ ํ ๋ํ๋๋ ๋ฉ์ธ์ง document.math.firstnum.value=""; //time์ด 0์ด๋๋ฉด ๋ชจ๋ ๊ฐ์ ์ด๊ธฐํ, ๋น๊ฐ("")์ผ๋ก ์ค์ document.math.operator.value=""; document.math.secondnum.value=""; document.math.answer.value=""; } } /* game start */ function startgame() { if (started!=0) { //var started=0;์ผ๋ก ์ค์ ํ์๊ธฐ ๋๋ฌธ์, 0์ด์๋๊ฒ(started!=0) true๋ฉด alert๊ฐ ๋ฌ๋ค alert('์ด๋ฏธ ๊ฒ์์ ํ์ จ์ต๋๋ค.\n\n์๊ฒ์ ๋ฒํผ์ ๋๋ฅด์๊ณ ๋ค์ ํด์ฃผ์ธ์.'); } else { started=1; //??์ด๊ฑธ ์ง์ฐ๋ฉด if๋ฌธ ๋ด์ alert๊ฐ ๋จ์ง ์๋๋ค. ์ด๊ฒ์ ์๋ฏธ๋ ๋ฌด์์ผ๊น CountDown(); getProb(); //get problem (๋ฌธ์ ๋ฅผ ์ป๋ค) ์๋ฏธ์ ํจ์ } } /* question */ function randnum(min,max) { var num=Math.round(Math.random()*(max-min))+min; return num; } var choose, rightanswer function getProb() { choose=randnum(1,4); // if (choose=="1") { document.math.operator.value="+"; var choose1=randnum(0,50); var choose2=randnum(0,50); document.math.firstnum.value=choose1; document.math.secondnum.value=choose2; rightanswer=choose1 + choose2; } if (choose=="2") { document.math.operator.value="-"; var choose2=randnum(0,50); var choose1=randnum(choose2,50); //์ฌ์ฉ์๊ฐ ์ ๋ ์ ๋ต์ด ์ ์๋ง ๋์์ผ ํ๋๊น document.math.firstnum.value=choose1; document.math.secondnum.value=choose2; rightanswer=choose1 - choose2; } if (choose=="3") { document.math.operator.value="x"; var choose1=randnum(0,10); var choose2=randnum(0,10); document.math.firstnum.value=choose1; document.math.secondnum.value=choose2; rightanswer=choose1 * choose2; } if (choose=="4"){ document.math.operator.value="/"; var choose2=randnum(1,10); var choose1=choose2 * randnum(0,10); //์ฌ์ฉ์๊ฐ ์ ๋ ์ ๋ต์ด ์ ์๋ง ๋์์ผ ํ๋๊น document.math.firstnum.value=choose1; document.math.secondnum.value=choose2; rightanswer=choose1 / choose2; } } /* answer submit */ function answerit() { if (started==0) { //์ ์ญ๋ณ์ var started=0; ์ด๊ธฐ๋๋ฌธ์ true // ์์ํ์ง ์๊ณ ๋ฐ๋ก ๋ต๋ฒํผ์ ๋๋ ์๋ alert("Click the 'Game Start' button"); } else { //Game Start๋ฒํผ ๋๋ฅธ ํ answer๋ฅผ ์ ์ถํ๋ ๊ฒฝ์ฐ if (time==0) { //ํ์์ค๋ฒ๋ ์ผ์ด์ค alert('Time Over'); } else { //์ฌ์ฉ์๊ฐ submitํ๋ ์ผ์ด์ค var theiranswer=eval(document.math.answer.value); //eval()ํจ์๋ ์ธ์๋ก์ ๋ฌธ์์ด์ ๋ฐ์ ์ซ์๋ก ๋ณํํ์ฌ ๊ฒฐ๊ณผ๊ฐ์ ๋๋ ค์ค๋ค var theirpoints=eval(document.math.points.value); if (theiranswer==null) { // ๊ฒ์์ค ๋ต๋์ ๋ต์ ์ ์ง ์๊ณ ๋ต๋ฒํผ์ ๋๋ ์๋ ๋ํ๋๋ ๋ฉ์ธ์ง alert("After type on your answer about this question, click on the 'answer' button"); document.math.answer.select(); //select()ํจ์๋ input text๋ textarea ์์๋ฅผ ๋๋๊ทธ ํ ๋ฏ์ด ์ ํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํจ์ (์ฆ, answer text์ฐฝ์ ์ฐ์ฌ์ง ์ด์ ๋ฌธ์ ์ ๋ต์ด ๋๋๊ทธํ ๋ฏ ์ปค์๊ฐ ์ ํ๋ ๋ชจ์ต) } else { //์ฌ์ฉ์๊ฐ ๋ต์ ์ ์์ ์ผ์ด์ค if (theiranswer==rightanswer) { // ๋ฌธ์ ์ ๋ต์ด ๋ง์์๊ฒฝ์ฐ ๋ํ๋๋ ๋ฉ์ธ์ง alert('Right Answer!'); theirpoints++; document.math.points.value=theirpoints; } else { // ๋ฌธ์ ์ ๋ต์ด ํ๋ ธ์ ๊ฒฝ์ฐ ๋ํ๋๋ ๋ฉ์ธ์ง // theiranswer๋ ํ๋ฆฐ๋ต์ ๋ํ๋ด๊ณ rightanswer๋ ์ ํํ ๋ต์ ๋ํ๋ธ๋ค. alert(theiranswer+ " is wrong answer.\n\nThe right answer is "+rightanswer+".") } // ๋ฐฑ์ฌ๋์ฌ(\) + n ์ ์ค๋ฐ๊ฟ document.math.answer.select(); //์ด์ ๋ฌธ์ ์ ์ด ๋ต์ ๋๋๊ทธ ์ปค์ ์ฒ๋ฆฌ getProb(); //์๋ก์ด ๋ฌธ์ ์ป๋๋ค } } } }
โ .select()
๋ฉ์๋(ํจ์)๋ฅผ ์ ์ฉํ๋ฉด input text๋ textarea ์์๋ฅผ ๋๋๊ทธ ํ ๋ฏ์ด ์ ํ๋๋ค.