【发布时间】:2023-02-24 03:59:08
【问题描述】:
我正在参加 JavaScript 中的 Coursera 课程的实验室测验。
目前,我没有通过这个测验,即使我在 chrome 控制台输出并且 node.js 满足要求。请帮忙!
// 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) {
for(let i = 0; i < dishData.length; i++){
let finalPrice
if(taxBoolean === true){
finalPrice = dishData[i]['price'] * tax
} else if(taxBoolean === false){
finalPrice = dishData[i]['price']
} else {
console.log("You need to pass a boolean to the getPrices call!")
return
}
console.log("Dish: " + dishData[i]['name'] + "Price: $" + finalPrice)
}
}
// Implement getDiscount()
function getDiscount(taxBoolean, guests) {
getPrices(taxBoolean)
if (typeof guests == "number" && guests > 0 && guests < 30){
let discount = 0
if(guests < 5){
discount = 5
} else {
discount = 10
}
console.log('Discount is: $' + discount)
} else {
console.log('The second argument must be a number between 0 and 30')
}
}
// Call getDiscount()
当我提交这个时,结果是:
但是当我在节点中运行相同的代码时,输出是:
为什么我的输出中有“\n”?我怎样才能让它输出预期的输出?
【问题讨论】:
-
也许它想要在名称和价格之间留一个空格?
dishData[i]['name'] + " Price: $" -
.....你是对的......我怎么会错过这个
标签: javascript node.js