Home >Web Front-end >JS Tutorial >Example of a simple web calculator that can change colors randomly using javascript
The example in this article describes the implementation of a simple random color-changing web calculator in JavaScript. Share it with everyone for your reference, the details are as follows:
This program can implement simple addition, subtraction, multiplication, division, and remainder. The page also adds the function of randomly changing colors.
The running effect diagram is as follows:
The complete example code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Day 2 </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script> function calculator(){ var selection=document.form.selection.value; var numb1=document.form.number1.value; var numb2=document.form.number2.value; var relnum=""; switch(selection){ case "+": relnum=parseFloat(numb1)+parseFloat(numb2); break; case '-': relnum=numb1 - numb2; break; case '*': relnum=numb1 * numb2; break; case '/': if(numb2==0) alert("wrong input! "); relnum=numb1 / numb2; break; case '%': relnum=numb1 % numb2; break; } document.form.result.value=relnum; } function sound(){ document.all.sound.src="clock.wav"; } function changeBgcolor(){ var bgc=document.getElementById("idbgc"); var rand=""; for(var i=0;i<6;i++){ rand+=Math.round(Math.random()*9) } bgc.style.backgroundColor='#'+rand; } </script> </HEAD> <BODY bgcolor="aliceblue" id="idbgc"> <h1>simple web calculator</h1> <form name="form" action=""> <input type=text name="number1" style="width:80px "> <select name="selection"> <option value='+'> + <option> <option value='-'>-<option> <option value='*'>*<option> <option value='/'>/<option> <option value='%'>%<option> </select> <input type=text name="number2" style="width:80px "> <input type="button" value=" = " onclick="calculator(),changeBgcolor()"> <input type=text name="result" style="width:80px "><br> <input type="reset" value="reset" onclick="changeBgcolor()"> </form> </BODY> </HTML>
I hope this article will be helpful to everyone in JavaScript programming. .
For more javascript implementation of simple random color changing web calculator examples, please pay attention to the PHP Chinese website!