JavaScript implements simple calculator function

JavaScript implements simple calculator function

This article shares the specific code of JavaScript to implement a simple calculator for your reference. The specific content is as follows

This example is a simple calculator:

Code example:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Calculator</title>
    <script>
        var choice = prompt('Welcome to the simple calculator:\n1. Addition;\n2. Subtraction;\n3. Multiplication;\n4. Division;\n5. Exit;\nPlease enter your option:');
        switch (choice) {
            case '1':
                add();
                break;
            case '2':
                sub();
                break;
            case '3':
                multiplication();
                break;
            case '4':
                division();
                break;
            case '5':
                alert('Exited')
                break;
        }

        // add function add() {
            var num = prompt('Please enter the number of numbers to be added:');
            var sum = 0;
            var arr = [];
            for (var i = 0; i < num; i++) {
                arr[i] = prompt('Please enter the value of the ' + (i + 1) + ' number: ');
                console.log(arr[i]);
                sum += parseFloat(arr[i]);

                /* 
                Note: This way of writing can assign values ​​to the arr array, but you cannot call arr[arr.length], as the result of the call is undefined
                arr[arr.length] = prompt('Please enter the value of the ' + (i + 1) + ' number: ');
                console.log(arr[arr.length]);
                sum += parseFloat(arr[arr.length]); 
                */
            }
            alert(arr + 'The sum of these numbers is:' + sum);
        }

        // subtraction function sub() {
            var number1 = prompt('Please enter the first value:');
            var number2 = prompt('Please enter the second value:');
            var result = parseFloat(number1) - parseFloat(number2);
            alert(number1 + 'minus' + number2 + 'the value is:' + result);
        }

        // multiplication function multiplication() {
            var number1 = prompt('Please enter the first value:');
            var number2 = prompt('Please enter the second value:');
            var result = parseFloat(number1) * parseFloat(number2);
            alert(number1 + ' multiplied by ' + number2 + ' is: ' + result);
        }

        // In addition to function division() {
            var number1 = prompt('Please enter the first value:');
            var number2 = prompt('Please enter the second value:');
            var result = parseFloat(number1) / parseFloat(number2);
            alert(number1 + 'divided by' + number2 + 'the value is:' + result);
        }
    </script>
</head>

<body>

</body>

</html>

Note: Try to use arr[arr.length] to assign and calculate, but you can only assign, not call. The call shows that the value of arr[arr.length] is undefined

Page effect:

add:

reduce:

take:

remove:

quit:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing a simple age calculator based on HTML+JS
  • Implementing a web calculator with native JavaScript
  • Detailed explanation of the process of realizing calculator function in javascript
  • js version to realize calculator function
  • Native js to implement a simple calculator
  • Implementing a simple calculator with javascript
  • Writing a web calculator using javascript
  • JavaScript Example - Implementing a Calculator

<<:  A brief discussion on three methods of asynchronous replication in MySQL 8.0

>>:  How to restore a single database or table in MySQL and possible pitfalls

Recommend

The most comprehensive collection of front-end interview questions

HTML+CSS 1. Understanding and knowledge of WEB st...

H tags should be used reasonably in web page production

HTML tags have special tags to handle the title of...

JavaScript to implement login slider verification

This article example shares the specific code of ...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

HTML design pattern daily study notes

HTML Design Pattern Study Notes This week I mainl...

Vue.js front-end web page pop-up asynchronous behavior example analysis

Table of contents 1. Preface 2. Find two pop-up c...

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

Introduction to html form control disabled attributes readonly VS disabled

There are two ways to disable form submission in ...