In: Computer Science
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
<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