Back to feed
Renewal·서른의 생활코딩

Sample Source 3) JavaScript Basic Document Structure (Number Object — Application 2)

NS
normalstory
cover image


Honestly... the previous
example is hard to call a game...
 
A game where you have to type is a bit... So I made it in a button format.

My approach was...
1. Create buttons for the user to use.
2. First, create global variables for the computer's random possibilities.
3. Create separate functions that run each time the user clicks, to execute the game. 



<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />




<title> Math Object 2</title>






<script type="text/javascript">


//<!CDATA[





pc=(Math.random()*3)+1; // a number between 0~3


pc=Math.floor(pc);






function fight1()


{


if(pc==1){


document.write("pc = Scissors — it's a tie.") ;


}


else if(pc==2){


document.write("pc = Rock — you lost.") ;


}


else if(pc==3){


document.write("pc = Paper — you won.") ;


}


else{


document.write("pc =...") ;


}


}




//




function fight2()


{


if(pc==1){


document.write("pc = Scissors — you won.") ;


}


else if(pc==2){


document.write("pc = Rock — it's a tie.") ;


}


else if(pc==3){


document.write("pc = Paper — you lost.") ;


}


else{


document.write("pc =...") ;


}


}




//




function fight3()


{


if(pc==1){


document.write("pc = Scissors — you lost.") ;


}


else if(pc==2){


document.write("pc = Rock — you won.") ;


}


else if(pc==3){


document.write("pc = Paper — it's a tie.") ;


}


else{


document.write("pc =...") ;


}


}




//]]1]]]]>


</script>




</head>






<body>


<p><input type="button" value="Scissors" onclick="fight1()" /></p>


<p><input type="button" value="Rock" onclick="fight2()" /></p>


<p><input type="button" value="Paper" onclick="fight3()" /></p>


</body>




</html>



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Why You Should Use .html Instead of .md Seeing Is Believing

Jul 25, 2026·1 min
Renewal

The Most Important Thing About AX

Jul 17, 2026·2 min
Renewal

디지털 기술과 혁신에 대한 논고(지탱하는 힘)

Jul 17, 2026·5 min