rocconz rocconz - 25 days ago 4
Javascript Question

Javascript code showing errors "use strict" and '' is already defined

Here is my jsfiddle link https://jsfiddle.net/yr7ffuLu/7/

I'm trying to make a javascript/html menu for school but my javascript is showing some errors. Can anyone help me? Dreamweaver says I need to put "use strict"; but when I do the code doesn't work anymore, and Dreamweaver also tells me that 'standard' and 'gourmet' are already defined (used three times in the code) but when I remove the other two instances where I use them, it won't display the order details.

var totalStandardNumber = 0;
var totalGourmetNumber = 0;
var standardPrice = 9.50;
var gourmetPrice = 15.50;
var deliveryPrice = 5;
var orderTotalPrice = 0;

function order() {
var standard = [];
standard[0] = document.form.hawaiian.value;
standard[0] = Number(standard[0]);
standard[1] = document.form.cheese.value;
standard[1] = Number(standard[1]);
standard[2] = document.form.veggie.value;
standard[2] = Number(standard[2]);
standard[3] = document.form.supreme.value;
standard[3] = Number(standard[3]);
standard[4] = document.form.pepperoni.value;
standard[4] = Number(standard[4]);
totalStandardNumber = standard[0] + standard[1] + standard[2] + standard[3] + standard[4];
var gourmet = [];
gourmet[0] = document.form.meatlovers.value;
gourmet[0] = Number(gourmet[0]);
gourmet[1] = document.form.chicken.value;
gourmet[1] = Number(gourmet[1]);
gourmet[2] = document.form.prawn.value;
gourmet[2] = Number(gourmet[2]);
totalGourmetNumber = gourmet[0] + gourmet[1] + gourmet[2];
if (isNaN(standard[0]) || isNaN(standard[1]) || isNaN(standard[2]) || isNaN(standard[3]) || isNaN(standard[4]) || isNaN(gourmet[0]) || isNaN(gourmet[1]) || isNaN(gourmet[2])) {
alert("Oops! You can only enter numeric values for pizza quantities. Please re-order.");
location.reload(true);
} else if (standard[0] === 0 && standard[1] === 0 && standard[2] === 0 && standard[3] === 0 && standard[4] === 0 && gourmet[0] === 0 && gourmet[1] === 0 && gourmet[2] === 0) {
alert("Oops! You did not enter a pizza quantity. Please re-order.");
location.reload(true);
} else if (totalStandardNumber + totalGourmetNumber > 12) {
orderTotalPrice = (+totalStandardNumber + totalGourmetNumber);
alert("Oops! Limit of 12 pizzas per order, you have ordered " + orderTotalPrice + ". Please re-order.");
location.reload(true);
} else
alert("Order details:" + "\n" +
"Standard Hawaiian Pizza: " + standard[0] + "\n" +
"Standard Cheese Pizza: " + standard[1] + "\n" +
"Standard Veggie Pizza: " + standard[2] + "\n" +
"Standard Supreme Pizza: " + standard[3] + "\n" +
"Standard Pepperoni Pizza: " + standard[4] + "\n" +
"Gourmet Meat-lovers Pizza: " + gourmet[0] + "\n" +
"Gourmet Chicken Pizza: " + gourmet[1] + "\n" +
"Gourmet Prawn Pizza: " + gourmet[2]);
calculate();
}

function calculate() {
orderTotalPrice = (totalStandardNumber * standardPrice + totalGourmetNumber * gourmetPrice);
var optionalDelivery = prompt("Would you like your order delivered?");
while (optionalDelivery !== "YES" && optionalDelivery !== "yes" && optionalDelivery !== "NO" && optionalDelivery !== "no") {
optionalDelivery = prompt("Please specify if you would like your order delivered. Enter YES or NO:");
}
if (optionalDelivery === "YES" || optionalDelivery === "yes") {
orderTotalPrice = (+orderTotalPrice + deliveryPrice);
alert("You have chosen to have your order delivered. This will incur a $5 delivery fee.");
var deliveryName = prompt("Please enter your name:");
while (deliveryName === "" || deliveryName === null) {
deliveryName = prompt("Please enter your name.");
}
var deliveryAddress = prompt("Please enter a delivery address:");
while (deliveryAddress === "" || deliveryAddress === null) {
deliveryAddress = prompt("Please enter a delivery address.");
}
var deliveryPhone = prompt("Please enter your phone number:");
while (deliveryPhone >= 0 && deliveryPhone <= 8600000 || deliveryPhone >= 9999999) {
deliveryPhone = prompt("Please enter a 7-digit phone number between 8600000-9999999.");
}
var standard = [];
standard[0] = document.form.hawaiian.value;
standard[0] = Number(standard[0]);
standard[1] = document.form.cheese.value;
standard[1] = Number(standard[1]);
standard[2] = document.form.veggie.value;
standard[2] = Number(standard[2]);
standard[3] = document.form.supreme.value;
standard[3] = Number(standard[3]);
standard[4] = document.form.pepperoni.value;
standard[4] = Number(standard[4]);
var gourmet = [];
gourmet[0] = document.form.meatlovers.value;
gourmet[0] = Number(gourmet[0]);
gourmet[1] = document.form.chicken.value;
gourmet[1] = Number(gourmet[1]);
gourmet[2] = document.form.prawn.value;
gourmet[2] = Number(gourmet[2]);
alert("Customer details:" + "\n\n" +
"Customer's name: " + deliveryName + "\n" +
"Customer's address: " + deliveryAddress + "\n" +
"Customer's phone number: " + deliveryPhone + "\n\n" +
"Order details:" + "\n" +
"Standard Hawaiian Pizza: " + standard[0] + "\n" +
"Standard Cheese Pizza: " + standard[1] + "\n" +
"Standard Veggie Pizza: " + standard[2] + "\n" +
"Standard Supreme Pizza: " + standard[3] + "\n" +
"Standard Pepperoni Pizza: " + standard[4] + "\n" +
"Gourmet Meat-lovers Pizza: " + gourmet[0] + "\n" +
"Gourmet Chicken Pizza: " + gourmet[1] + "\n" +
"Gourmet Prawn Pizza: " + gourmet[2] + "\n\n" +
"Total cost:" + "\n" +
"$" + orderTotalPrice.toFixed(2) + " including a $" + deliveryPrice + " delivery fee.");
var checkOrder = prompt("Are the order details correct?");
while (checkOrder !== "YES" && checkOrder !== "yes" && checkOrder !== "NO" && checkOrder !== "no") {
checkOrder = prompt("Please specify if the order details are correct. Enter YES or NO:");
}
if (checkOrder === "YES" || checkOrder === "yes") {
alert("Thank you for ordering online at Pete's Pizza.");
location.reload(true);
} else if (checkOrder === "NO" || checkOrder === "no") {
alert("Your order has been recognised as incorrect and has been canceled. Please re-order.");
location.reload(true);
}
} else if (optionalDelivery === "NO" || optionalDelivery === "no") {
orderTotalPrice = (+orderTotalPrice);
alert("You have chosen to pick-up your order.");
var pickupName = prompt("Please enter your name:");
var standard = [];
standard[0] = document.form.hawaiian.value;
standard[0] = Number(standard[0]);
standard[1] = document.form.cheese.value;
standard[1] = Number(standard[1]);
standard[2] = document.form.veggie.value;
standard[2] = Number(standard[2]);
standard[3] = document.form.supreme.value;
standard[3] = Number(standard[3]);
standard[4] = document.form.pepperoni.value;
standard[4] = Number(standard[4]);
var gourmet = [];
gourmet[0] = document.form.meatlovers.value;
gourmet[0] = Number(gourmet[0]);
gourmet[1] = document.form.chicken.value;
gourmet[1] = Number(gourmet[1]);
gourmet[2] = document.form.prawn.value;
gourmet[2] = Number(gourmet[2]);
alert("Customer details:" + "\n\n" +
"Customer's name: " + pickupName + "\n\n" +
"Order details:" + "\n" +
"Standard Hawaiian Pizza: " + standard[0] + "\n" +
"Standard Cheese Pizza: " + standard[1] + "\n" +
"Standard Veggie Pizza: " + standard[2] + "\n" +
"Standard Supreme Pizza: " + standard[3] + "\n" +
"Standard Pepperoni Pizza: " + standard[4] + "\n" +
"Gourmet Meat-lovers Pizza: " + gourmet[0] + "\n" +
"Gourmet Chicken Pizza: " + gourmet[1] + "\n" +
"Gourmet Prawn Pizza: " + gourmet[2] + "\n\n" +
"Total cost:" + "\n" +
"$" + orderTotalPrice.toFixed(2) + ". Order is for pick-up.");
}
}


Thank you in advance.

Answer

You are declaring variables standard and gourmet twice in your calculate function.

Just delete var standard = [] and var gourmet = [] used second time in the function.

If you would like to start with an empty array, start with standard = [] and gourmet = [] (emitting the var declaration)

However, I think this is more of a Dreamweaver thing than the strict mode.

Also, refer this answer to how scope of the variable can be block or function.

https://jsfiddle.net/BoyWithSilverWings/yr7ffuLu/5/