Question

please create a calculator using p5.js web editor

please create a calculator using p5.js web editor

Homework Answers

Answer #1

var calculator = [];
var minusReplacement = '_';
//var command = '';

function setup() {
createCanvas(400, 400);
calculator[0] = '';
}

function draw() {
background(220);
noStroke();
fill(200, 50, 200);
textSize(32);
for (var i = 0; i < calculator.length; i++) {
text(' ' + calculator[i], 25, (400 + (i * 40)) - (calculator.length * 40));
}
}

function button(b) {
if (b == '=') {
if (calculator[calculator.length - 1] == '' && calculator.length > 2) {
calculator[calculator.length - 1] = calculator[calculator.length - 3];
}
var peices = splitTokens(calculator[calculator.length - 1], ['+', minusReplacement, '*', '/']);
var mafs = splitTokens(calculator[calculator.length - 1], ['a', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
//console.log(peices);
//console.log(mafs);
var total = peices[0];
if (total == 'a') {
if (calculator.length > 2) {
total = calculator[calculator.length - 2];
} else {
total = 0;
}
}
for (var i = 0; i < mafs.length; i++) {
if (mafs[i] == '+') {
total = int(total) + int(peices[i + 1]);
}
if (mafs[i] == minusReplacement) {
total = int(total) - int(peices[i + 1]);
}
if (mafs[i] == '*') {
total = int(total) * int(peices[i + 1]);
}
if (mafs[i] == '/') {
total = int(total) / int(peices[i + 1]);
}
}
//console.log(total);
calculator[calculator.length] = total;
calculator[calculator.length] = '';
} else if (b == 'cc') {
calculator = [];
calculator[0] = '';
} else if (b == 'c') {
calculator[calculator.length - 1] = '';
}
/*else if(b == 'a' && commands.length > 1) {
commands[commands.length-1] = str(commands[commands.length-1]) + str(commands[commands.length-2]);
}else if(b == 'a') {
commands[commands.length-1] = str(commands[commands.length-1]) + 0;
}*/
else if (b == '-') {
calculator[calculator.length - 1] = calculator[calculator.length - 1] + minusReplacement;
} else {
calculator[calculator.length - 1] = calculator[calculator.length - 1] + b;
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
On your Ubtuntu machine use your text editor to create a text file with the following...
On your Ubtuntu machine use your text editor to create a text file with the following content (3 points) I am using the ______ text editor. I am ready to start configuring TCP/IP servers show step by step with images there is no realtion to tcp, it's just that using the text editor on ubtunu you have to show those results.
PART B- Javascript Using a text editor (VS Code or Notepad) and a web browser, you...
PART B- Javascript Using a text editor (VS Code or Notepad) and a web browser, you will demonstrate how to create an HTML file, externally link a JavaScript file, and write some source code in the JavaScript file. a..Create two blank files to be an HTML file and a JavaScript file. The file names should be partA.html and partA.js. b.. Create a basic HTML webpage structure. c..Link the JavaScript file to the HTML file using the <script> tag. d.. Prompt...
Please Use JavaScript and P5 3) Password Protected Create an application with two input fields and...
Please Use JavaScript and P5 3) Password Protected Create an application with two input fields and one button. When the button is clicked, verify the user has written these (exact) strings in the first and second field: First field should be "Username" Second field should be "Password" If the two fields match, update a DIV on the page with the text "Success" or else "Wrong information". For this assignment: use only one if statement to to the check. Remember you'll...
Create a webpage with CSS Box Model: Create a basic web page using the 5 required...
Create a webpage with CSS Box Model: Create a basic web page using the 5 required html tags. Add paragraph tags and enclosed them with a div element. Using the CSS Box Model, style the div element to have a total width of 350px and a height of 250px. It should have 10px padding on all sides, a gray border of 5px on all sides, and 15px margins on both sides but none on the top and bottom. Add a...
create a Letter to the Editor of a local or national newspaper on an issue related...
create a Letter to the Editor of a local or national newspaper on an issue related to immigration. This should be short and to the point, featuring a detailed argument to persuade the appropriate target audience. The assignment should be no longer than ½ page typed. My topic is on deportation
Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be some sentence Using linked list class not STL. main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers....
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. It doesn't need an HTML form, all the arguments are put in through the method. The class must check for a correct operator (+,*,-,/), and a...
Your manager has asked you to create a web portal for your company’s customers, where they...
Your manager has asked you to create a web portal for your company’s customers, where they can place orders for the products and services they wish to purchase. Today, customers call and place orders with your company’s call center, where professional customer service representatives perform the order entry. Your first thought is to web-enable the system used by the customer service reps. What problems and challenges might you face if you simply made an order entry application designed for professional...
How to create the multiple class in Java? Please elaborate it by using the example.
How to create the multiple class in Java? Please elaborate it by using the example.
Solve using a financial calculator: please show each step you use on the financial calculator! 1....
Solve using a financial calculator: please show each step you use on the financial calculator! 1. Given the following information, calculate the NPV for this property. Initial cash outflow: $200,000, Discount rate: 15%, CF for year 1: $25,876, CF for year 2: $23,998, CF for year 3: $23,130, CF for year 4: $22,105, CF for year 5: $144,670. Draw your cash flow diagram and show your steps used in financial calculator. 2. Suppose you purchased an income producing property for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT