Home > Web Front-end > JS Tutorial > Example of a simple web calculator that can change colors randomly using javascript

Example of a simple web calculator that can change colors randomly using javascript

高洛峰
Release: 2017-01-20 17:17:15
Original
1617 people have browsed it

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:

Example of a simple web calculator that can change colors randomly using javascript

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 &#39;-&#39;:
        relnum=numb1 - numb2;
        break;
      case &#39;*&#39;: 
        relnum=numb1 * numb2;
        break;
      case &#39;/&#39;:
        if(numb2==0)
          alert("wrong input! ");
        relnum=numb1 / numb2;
        break;
      case &#39;%&#39;:
        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=&#39;#&#39;+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=&#39;+&#39;> + <option>
      <option value=&#39;-&#39;>-<option>
      <option value=&#39;*&#39;>*<option>
      <option value=&#39;/&#39;>/<option>
      <option value=&#39;%&#39;>%<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template