Javascript Code : Guessing Game using Javascript CSS/HTML Styles , If Else and Switch case , Math.random
<html>
<head> Javascript Events</head>
<body>
<h2> Enter a guess number </h2>
<input type="text" id="input1" onkeydown="keydownevent()"/>
<p id="display">
<p id="display2">
<script>
<!--
function keydownevent()
{
document.getElementById("input1");
n=document.getElementById("input1").value;
document.getElementById("display").style.color="white";
x=Math.floor(Math.random()*5 +1);
switch(Number(n))
{
case 1: document.getElementById("display").style.background="pink";
document.getElementById("display").innerHTML="pink";
break
case 2: document.getElementById("display").style.background="blue";
document.getElementById("display").innerHTML="blue";
break;
case 3: document.getElementById("display").style.background="maroon";
document.getElementById("display").innerHTML="maroon";
break
case 4:document.getElementById("display").style.background="green";
document.getElementById("display").innerHTML="green";
break;
default: case 4:document.getElementById("display").style.background="orange";
c="orange";
document.getElementById("display").innerHTML="orange";
break;
}
if(x==n)
{
document.getElementById("display2").style.color="yellow";
document.getElementById("display2").style.background="purple";
document.getElementById("display2").innerHTML="Congratulations , You Guess it Right ";
}
else
{
document.getElementById("display2").style.background="red";
document.getElementById("display2").innerHTML="Im sorry you are wrong, the Correct Answer is " +x;
}
}
//-->
</script>
</body>
</html>
Javascript Events
Enter a guess number
Comments
Post a Comment