Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
User Prompt Array Question for Javascript

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
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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;
      }
about 4 years ago · Juan Pablo Isaza Report

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!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!