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

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

NS
normalstory
cover image

The code just written above seems more convenient for the user to play the game..
But the code is a bit long. ( Relative to what is needed...)

Using the global variable approach we saw earlier, we can reduce three separate function definitions
to just one.



<!DOCTYPE html>


<html>


<head>


<meta charset="utf-8" />


<title></title>


<script type="text/javascript">


//<![CDATA[
 


function process(user) {


var result = 0;


var computer = parseInt((Math.random() * (3 - 1 + 1)) + 1);


switch(user-computer) {


case -2: case 1: result = 1; break;


case -1: case 2: result = -1; break;


case 0: result = 0; break;


default: break;


}


document.write("<p />You: ");


document.write((user==1)?"Scissors":(user==2)?"Rock":"Paper");




document.write("<p />Opponent: ");


document.write((computer==1)?"Scissors":(computer==2)?"Rock":"Paper");




document.write("<p />Result: ");


document.write((result==1)?"Win": (result==0)?"Draw":"Loss");


}




//]]]]>


</script>


</head>


<body>


<input type="button" value="Scissors" onclick="process(1)" />


<input type="button" value="Rock" onclick="process(2)" />


<input type="button" value="Paper" onclick="process(3)" />


</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

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min