JS to implement a simple calculator

Source: Internet
Author: User

use JS to complete a simple calculator function. After you have entered an integer into the 2 input boxes, click on the third input box to give a subtraction of 2 integers. Effect as above:

The first step: Create the build Operation function count ().

Step two: Get the values in the two input boxes and get the value of the selection box.

Hint: document.getElementById (id name). Value Gets or sets the value of the ID name.

Step three: Get the value selected by the drop-down box to change the subtraction algorithm.

Tip: Use switch to determine the algorithm.

Fourth step: Call the Created function with the = button to get the result.

Note: use the parseint () function to parse a string and return an integer.

Code:

1 <!DOCTYPE HTML>2 <HTML>3  <Head>4   <title>Event</title>  5   <Scripttype= "Text/javascript">6    functioncount () {7     vara=document.getElementById ("txt1"). Value;8     varb=document.getElementById ("txt2"). Value;9     //gets the value of the first input boxTen     //gets the value of the second input box One     //gets the value of the selection box A     varC=document.getElementById ("Select"). Value; - a=parsefloat (a); - b=parsefloat (b); the     //gets the value selected by the drop-down box to change the subtraction algorithm -     //set the value of the result input box -     Switch(c) { -          Case "+": + document.getElementById ("Fruit"). Value=parseint (a)+parseint (b); -          Break; +          Case "-": A document.getElementById ("Fruit"). Value=parseint (a)-parseint (b); at          Break; -          Case "*": - Ocument.getelementbyid ("Fruit"). Value=parseint (a)*parseint (b); -          Break; -          Case "/": - document.getElementById ("Fruit"). Value=parseint (a)/parseint (b); in          Break; -     } to    } +   </Script>  -  </Head>  the  <Body> *    <inputtype= ' text 'ID= ' Txt1 '/>  $    <SelectID= ' SELECT '>Panax Notoginseng         <optionvalue="+">+</option> -         <optionvalue="-">-</option> the         <optionvalue="*">*</option> +         <optionvalue="/">/</option> A    </Select> the    <inputtype= ' text 'ID= ' txt2 '/>  +    <inputtype= ' button 'value= ' = 'onclick= "count ()"/> -    <inputtype= ' text 'ID= ' Fruit '/>    $  </Body> $ </HTML>

JS to implement a simple calculator

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.