嘿伙计们,我正在做一项作业,我遇到了困难,但不确定我错过了什么。它在第 4 步时不断抛出错误
我不太确定如何构建代码以满足步骤 4 的要求
并且感谢所有帮助。
分配说明 在本练习中,您将使用一些作为对象数组提供的数据,列出有关 Little Lemon 餐厅提供的菜肴的信息。
第1步:在函数getPrices()中,传入taxBoolean参数。
第 2 步:在 getPrices() 函数内,编写一个 for 循环,该循环将循环遍历ishDishData 数组中的所有对象。
第 3 步:在 for 循环内,声明一个 FinalPrice 变量,但不为其赋值。
第 4 步:仍然在 for 循环内,添加 if 条件,检查taxBoolean 是否设置为 true。在 if 块内,乘以以下内容: * 盘数据数组中当前循环对象的价格,以及 * 税值。将相乘后的值赋给 FinalPrice 变量。
第5步:在if条件后面添加一个else if,检查taxBoolean的值是否为假。在此条件的块内,将dishData 数组中当前循环的菜肴价格属性分配给finalPrice 变量。
第6步:编写else case,并在其中添加两行代码:
字符串的控制台日志:
“您需要将布尔值传递给 getPrices 调用!”
return(“跳出”进一步的函数执行)
第 7 步:在所有条件语句之后,但仍在 for 循环内,使用四个参数编写另一个控制台日志:
字符串“Dish:”
当前循环的菜品对象的 name 属性的值
字符串“价格:$”
finalPrice 变量的值
第 8 步:您已经完成了 getPrices() 函数,现在您可以编写另一个函数了。给 getDiscount() 函数,两个参数:taxBoolean 和 guest 参数。
第 9 步:在 getDiscount() 函数内部的第一行,调用 getPrices() 函数,并将taxBoolean 作为参数传递给它。
第10步:在另一行中,您需要实施您的防御性编码技巧,并检查guests参数的类型是否为“number”,并且guests变量的值是否大于0且小于30。如果所有这些条件返回 true,按下一步所述对条件主体进行编码。如果它们没有全部返回 true,请按照步骤 12 中的说明对 else 条件的主体进行编码。
第11步:在if语句中,声明一个新变量,名为discount,并将其设置为0。在下一行,添加另一个if...else if:在第一个if中,您将检查guest 变量小于 5。如果是这种情况,请将discount 变量的值重新赋值为 5;
在 else if 条件中,检查 guest 变量的值是否大于或等于 5 - 如果是这种情况,请将折扣变量重新分配为 10。 关闭 else-if 语句后控制台记录以下内容:“折扣为:$”+折扣); 步骤 12:在 else 条件下,控制台记录以下字符串:“第二个参数必须是 0 到 30 之间的数字”。由于您已完成 getPrices() 和 getDiscount() 函数的声明,因此您现在可以使用各种参数组合多次调用 getDiscount() 函数来检查行为。
这里有两个例子:
getDiscount(true, 2) 获取折扣(假,10) 当你不传入任何参数时会发生什么?
当您传递非预期值时会发生什么?
// Given variables
const dishData = [
{
name: "Italian pasta",
price: 9.55
},
{
name: "Rice with veggies",
price: 8.65
},
{
name: "Chicken with potatoes",
price: 15.55
},
{
name: "Vegetarian Pizza",
price: 6.45
},
]
const tax = 1.20;
// Implement getPrices()
function getPrices(taxBoolean) { //step1
let finalPrice = "";
for (var i = 0; i < dishData.length; i++) { // step 2
let finalPrice = 0 // step 3
if (taxBoolean == true) { // step 4
let name = dishData[i]['name'];
let price = dishData[i]['price']; // Step 4
//console.log('Prices with 20% tax:');
console.log(`Dish: ${name}, Price (incl. tax): $${+price * tax}`);
} else if (taxBoolean == false) { //step 5
let name = dishData[i]['name'];
let price = dishData[i]['price'];
console.log('Prices without tax:'); {
console.log(`Dish: ${name}, Price (incl. tax): $${+price}`);
}
} else { //step 6
console.log("You need to pass a boolean to the getPrices call!")
return;
}
}
}
console.log(getPrices(true));
console.log(getPrices(false));
// Implement getDiscount()
function getDiscount(taxBoolean, guests) { //Step 8
getPrices(taxBoolean); //Step 9
try { //Step 10
if (typeof (guests) != 'number' && (guests) < 0 && (guests) > 30) { //Step 11
var discount = 0;
if (typeof (guests) < 5) {
discount = 5;
}
else if (typeof (guests) >= 5) {
discount = 10;
console.log('Discount is: $' + discount);
}
else { //step 12
console.log('the second argument must be a number between 0 and 30')
}
}
} catch (err) { // Code throws error
alert('catch', err);
}
}
//Call getDiscount();
getDiscount(true, 2);
getDiscount(false, 10)
您的 getPrices()
console.log
(Dish: ${name} Price: $${finalPrice}
) 控制台可以出现在 ifelse
之后。
function getPrices(taxBoolean) {
for (let i = 0; i < dishData.length; i++) {
let finalPrice = "";
let name = dishData[i].name;
let price = dishData[i].price;
if (taxBoolean === true) {
finalPrice = price * tax;
} else if (taxBoolean === false) {
finalPrice = price;
} else {
console.log("You need to pass a boolean to the getPrices call!");
return;
}
console.log(`Dish: ${name} Price: $${finalPrice}`);
}
}
// Implement getDiscount()
function getDiscount(taxBoolean, guests) {
getPrices(taxBoolean);
try {
if (typeof guests === "number" && guests > 0 && guests < 30) {
let discount = 0;
if (guests < 5) {
discount = 5;
} else if (guests >= 5) {
discount = 10;
}
console.log("Discount is: $" + discount);
} else {
console.log("The second argument must be a number between 0 and 30");
}
} catch (error) {
console.log(error);
}
}
// Call getDiscount()
getDiscount(true, 2);
getDiscount(false, 10);
纳税时试试这个:
var finalPrice = 0;
dishData.forEach((dish)=>{
finalPrice+=dish.price*tax;
})
只需在不纳税时删除乘以税,或者如果您想进一步简化代码,请尝试一行 if for
taxBoolean
:
var finalPrice = 0;
dishData.forEach((dish)=>{
finalPrice+=dish.price * (taxBoolean ? tax : 1);
})
这样您在获得最终价格时就无需检查
taxBoolean
。关于 GetPrices
中的 if 语句,您只需要 true
的 if 和 false
的 else,除非它是一些 null 或未定义的类型变量。
如果由于某种原因您想要返回含税价格数组而不是总含税价格,请对
map
数组使用 dishData
函数。
var finalPrice = dishData.map((dish)=>dish.price*(taxBoolean ? tax : 1));
dishData
是一个对象数组,而不是对象本身,因此您需要迭代它并添加 price
属性乘以 tax
。考虑将 dishData
重命名为 dishes
或复数形式以表明它是一个数组。
也不要重新声明
finalPrice
,或者删除 var finalPrice = ""
行或将其更改为 var finalPrice=0;
并删除 if 语句中的其他 var finalPrice = 0;
。
在朋友的帮助下解决了这个问题。
function getPrices(taxBoolean) { //step1
for (var i = 0; i < dishData.length; i++) { // step 2
let finalPrice = 0 // step 3
if (taxBoolean == true) { // step 4
let name = dishData[i]['name'];
let price = dishData[i]['price']; // Step 4
finalPrice = price * tax
console.log(`Dish: ${name}` + " " + `Price: $${finalPrice}`)
} else if (taxBoolean == false) { //step 5
let name = dishData[i]['name'];
let price = dishData[i]['price'];
//console.log('Prices without tax:');
finalPrice = price
console.log(`Dish: ${name}` + " " + `Price: $${finalPrice}`)
} else { //step 6
console.log("You need to pass a boolean to the getPrices call!")
return;
}
}
}
console.log(getPrices(true));
console.log(getPrices(false));
// Implement getDiscount()
function getDiscount(taxBoolean, guests) { //Step 8
getPrices(taxBoolean); //Step 9
try { //Step 10
if (typeof(guests) == 'number' && guests > 0 && guests < 30) { //Step 11
let discount = 0;
if (guests < 5) {
discount = 5;
console.log('Discount is: $' + discount);
} else if (guests >= 5) {
discount = 10;
console.log('Discount is: $' + discount);
}
} else { //step 12
console.log('the second argument must be a number between 0 and 30');
}
} catch (err) { // Code throws error
console.log('catch', err);
}
}
//Call getDiscount();
console.log(getDiscount(true, 2));
console.log(getDiscount(false, 10));
// Given variables
const dishData = [
{
name: "Italian pasta",
price: 9.55
},
{
name: "Rice with veggies",
price: 8.65
},
{
name: "Chicken with potatoes",
price: 15.55
},
{
name: "Vegetarian Pizza",
price: 6.45
},
]
const tax = 1.20;
function getPrices(taxBoolean) {
for (let dish of dishData) {
let finalPrice;
if (taxBoolean) {
finalPrice = dish.price * tax;
} else if (!taxBoolean) {
finalPrice = dish.price;
} else {
console.log("You need to pass a boolean to the getPrices call!");
return;
}
console.log(`Dish: ${dish.name} Price: $${finalPrice}`);
}
}
function getDiscount(taxBoolean, guests) {
getPrices(taxBoolean);
if (typeof guests === 'number' && guests > 0 && guests < 30) {
let discount = 0;
if (guests < 5) {
discount = 5;
} else
if (guests >= 5) {
discount = 10;
}
console.log(`Discount is: $${discount}`);
} else {
console.log('The second argument must be a number between 0 and 30');
}
}
getDiscount(true, 2);
getDiscount(false, 10);