【问题标题】:JS output has "strings"\n"strings" instead of actually creating a new line - Coursera little lemon receipt makerJS 输出有 \"strings\"\\n\"strings\" 而不是实际创建一个新行 - Coursera little lemon receipt maker
【发布时间】: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


【解决方案1】:

更改此行:

console.log("Dish: " + dishData[i]['name'] + "Price: $" + finalPrice)

对此:

console.log("Dish: " + dishData[i]['name'] + " Price: $" + finalPrice)

现在您可以提交该代码

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

【讨论】:

    【解决方案2】:

    console.log("菜肴:" + dishData[i]['name'] + "价格:$" + finalPrice)

    将上面的行更改为此, 它会成功的

    控制台日志("Dish: "${dishData[i]['name']}"Price: $" ${finalPrice}

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
    【解决方案3】:
    // 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.2;
    
    // Implement getPrices()
    function getPrices(taxBoolean) {
      for (let i = 0; i < dishData.length; i++) {
        let finalPrice;
        if (taxBoolean === true) {
          finalPrice = dishData[i].price * tax;
          console.log(`Dish: ${dishData[i].name} Price: $${finalPrice}`);
        } else if (taxBoolean === false) {
          finalPrice = dishData[i].price;
          console.log(`Dish: ${dishData[i].name} Price: $${finalPrice}`);
        } else {
          console.log('You need to pass a boolean to the getPrices call!');
          return;
        }
      }
    }
    
    // Implement getDiscount()
    function getDiscount(taxBoolean, guests) {
      getPrices(taxBoolean);
      if (!isNaN(guests) && guests > 0 && guests < 30) {
        let discount = 0;
        if (guests < 5) {
          discount = 5;
        } else if (guests >= 5) {
          discount = 10;
        } else {
          console.log(`${guests} is not a number!`);
        }
        console.log(`Discount is: $${discount}`);
      } else {
        console.log(`The second argument must be a number between 0 and 30`);
      }
    }
    
    // Call getDiscount()
    getDiscount(true, 6);
    

    【讨论】:

    • 经过测试的代码,在 coursera 上的评分为 100%。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多