gpt4 book ai didi

javascript - 我该如何使其更加“复杂”?

转载 作者:行者123 更新时间:2023-11-28 04:00:16 27 4
gpt4 key购买 nike

首先,我刚刚开始自己​​学习JS,没有任何编程经验,这意味着我仍然要了解这种出色的编程语言的基本构建模块。

我的问题与我编写的以下代码有关:



  let orderCount = 0;

const takeOrder = (size, beverage, milk, flavouring) => {
console.log("Order: A " + size + " " + beverage + " made of " + milk + " with added " flavouring");
orderCount++
};

const getTotal = (itemCount) => {
return itemCount * 1000;
};

takeOrder('large', 'latte', 'soy milk', 'caramel syrup');
takeOrder('medium', 'cappuchino', 'coconut milk', 'vanilla syrup');
takeOrder('small', 'latte macchiato', 'lactose free milk', 'sugar-free hazelnut syrup');


console.log(getTotal(orderCount));





如您所知,这是关于接受订单。目前,每笔订单的价格为1000(匈牙利福林),这是我想要更改的价格。我的意思是,例如,如果某个订单包含椰子奶,那么我想将价格提高160。

最佳答案

您可以使用object作为价格,这很容易扩展。



const takeOrder = (size, beverage, milk, flavouring) => {
console.log("Order: A " + size + " " + beverage + " made of " + milk + " with added " + flavouring);
total += data[beverage][size] + (data[milk] || 0) + (data[flavouring] || 0);
// ^^^^ use a default value if the key does
// not exist in the object or is falsy
orderCount++;
};

var data = {
latte: {
large: 1000
},
cappuchino: {
medium: 1000
},
'latte macchiato': {
small: 1000
},
'coconut milk': 160
},
orderCount = 0,
total = 0;

takeOrder('large', 'latte', 'soy milk', 'caramel syrup');
takeOrder('medium', 'cappuchino', 'coconut milk', 'vanilla syrup');
takeOrder('small', 'latte macchiato', 'lactose free milk', 'sugar-free hazelnut syrup');

console.log(orderCount);
console.log(total);

.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 我该如何使其更加“复杂”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com