Question

In: Computer Science

please create a calculator using p5.js web editor. Please make sure it works with only js....

please create a calculator using p5.js web editor. Please make sure it works with only js. No added html or css; p5.js only. PLEASE MAKE SURE IT WORKS

Solutions

Expert Solution

<script type="text/javascript">
          
          function add(a,b)
          {
              document.write(a+"+"+b+"= ");
              document.write(a+b);
          }
          
          function subtract(a,b)
          {
              document.write(a+"-"+b+"= ");
              document.write(a-b);
          }
          
          function multiply(a,b)
          {
              document.write(a+"*"+b+"= ");
              document.write(a*b);
          }
          
          function divide(a,b)
          {
              if(b!=0 || b!=0.0)
                {
                        document.write(a+"/"+b+"= ");
                        document.write(a/b);
                }
              else
                  {
                      document.write("Division By Zero!!! Refresh to enter again :->");
                  }
              
          }
          
          function modulo(a,b)
          {
              if(b!=0 || b!=0.0)
                {
                        document.write(a+"%"+b+"= ");
                        document.write(a%b);
                }
              else
                  {
                      document.write("Division By Zero!!! Refresh to enter again :->");
                  }
          }
          
          function increment(a)
          {
              document.write(a+"++"+"= ");
              document.write(++a);
          }
          
          function decrement(a)
          {
              document.write(a+"--"+"= ");
              document.write(--a);
          }
    
          var input=prompt("Type I to perform Arithmetic Operation on Integers. Type F to perform Arithmetic Operation on Floating Numbers. Type BOTH to enter both types of values. ");
          if(input=="I")
              {
                    var a=prompt("Enter First Number: ");
                    a=parseInt(a);
                    var b=prompt("Enter Second Number: ");
                    b=parseInt(b);
              }
          else if(input=="F")
              {
                   var a=prompt("Enter First Number: ");
                   a=parseFloat(a);
                   var b=prompt("Enter Second Number: ");
                   b=parseFloat(b);
              }
          else if(input=="BOTH")
              {
                   var a_type=prompt("Type A if you want First Number to be Integer and Second Number to be Float else type B");
                  if(a_type=="A")
                      {
                               var a=prompt("Enter First Number: ");
                               a=parseInt(a);
                               var b=prompt("Enter Second Number: ");
                               b=parseFloat(b);
                      }
                  else if(a_type=="B")
                         {
                                var a=prompt("Enter First Number: ");
                                a=parseFloat(a);
                                var b=prompt("Enter Second Number: ");
                                b=parseInt(b);
                         }
                  else
                  {
                         document.write("Invalid Entry!!! Refresh to enter Again :->") ;   
                  }
                  
              }
          else
              {
                  document.write("Invalid Entry!!! Refresh to enter Again :->") ; 
                  exit();
              }
         
          var choice=prompt("Enter Arithmetic Operation you want to perform: ");
          if(choice=="+")
              {
                  add(a,b);
              }
          else if(choice=="-")
              {
                  subtract(a,b);
              }
          else if(choice=="*")
              {
                  multiply(a,b);
              }
          else if(choice=="/")
              {
                  divide(a,b);
              }
          else if(choice=="%")
              {
                  modulo(a,b);
              }
          else if(choice=="++")
              {
                  var choice1=prompt("Enter A if you want to increment First number else type B");
                  if(choice1=="A")
                       increment(a);
                  else if(choice1=="B")
                      increment(b);
                  else
                       document.write("Invalid Entry!!! Refresh to enter Again :->") ;   
              }
          else if(choice=="--")
              {
                  var choice1=prompt("Enter A if you want to decrement First number else type B");
                  if(choice1=="A")
                       decrement(a);
                  else if(choice1=="B")
                      decrement(b);
                  else 
                       document.write("Invalid Entry!!! Refresh to enter Again :->") ;   
              }
          else
              {
                  document.write("Invalid Entry!!! Refresh to enter Again :->") ;  
              }
          
</script>

Explanation of Code

1. In this js code you can perform +,-,*,/,%,increment and decrement of numbers. This code can handle both integers and float values.

2. No html or css is used in making this calculator.

3. This calculator works for 2 numbers only.

4. First you will be asked if you want to enter Integer values or Float values or both type of values. If you want to perfrom arithmetic operation on Integers only then press 'I' or if you want to perform arithmetic operation on Float values then press 'F' and if you want one  float and one integer.

5. Suppose you want to enter both types of values then you will be asked to enter A if you want your first number to be integer and second number to be float otherwise enter B.

6. Then enter the operator you want to perform on them.

7. If you want to increment or decrement then you will be asked if you want to increment or decrement A or B because increment/decrement is a unary operation.

Output


Related Solutions

please create a calculator using p5.js web editor. Please make sure it works with only js....
please create a calculator using p5.js web editor. Please make sure it works with only js. No added html or css; p5.js only
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any of the following: • Your hobbies, likes/dislikes, career aspirations, dream job, favorite animals/ pets, favorite superhero, etc. • You do not have to share personal information that you are uncomfortable with sharing. Follow these guidelines when creating your page: • Include at least one heading (h1, h2, etc.) in your web page. • Provide a quote from a book, movie, etc. • Include at...
In Marvin JS Editor how do you make OH into OD?
In Marvin JS Editor how do you make OH into OD?
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Create a portfolio using the four stocks and information below: ((((please please make sure the answers...
Create a portfolio using the four stocks and information below: ((((please please make sure the answers are correct please)))) Expected Return Standard Deviation Weight in Portfolio Stock A 21.00% 21.00% 15.00% Stock B 5.00% 17.00% 28.00% Stock C 7.00% 12.00% 11.00% Stock D 22.00% 22.00% 46.00% ---------------------- ---------------------- ---------------------- ---------------------- Correlation (A,B) 0.7000 ---------------------- ---------------------- Correlation (A,C) 0.4900 ---------------------- ---------------------- Correlation (A,D) 0.2500 ---------------------- ---------------------- Correlation (B,C) 0.4400 ---------------------- ---------------------- Correlation (B,D) 0.9600 ---------------------- ---------------------- Correlation (C,D) 0.2000 ---------------------- ----------------------...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a CE button to cancel the operation.    Use the following website or any online resource as a reference. Don’t use their CSS or any other code.                 https://freshman.tech/calculator/ I am trying to learn please make comments then I will understand better.
JS Bin / Tax Calculator / using HTML | CSS | JavaScript ------------------------------------------------------------------------------------------- How can I...
JS Bin / Tax Calculator / using HTML | CSS | JavaScript ------------------------------------------------------------------------------------------- How can I edit the JavaScript below so that when the calculate button is clicked the total price only shows two decimal places by using the toFixed() method? ----------------------------------------------------------------------------------------------------------- JavaScript: // Variables var tax = "tax"; // Tax percentage var taxRate = document.getElementById('tax'); // Selecting tax element // On the time of loading the DOM we are setting the tax rate window.addEventListener('DOMContentLoaded', (event) => { taxRate.value =...
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
create a VHDL program for the Truth Table below. Please make sure to create your code...
create a VHDL program for the Truth Table below. Please make sure to create your code for the simplified circuit. A B C Z 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0
Create a graph using GeoGebra of x 3 − 3x 2 + 2x. Make sure the...
Create a graph using GeoGebra of x 3 − 3x 2 + 2x. Make sure the graph contains the zeros, maxima, and minima with labels for each. Having a really hard time graphing on this application, an explanation of how to do so would be extremely helpful.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT