当前位置:网站首页>JS function to realize simple calculator

JS function to realize simple calculator

2022-06-25 05:02:00 Dai Sensen

 Please add a picture description

        var n = prompt(' Welcome to the easy calculator :\n1. Addition calculator :\n2. Subtraction calculator :\n3. Multiplication calculator :\n4 Division calculator :\n5. sign out :\n Please enter your choice :');

        function Calculate(n) {
    
            n = parseInt(n);
            while (n != 5) {
    

                switch (n) {
    
                    case 1:
                        n1 = parseInt(prompt(' Please enter the first number '));
                        n2 = parseInt(prompt(' Please enter the second number '));
                        alert(n1 + n2);

                        break;
                    case 2:
                        n1 = parseInt(prompt(' Please enter the first number '));
                        n2 = parseInt(prompt(' Please enter the second number '));

                        alert(n1 - n2);

                        break;
                    case 3:
                        n1 = parseInt(prompt(' Please enter the first number '));
                        n2 = parseInt(prompt(' Please enter the second number '));
                        alert(n1 * n2);

                        break;
                    case 4:
                        n1 = parseInt(prompt(' Please enter the first number '));
                        n2 = parseInt(prompt(' Please enter the second number '));
                        alert(n1 / n2);

                        break;
                    case 5:
                        n = 5;
                        break;
                }
                n = prompt(' Welcome to the easy calculator :\n1. Addition calculator :\n2. Subtraction calculator :\n3. Multiplication calculator :\n4 Division calculator :\n5. sign out :\n Please enter your choice :');
                n = parseInt(n);
            }
        }
        Calculate(n);
原网站

版权声明
本文为[Dai Sensen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210528225864.html