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

Solutions

Expert Solution

var commands = [];
var minusReplacement = '_';


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

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

function button(b) {
  if (b == '=') {
    if (commands[commands.length-1] == '' && commands.length>2) {
      commands[commands.length-1] = commands[commands.length-3];
    }
    var peices = splitTokens(commands[commands.length-1], ['+',minusReplacement,'*','/']);
    var mafs = splitTokens(commands[commands.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 (commands.length>2) {
        total = commands[commands.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);
    commands[commands.length] = total;
    commands[commands.length] = '';
  }else if(b == 'cc') {
    commands = [];
    commands[0] = '';
  }else if(b == 'c') {
    commands[commands.length-1] = '';
  }else if(b == '-') {
    commands[commands.length-1] = commands[commands.length-1] + minusReplacement;
  }else{
    commands[commands.length-1] = commands[commands.length-1] + b;
  }
}

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. PLEASE MAKE SURE IT WORKS
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