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:
|
<<: 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
HTML+CSS 1. Understanding and knowledge of WEB st...
The MySQL database does not have an incremental b...
With the continuous development of the Internet ec...
HTML tags have special tags to handle the title of...
Install Apache from source 1. Upload the Apache s...
<br />Question: Why is it not recommended to...
This article example shares the specific code of ...
The img tag in XHTML should be written like this:...
Table of contents JavaScript private class fields...
The editor recently wanted to get started with th...
Friends who have bought space and built websites s...
HTML Design Pattern Study Notes This week I mainl...
Table of contents 1. Preface 2. Find two pop-up c...
Table of contents definition Constructor bodies a...
There are two ways to disable form submission in ...