please format code with </> button * homework policy * asking questions
Just confused on how I would make this instruction given:
You are going to create an app to help you determine what percentage you should tip someone based on the quality of service you received.
You are to pick at least three (3) places to tip and create an app that will allow the user to enter the amount of their bill and the quality of service they received. There will be an output displaying the amount you should tip and the total amount of your bill and the tip.
With each place to tip, you are going to change an image to match that area, it can either be the entire background or just a portion of the screen
Need help fast!
micuat
2
Iām confused too. Is this like homework or are you starting a business? 
its like a homework question
1 Like
micuat
4
ok I thought you run a startup company or something
then you should tag āhomeworkā too: FAQ - Processing Foundation
and as stated in the link, you canāt ask for the āanswerā. Do you have something you tried already? If not did they provide some code to start with? If you have completely no idea, I recommend to follow tutorials such as the coding train to how to get started.
2 Likes
right now i have added the basic format like looks for the app with a title and added some drop-down menus its just the actual calculating using percentages adding total amounts after the percentage is added having a final price of the bill too.
this is my current code right now im trying to use the drop down menu to change the background to the according restaurant in the drop down menu but keep getting errors please help:
var pickEvent;
var m, bp, n; // variables to hold my images
var img;
var day = āwhatā;
var sel;
var sel1;
// preload some images
function preload() {
m = loadImage(āmandarin.jpgā);
bp = loadImage(ābostonpizza.jpgā);
n = loadImage(ānandos.jpgā);
}
function setup() {
let inp = createInput(āā);
inp.position(312, 175);
inp.size(100);
textAlign(CENTER);
background(200);
sel = createSelect();
sel.position(315, 110);
sel.option(āMandarinsā);
sel.option(āNandosā);
sel.option(āBoston Pizzaā);
sel.selected(āResturauntā);
sel.changed(pickEvent);
textAlign(CENTER);
background(200);
sel1 = createSelect();
sel1.position(325, 245);
sel1.option(āBadā);
sel1.option(āGoodā);
sel1.option(āExcellentā);
sel1.selected(āServiceā);
sel1.changed(pickEvent);
createCanvas(900, 500);
background(153);
}
function draw() {
textSize(36);
text(āTip Calculatorā, 350, 50);
fill(50, 50, 50);
image(img, 0, 0);
}
function pickEvent() {
//get the item chosen with .value()
day = picker.value();
if (day === āMandarinsā) {
img = m;
} else if (day === āNandosā) {
img = n;
} else if (day === āBoston Pizzaā) {
img = bp;
}
}
micuat
7
ok thatās a good start - however I see you posting from email, but I recommend using the web platform and use </> button to format the code. Itās hard to read it like this.
Also what exactly are the errors?
not exactly sure what the errors are they arenāt very specific it just shows āscript errorā but im guessing it might have to do with the āpicker. valueā because itās pointing at that line.
micuat
9
do you have a screenshot?
micuat
13
sorry I had to copy the url from your screenshot to try the code. when I run the āproductionā link, I get error at image(img, 0, 0), which I guess, itās because until pickEvent is fired, img remains empty so that it cannot draw anything.
by the way itās a bit weird that repl.it shows āscript errorā message. I wonder if theres a way to show proper error messageā¦
ok thanks what would i do to fix this? should I change āimgā value and what should i do it to?
Great progress here!
Basically you can check if img != null
micuat
16
yes basically img is not defined at first, so either
- initialize
img in setup by assiging one of the images
- or skip
image() if img is not defined
The latter is what Chrisir suggested, but strictly speaking, when a variable is not defined yet, it is undefined so you should test like img !== undefined
could u show me an example on how it would work and look like im still a bit confused
micuat
18
Basically in your code what happens is this:
var img;
image(img, 0, 0);
It doesnāt work because img is empty. One solution is to initialize img with an image that you preloaded:
function setup() {
img = m;
}
But this would mean that there will be an image by default. Another solution is to āonlyā draw if img is not empty
function draw() {
if (img !== undefined) {
image(img, 0, 0);
}
}
This wonāt draw anything by default.
1 Like
so how would i get it to work if i select an option from the dropdown menu and each selection from the drop-down menu has its own image?
micuat
20
Isnāt that what the code is doing? Did you write the code you posted by yourself? (Iām not judging, just asking)
yes i wrote the whole thing i was just wondering would this make the image change when i change the selection from drop-down?
micuat
22
Can you try it by yourself? 