jquery implements a simple calculator

Source: Internet
Author: User

It's 2:50 P.M. and the eyes and shoulders are a bit sour and the brain is a bit of wood.

Organize the process and results of the calculator:

1, table layout keys and areas:

A 6-row table. The first and second lines are two type= "text" of the <input>, the width occupies four columns of width, colspan= "4", respectively, the input and output of the display line. The third row has two columns, namely clear 0 and the Abdication button. Give each key a tag ID and value.

2, the script when the idea is really important, the logic of the code is messy, the structure and readability is poor, enter the state will be a kung fu ah.

2.1 According to the key classification:

If the decimal point is pressed but the input already contains a decimal point, the key is not valid at this time;

If you press 0, and no previous input value, the key is not valid at this time, if there is a button 0 has been pressed, the key is still invalid;

If the input value is too long, the key is invalid;

If the decimal point is pressed, but no previous input value or input value is 0 o'clock, the current input value is stored as "0.";

If 0 is pressed, and the input value is longer than 1 o'clock, the current input is considered valid and stored in the input value sequentially;

If 1-9 or. is pressed, then the input is stored sequentially;

If the operation symbol +-*/is pressed, the input value is converted into floating point storage into number1, and the input value is emptied, and the current operation symbol is stored in the Mysi;

If press =, convert the input value into floating point storage into number2, and empty the input value, according to the stored Mysi operation symbol, NUMBER1, calculated;

2.2

Input values, real-time display of output values.

2.3

Finally consider clearing 0 keys, the abdication key and input and output to establish a relationship, and improve.

3, multiple tests, check the bug.

   

Result: The function is basically complete.

Experience: 2/3 of the time is the last input value and output value of real-time display, it is a bit strange. May be the previous part of the function has been realized, complacent, blind to the effect. A particularly sober state to make the final perfect is really important, the two days of more chores, the state is not good.

It is important that the original logical framework is clear and concise, like the foundation of a building. The foundation is too shallow, the load is too big, the code is very cumbersome. The pore is too small, the place of the brick is too little, the house space is too small, too limited.

The final perfect part is often the finishing touch, icing on the cake, but I really do not do well.

Finish, encourage yourself.

    

jquery implements 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.