Write a calculator with JS

Source: Internet
Author: User

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd ">

<meta http-equiv = "Content-type" content= "Text/html;charset=utf-8"/>

<script src= "Js.js" type= "Text/javascript" language= "JavaScript" >

</script>

<body>

<form>

<input type= "button" value= "1" onclick = "cal (This.value);" /> <!--calling Functions--

<input type= "button" value= "2" onclick = "cal (This.value);" />

<input type= "button" value= "3" onclick = "cal (This.value);" />

<input type= "button" value= "4" onclick = "cal (This.value);" />

<input type= "button" value= "+" onclick = "cal (This.value);" />

<input type= "button" value= "-" onclick = "cal (This.value);" />

<input type= "button" value= "*" onclick = "cal (This.value);" />

<input type= "button" value= "/" onclick = "cal (This.value);" />

<input type= "button" value= "=" onclick = "cal (This.value);" /><br/>

<input type = "text" id = "text" name = "Text"/>

</form>

</body>

Js.js

Function cal (value) {Var text = document.getelementbyid ("text"). value;var last =  text.charat (text.length-1);//  gets the last element in the text box   if (text == null) {text =  "";     }else{if ((value ==  "+") | | (value ==  "-") | | (value ==  "*") | | (value ==  "/")) {//input is  + - / *if ((last ==  "+") | | (last ==  "-") | | (last ==  "*") | | (last ==  "/")) The last element in the {///text box is  + - * /  Error alert when input  + - / *  ("incorrect input");} else{//the last element in the text box is  + - * /  re-enters the number when it is added to the back Text += value;}} Else if (value ==  "=") {//input is =   if ((last ==  "+") | | (last ==  "-") | | (last ==  "*") | | (last ==  "/")) {//Enter =  ,  when the last element is + - * / ,  cannot calculate   only the number of the last symbol is displayed text =  Text.substring (0,text.length-1),   }else{//input is =    the last one is the number    can be calculated text = eval (text);   /// Use eval (str)   Calculate   }}else{//input is digital    direct display Text += value;}} document.getElementById ("text"). value = text; //returns the final result to the text box}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.