0

EDIT: Thanks to someone's response, I got a portion working. It now adds topping prices no problem. However, it adds a price even when a user doesn't put toppings into the prompt. I've updated my code below.

EDIT 2: For full code, here's my github link. It's updated with the below code (and the rest of the coding) https://github.com/cas389/m4-hw6-ju-cheryl.

I'm in a JavaScript class, and I am stuck on this portion of my homework assignment. Basically, we are creating a pizza ordering system where a user would fill out if they want thin/thick/pan crust, which toppings they want, if they want extra cheese and finally if they want delivery.

I have everything except the pizza toppings part. This is the information for this section (including the comments the teacher gave us as "hints") along with what I've been trying to make work. I've cut out all the sections I've already finished.

function getPizzaOrder() {
  var extraCheeseUpcharge = 1.5
  var thickCrustUpcharge = 2
  var deliveryFee = 3.5
  var toppingsFee = 1.5
  var basePrice = 10

  var toppings = prompt("Please enter additional toppings (comma separated)")
      // HINT: prompt() will return an empty string "" if the user presses 'OK' without entering a value
      // if the user enters toppings, use .split(",") to separate toppings into an array
      // if no toppings are given, make sure pizza.toppings is set to []
      // if the user has added toppings, add toppingsFee multiplied by
      // the number of toppings added to pizza.cost
      // YOUR CODE HERE
    
       pizza.toppings = [];
       pizza.toppings = toppings.split(',');
       for (let i = 0; i < pizza.toppings.length; i++){
           pizza.cost += toppingsFee;
       }
       console.log(pizza.toppings);




  return pizza
}
4
  • 3
    pizza.cost =+ pizza.toppings * toppingsFee is using =+ instead of +=. You also need to access the correct topping in the loop using the index (i). Commented Feb 8, 2022 at 19:55
  • Yeah =+ causes in-flight cast to ineger - stackoverflow.com/questions/10160850/… Commented Feb 8, 2022 at 20:06
  • As hinted by the professor, before trying to split the toppings, you most likely need an if clause to check if the toppings input is an empty string or not. - if it is an empty string, you will not need to use .split(',') and maybe give a message. A technique that might be useful to you is using a debugger, or just plain print debugging with a lot of console.log Commented Feb 8, 2022 at 20:21
  • @BaoHuynhLam thank you! I thought I needed an if / else statement (but when I asked my teacher he said we didn't) but I just went ahead and added one and I got it to work along with some other changes suggested. Thank you so much!! Commented Feb 8, 2022 at 21:02

2 Answers 2

0

Access element through index. And first of all You should declare pizza

let pizza;
pizza.toppings = [];
      pizza.toppings = toppings.split(',');
      for (let i = 0; i < pizza.toppings.length; i++){
       pizza.cost += +pizza.toppings[i] * toppingsFee;
      }
Sign up to request clarification or add additional context in comments.

Comments

0

I figured it out with some help of the comments. I needed to create an if / else statement around my for statement :) I got it to work now!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.