This is my professor's assignment, to make a pizza menu: The price of the pizza will be $10 plus extra for each topping: cheese: $2; tomato $1, mushrooms $3 and peppers $1
use prompts to select three toppings of the five into an array
use a for loop and if tests to compute the total price of the pizza
use an alert to report your toppings and the array
use another alert to report your bill and the price
what I don't understand is how to use a 'for loop' to compute the prices for all the toppings the user entered through the prompt
here is my js code so far:
var pizza = 10
var toppings = [];
var price = [];
var t1 = "cheese";
var p1 = 2;
var t2 = "tomato";
var p2 = 1;
var t3 = "mushrooms";
var p3 = 3;
var t4 = "peppers";
var p4 = 1;
toppings.push(t1);
toppings.push(t2);
toppings.push(t3);
toppings.push(t4);
price.push(p1);
price.push(p2);
price.push(p3);
price.push(p4);
var meow = [];
var m1 = prompt("Choose a desired topping: cheese, tomato, mushrooms, or peppers");
var m2 = prompt("Choose another desired topping: cheese, tomato, mushrooms, or peppers");
var m3 = prompt("Choose another desired topping: cheese, tomato, mushrooms, or peppers");
meow.push(m1);
meow.push(m2);
meow.push(m3);
var total = [];
var desired = [];
for (var i = 0; i < toppings.length; i++) {
for (var i = 0; i < price.length; i++) {
desired.push(toppings[i] + price[i]);
}
}
total.push(desired + pizza);
alert(total);
Your solution is almost there, because there's only a couple of small changes you need to to make to get your code working:
var total[]
array use ordinary variable: var total
for the outer loop use the meow
array instead of toppings
array
(optional) you can either use price
or toppings
array as the inner
array
var pizza = 10
var toppings = [];
var price = [];
var t1 = "cheese";
var p1 = 2;
var t2 = "tomato";
var p2 = 1;
var t3 = "mushrooms";
var p3 = 3;
var t4 = "peppers";
var p4 = 1;
toppings.push(t1);
toppings.push(t2);
toppings.push(t3);
toppings.push(t4);
price.push(p1);
price.push(p2);
price.push(p3);
price.push(p4);
var meow = [];
var m1 = prompt("Choose a desired topping: cheese, tomato, mushrooms, or peppers");
var m2 = prompt("Choose another desired topping: cheese, tomato, mushrooms, or peppers");
var m3 = prompt("Choose another desired topping: cheese, tomato, mushrooms, or peppers");
meow.push(m1);
meow.push(m2);
meow.push(m3);
var total = 0;
var desired = [];
for (var i = 0; i < meow.length; i++) {
for (var j = 0; j < toppings.length; j++) {
if (meow[i] == toppings[j])
total = total + price[j];
}
}
//total.push(desired + pizza);
alert("total " + total);
Please note I think it's a good practice to have different variables for inner and outer for loops: j
and i