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

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

NS
normalstory
cover image

Let's build a rock-paper-scissors game you can play against the computer.



First, here's the model(?) answer.
 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


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


<script type="text/javascript">


//<![CDATA[


// Rock-Paper-Scissors game


var computer, computer100,user, user100;


var result;




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


computer = Math.floor(computer); // 1:Scissors 2:Rock 3:Paper


user100 = prompt("Rock-Paper-Scissors: choose one of Scissors, Rock, or Paper!!","");


switch(user100){


case "Scissors": user=1;


break;


case "Rock": user=2;


break;


case "Paper": user=3;


break;


default: alert("Invalid choice!");


}




switch(computer){


case 1: computer100="Scissors";


break;


case 2: computer100="Rock";


break;


case 3: computer100="Paper";


break;


default: alert("Invalid choice!");


}




document.write("user : " + user100 + "<br />");


document.write("computer : " + computer100 + "<br />");






if (user - computer == 1 || user - computer == -2)


{


result = "You won...";


}


else if (user - computer == 0)


{


result = "It's a tie";


}


else


{


result = "You lost...";


}


document.write("result : " + result + "<br />");


//]]]]>


</script>


</head>


<body>




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