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.
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 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
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...
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...
Having a plan for your paper is important! You may choose to create a web, an...
Having a plan for your paper is important! You may choose to create a web, an outline, or any other form of prewriting. This can help you create a well-rounded paper. Explore the following resource: Writing a Persuasive/Argumentative Essay through this week's learning activity, which helps you to create a Persuasion Map for your writing. How might the persuasion map you created help when writing your paper? How does this assist you in organizing your writing? Please aim for min....
Using the Web or other resources, find an article that describes a successful Web service(s) software...
Using the Web or other resources, find an article that describes a successful Web service(s) software and the hardware necessary to support this application. Describe how the company implemented Web service(s) software and hardware and explain why using this combination of software and hardware is better than using an alternative approach to solve the problem
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...
Using a web browser , search for term "disaster recovery plan."identify three or four examples of...
Using a web browser , search for term "disaster recovery plan."identify three or four examples of what appear to be comprehensive plans, different from the samples presented in this chapter. What do these have in common? Create an outline for a DR plan using these examples.
Using key terms, create a scenario where you can find a population, a sample, the parameter,...
Using key terms, create a scenario where you can find a population, a sample, the parameter, the statistic, a variable, and data. Post your scenario and provide a detailed description using the key terms. After reviewing the information, what did you learn about your target population? (No plagiarism please,although it can be found easily on web but write in your own words.)