Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Calculator with JS

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the JS how to achieve the simple calculator related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this JS how to achieve the simple calculator article will have a harvest, let's take a look.

Description of the topic

The user selects the operation to be done in the pop-up input box

After the user selects, two input boxes pop up, allowing the user to enter data

According to the operation selected by the user and the input data, calculate the final result and pop up a prompt box to display the result.

The operation interface is as follows:

Second, the process of realization

1. Prompt

The user selects the desired calculation by entering the corresponding numerical value in the pop-up input box

Two other input boxes pop up to collect the data entered by the user to participate in the operation

Display the calculation results through the prompt box

two。 Code demonstration

The code is as follows (example):

/ / the user selects the operation to be done in the pop-up input box

/ / two input boxes pop up after user selection, allowing the user to enter data

/ / according to the operation selected by the user and the data entered, calculate the final result and pop up a prompt box to display the result

Function getResult (symbol) {

Switch (symbol) {

Case '1clients:

Var num1 = prompt ('Please enter the first number')

Var num2 = prompt ('Please enter the second number')

Return parseFloat (num1) + parseFloat (num2)

Case '2clients:

Var num1 = prompt ('Please enter the first number')

Var num2 = prompt ('Please enter the second number')

Return num1-num2

Case '3pm:

Var num1 = prompt ('Please enter the first number')

Var num2 = prompt ('Please enter the second number')

Return num1 * num2

Case '4pm:

Var num1 = prompt ('Please enter the first number')

Var num2 = prompt ('Please enter the second number')

Return num1 / num2

Default:

Return'no such operation'

}

}

Var symbol = prompt ('Welcome to simple Calculator:\ n 1. Addition operation\ n2. Subtraction\ n3. Multiplication\ n4. Division operation\ n5. Exit\ nPlease enter your options:')

Var result

While (symbol! ='5') {

Result = getResult (symbol)

Alert ('result of operation' + result)

Symbol = prompt ('Welcome to simple Calculator:\ n 1. Addition operation\ n2. Subtraction\ n3. Multiplication\ n4. Division operation\ n5. Exit\ nPlease enter your options:')

}

Alert ('exiting')

Note:

Swicth (condition) and case 'condition' are congruent relations.

The return value of the input box is string.

Return can interrupt the function and does not execute the following statement, so there is no break written in switch.

This is the end of the article on "how to realize the simple Calculator in JS". Thank you for reading! I believe you all have a certain understanding of "how to realize the simple calculator in JS". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report