简体   繁体   English

从事一项任务,我只是不理解某些东西,但不确定我错过了什么(可能很多)

[英]working on an assignment and I am just not understanding something but not sure what I'm missing (probably alot)

Hey guys i'm working on an assignment and i am having difficulty but not sure what it is I am missing.嘿伙计们,我正在做一项任务,我遇到了困难,但不确定我错过了什么。 it keeps throwing an error on Step 4它在第 4 步不断抛出错误

I am not really sure how to structure the code to satisfy the requirements for Step 4我不太确定如何构建代码以满足第 4 步的要求

and and all help is appreciated here.并且在这里感谢所有帮助。

Assignment Instructions In this exercise, you will work with some data provided as an array of objects, listing information about dishes available in the Little Lemon restaurant.分配说明 在本练习中,您将使用作为对象数组提供的一些数据,列出有关 Little Lemon 餐厅中可用菜肴的信息。

Step 1: In the function getPrices(), give it the parameter of taxBoolean.步骤 1:在 function getPrices() 中,给它 taxBoolean 参数。

Step 2: Inside the getPrices() function, code a for loop that will loop over all the objects inside the dishData array.第 2 步:在 getPrices() function 中,编写一个 for 循环,该循环将遍历 discData 数组中的所有对象。

Step 3: Inside the for loop, declare a finalPrice variable, without assigning it a value.第 3 步:在 for 循环中,声明一个 finalPrice 变量,但不为其赋值。

Step 4: Still inside the for loop, add an if condition, checking that the taxBoolean is set to true.第 4 步:仍然在 for 循环中,添加一个 if 条件,检查 taxBoolean 是否设置为 true。 Inside the if block, multiply the following: * the price of the currently looped-over object from the dishData array, and * the tax value.在 if 块中,乘以以下内容: * 来自 discData 数组的当前循环的 object 的价格,以及 * 税值。 Assign the multiplied value to the finalPrice variable.将相乘后的值赋给 finalPrice 变量。

Step 5: Right after the if condition, add an else if, checking if the value of taxBoolean is false.第 5 步:在 if 条件之后,添加一个 else if,检查 taxBoolean 的值是否为 false。 Inside this condition's block, assign the currently looped-over dish price property in the dishData array to the finalPrice variable.在此条件的块中,将 discData 数组中当前循环的菜价格属性分配给 finalPrice 变量。

Step 6: Code the else case, and inside of it, add two lines of code:第 6 步:编写 else 案例,并在其中添加两行代码:

A console log of the string:字符串的控制台日志:

"You need to pass a boolean to the getPrices call!" “您需要将 boolean 传递给 getPrices 调用!”

return (to "jump out" of the further function execution)返回(“跳出”进一步的 function 执行)

Step 7: After all the conditional's statements, but still inside the for loop, code another console log with four arguments:第 7 步:在所有条件语句之后,但仍在 for 循环中,使用四个 arguments 编写另一个控制台日志:

The string "Dish: "字符串“Dish:”

The value of currently looped-over dish object's name property当前循环的菜对象的名称属性的值

The string "Price: $"字符串“价格:$”

The value of the finalPrice variable finalPrice 变量的值

Step 8: You're finshed with the getPrices() function, and now you're ready to code another function.第 8 步:您已完成 getPrices() function,现在您可以编写另一个 function。 Give the getDiscount() function, two parameters: the taxBoolean and the guests parameter.给getDiscount() function,两个参数:taxBoolean和guests参数。

Step 9: Inside the getDiscount() function, on the very first line of its body, invoke the getPrices() function, passing it the taxBoolean as an argument.第 9 步:在 getDiscount() function 中,在其主体的第一行,调用 getPrices() function,将 taxBoolean 作为参数传递给它。

Step 10: On another line, you need to implement your defensive coding skills, and check that the type of the guests parameter is 'number' and that the value of the guests variable is greater than zero and less than 30. If all these conditions return true, code the body of the conditional as described in the next step. Step 10:在另外一行,你需要执行你的防御性编码技巧,并检查guests参数的类型是'number'并且guests变量的值大于零小于30。如果所有这些条件返回 true,按照下一步中的说明对条件的主体进行编码。 If they don't all return true, code the body of the else conditional as instructed in step 12.如果它们不都返回 true,请按照步骤 12 中的说明对 else 条件的主体进行编码。

Step 11: Inside the if statment, declare a new variable, named discount, and set it to 0. On the next line, add another if...else if: in the first if, you'll check that the value of the guests variable is less than 5. If that's the case, reassign the value of the discount variable to 5;第 11 步:在 if 语句中,声明一个名为 discount 的新变量,并将其设置为 0。在下一行,添加另一个 if...else if:在第一个 if 中,您将检查guest 变量小于 5。如果是这种情况,则将 discount 变量的值重新分配为 5;

Inside the else if condition, check that the value of the guests variable is greater than or equal to 5 - if that's the case, reassign the discount variable to 10. Console log the following after closing your else-if statement: 'Discount is: $' + discount);在 else if 条件中,检查 guest 变量的值是否大于或等于 5 - 如果是这种情况,请将 discount 变量重新分配为 10。控制台在关闭 else-if 语句后记录以下内容:'折扣是: $' + 折扣); Step 12: In the else condition, console log the following string: 'The second argument must be a number between 0 and 30'.第 12 步:在 else 条件下,控制台记录以下字符串:“第二个参数必须是 0 到 30 之间的数字”。 Since you've finished declaring both the getPrices() and the getDiscount() functions, you can now invoke the getDiscount() function several times, with various combinations of arguments, to check the behavior.由于您已经完成了 getPrices() 和 getDiscount() 函数的声明,现在可以使用 arguments 的各种组合多次调用 getDiscount() function 来检查行为。

Here are two examples:这里有两个例子:

getDiscount(true, 2) getDiscount(false, 10) What happens when you don't pass-in any arguments? getDiscount(true, 2) getDiscount(false, 10) 如果没有传入任何 arguments 会发生什么?

What happens when you pass values that are not expected?当您传递不期望的值时会发生什么?

// 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)

Try this when taxed:征税时试试这个:

var finalPrice = 0;
dishData.forEach((dish)=>{
  finalPrice+=dish.price*tax;
})

Just remove the multiplication by tax when not being taxed, or if you want to simplify the code further try a one line if for taxBoolean :只需在不征税时删除乘以税,或者如果您想简化代码,请进一步尝试一行 if for taxBoolean

var finalPrice = 0;
dishData.forEach((dish)=>{
  finalPrice+=dish.price * (taxBoolean ? tax : 1);
})

This makes it so you don't check for taxBoolean when getting the final price.这使得您在获得最终价格时无需检查taxBoolean Regarding the if statements in GetPrices you only need an if for true and an else for false , unless its some null or undefined type variable.关于GetPrices中的 if 语句,您只需要一个 if 代表true和一个 else 代表false ,除非它的一些 null 或未定义的类型变量。

If for some reason you want to return the array of taxed prices instead of the total taxed price, use the map function for the dishData array.如果出于某种原因您想要返回已纳税价格数组而不是总已纳税价格,请使用map function 作为dishData数组。

var finalPrice = dishData.map((dish)=>dish.price*(taxBoolean ? tax : 1));

dishData is an array of objects not an object itself, so you need to iterate over it and add the price property multiplied with tax . dishData是一个对象数组,而不是 object 本身,因此您需要对其进行迭代并添加price属性乘以tax Consider renaming dishData to dishes or something plural to show that its an array.考虑将dishData重命名为dishes或复数以显示它是一个数组。

ALSO do not re-declare finalPrice either remove the var finalPrice = "" line or change it to var finalPrice=0;也不要重新声明finalPrice删除var finalPrice = ""行或将其更改为var finalPrice=0; and remove the other var finalPrice = 0;并删除另一个var finalPrice = 0; in the if statements.在 if 语句中。

figured it out with a little help from a friend.在朋友的帮助下想通了。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript for()。 我错过了什么吗,或者这不是这样工作 - Javascript for(). am I missing something, or this just not working this way 我不确定是否要滥用SwitchNavigator或只是缺少一些东西 - I'm unsure if I'm misusing the SwitchNavigator or just missing something 函数没有定义,但是我很确定我定义了它。我想念什么? - function not defined, but i'm quite sure i defined it.. What am i missing? 我正在尝试使用js将多个功能和按钮连接在一起。 和html。 我不确定我缺少什么 - I am trying to connect multiple functions and buttons together using js. and html. and I'm not sure what i'm missing 理解数组时我错过了什么? - What am I missing with the understanding of Arrays? setTimeout不起作用,我在这里错过了什么吗? - setTimeout not working,am I missing something here? 了解 switch 语句在 JS 中是如何工作的——我错过了什么吗? - Understanding how the switch statement works in JS - am i missing something? 有些东西在我的 html 中添加了一个跨度,我不确定是什么 - something is adding a span to my html and I'm not sure what 我是否正确了解For Loops? 我想念什么? -JavaScript - Am I understanding For Loops correctly? What am I missing? - JavaScript 使用 parseCSV 理解 promises async/await,我错过了什么? - Understanding promises async/await using parseCSV, what am I missing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM