Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this exercise that I am struggling to comprehend the logic to achieve the outcome when qty is more than 3:

An application to calculate the price of pizzas (listed below) that will be purchased during a promotional period. The number of pizzas will be entered by the user.

  • One large pizza will cost $6.45.
  • Two large pizzas will cost $12.00.
  • Three large pizzas will cost $14.00.
  • Four or more pizzas will use a combination of the above prices to ensure the best price for the customer. For example, the best price for five pizzas would be two pizzas ($12.00) + three pizzas ($14.00).

The algorithm must also take account of all possible situations by using sequence, selection and iteration structures.

Below is my code so far:

let calcOrder = () => {
  // get inputs

  var qty = document.getElementById("quantity").value;
  var price = 6.45;
  var price2 = 12.0;
  var price3 = 14.0;
  var totalPrice;

  // validate missing, non-digit, negative inputs

  if (qty == "") {
    document.getElementById("message").innerHTML = "Missing input";
  } else if (isNaN(qty)) {
    document.getElementById("message").innerHTML = "Numbers only";
  } else if (qty < 0) {
    document.getElementById("message").innerHTML =
      "Negative numbers are not allowed";
  } else {
    //calc total
    if (qty == 1) 
    {
      totalPrice = price;
    }
    else if (qty == 2)
    {
      totalPrice = price2;
    }
    else if (qty == 3)
    {
      totalPrice = price3;
    }
    //output total

    document.getElementById("message").innerHTML =
      `Total price is $${totalPrice}`;
  }

  // prevent form from submission
  return false;
};

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
240 views
Welcome To Ask or Share your Answers For Others

1 Answer

you can use division and mod operation to calculate the price:

(this example assuming all check you did valid input are already done)

const qte = document.getElementById("quantity").value;
const price = 6.45;
  const price2 = 12.0;
  const price3 = 14.0;
  let totalPrice;

const total3 = parseInt("" + qte / 3 + "", 10) * price3;
const total2 = parseInt("" + (qte % 3) / 2 + "", 10) * price2;
const total1 = parseInt("" + ((qte % 3) % 2) + "", 10) * price;

totalPrice = total1 + total2 + total3;

document.getElementById("message").innerHTML =
      `Total price is $${totalPrice}`;

what is actually happening in the code

well it is basic mathematics, if you want to know how many 3s in your number you divide by 3 and take the integer part or floor of the result, basic division. for example 10 = 3*3 + 1 so you have 3 as a result of that division. since you only want to apply the price of 3 pizza as top priority you do this division then multiply by the price for 3.

then come the priority for the price of 2 pizzas, but you not interested for the total number of pizzas, only what was left after you payed with the price of 3 pizzas so you do the mod operator (%) with 3 to get was left unpaid, for example 8 = 3*2 + 2, this give us 2 pizzas left unpaid so you apply the price of 2 pizzas.

the you check if a single pizza is left after you paid for 2 pizzas (which would only happen if only a single pizza was left after you paid for 3). if there is single pizza you pay for it otherwise you add nothing.

ps: after paying for pizzas in multiple of three, you only add the price of 2 pizzas or a single one, but never both, otherwise price of 3 pizzas would apply instead.

hope the explanation is clear, if not leave a comment and i'll try to adjust what was not clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...