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 implement a simple Calculator with Javascript Code

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Javascript code to achieve a simple calculator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1.html code

/ / enter the first number +-* / / drop-down menu, let the user select the operator they need / / enter the second number = / / when you click equals, the result will be output / / the result will be stored here

2.JS code

Var btn_result=document.getElementById ('btn'); / / get the ID var num1=document.getElementById of the equal sign (' txt-num1'); / / get the ID var num2=document.getElementById of the first number ('txt-num2'); / / get the ID var result=document.getElementById of the second number (' txt-result'); / / get the result ID var drop=document.getElementById ('dropdown') / / get the drop-down menu ID btn_result.onclick=function () {/ / this code means that the operation of the two numbers is all caused by the equal sign, and when you click the equal sign, it will make the two values operate var v1=parseFloat (num1.value); / / convert the num to numeric string- > number var v2=parseFloat (num2.value); var v3 values drop.valuesovereignty / use v3 to store the value of the drop-down menu var num / num is used to receive the result of the operation of two numbers switch (v3) {/ / determine which operator the user chooses, and perform the operation case "+": num=v1+v2; break; case "-": num=v1-v2; break; case "*": num=v1*v2; break; case "/": num=v1/v2; break; default: num=v1+v2; break;} result.value=num / / Save the results to result} "how to use Javascript code to implement a simple calculator" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report