JS方法测试失败排查及代码修正请求
getPrices与getDiscount函数测试失败排查
我的代码
// 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 (const product of dishData) { var finalPrice; if (taxBoolean == true) { finalPrice = product.price * tax; } else if (taxBoolean == false) { finalPrice = product.price; } else { console.log("You need to pass a boolean to the getPrices call!"); return } console.log(`Dish: ${product.name} Price $${finalPrice}`); } } // Implement getDiscount() function getDiscount(taxBoolean, guests) { getPrices(taxBoolean); if (typeof (guests == 'number') && (guests < 30 && guests > 0)) { var 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'); } } // Call getDiscount() getDiscount(true, 2); getDiscount(false, 10);
测试失败详情
测试用例1:taxBoolean为true时getPrices应添加税费
- 我的输出:
Dish: Italian pasta Price $11.46 Dish: Rice with veggies Price $10.38 Dish: Chicken with potatoes Price $18.66 Dish: Vegetarian Pizza Price $7.74 - 预期输出:
Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74
- 我的输出:
测试用例2:taxBoolean为false时getPrices不应添加税费
- 我的输出:
Dish: Italian pasta Price $9.55 Dish: Rice with veggies Price $8.65 Dish: Chicken with potatoes Price $15.55 Dish: Vegetarian Pizza Price $6.45 - 预期输出:
Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45
- 我的输出:
测试用例3:添加税费且客人少于5时的getDiscount
- 我的输出:
Dish: Italian pasta Price $11.46 Dish: Rice with veggies Price $10.38 Dish: Chicken with potatoes Price $18.66 Dish: Vegetarian Pizza Price $7.74 Discount is: $5 The second argument must be a number between 0 and 30 - 预期输出:
Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 Discount is: $5
- 我的输出:
测试用例4:添加税费且客人多于5时的getDiscount
- 我的输出多了错误提示,预期无该提示,且价格格式需修正。
测试用例5:不添加税费且客人少于5/多于5时的getDiscount
- 同样存在价格格式错误和多余错误提示的问题。
测试用例6:客人数量不在范围内时的getDiscount
- 仅价格格式错误,其余输出符合预期。
挑战要求
- 为
getPrices()函数添加taxBoolean参数。 - 在
getPrices()内编写for循环遍历dishData数组所有对象。 - 在for循环内声明
finalPrice变量,不赋值。 - 添加if条件判断
taxBoolean为true时,将当前菜品价格与tax相乘赋值给finalPrice。 - 添加else if判断
taxBoolean为false时,将当前菜品价格直接赋值给finalPrice。 - else分支输出错误信息并终止函数执行。
- 在for循环内输出格式为:
Dish: [菜品名] Price: $[最终价格]。 - 编写
getDiscount()函数,添加taxBoolean和guests两个参数。 - 函数第一行调用
getPrices()并传入taxBoolean。 - 防御性检查:
guests类型为number,且值在0-30之间,否则执行else分支。 - 符合条件时,根据客人数量设置折扣(少于5为5,大于等于5为10)并输出折扣信息。
- else分支输出指定错误信息。
问题修复方案
问题1:价格输出格式错误
在getPrices()函数的console.log语句中,Price后缺少冒号,需修改为:
console.log(`Dish: ${product.name} Price: $${finalPrice}`);
问题2:getDiscount中的条件判断语法错误
typeof使用错误:原代码typeof (guests == 'number')是判断表达式结果的类型,正确写法应为typeof guests === 'number'。else后多余分号:原代码else; { ... }中的分号会导致else块失效,后面的代码块无条件执行,需去掉分号。
修改后的getDiscount()函数:
function getDiscount(taxBoolean, guests) { getPrices(taxBoolean); if (typeof guests === 'number' && guests > 0 && guests < 30) { var 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'); } }
完整修复后代码
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 (const product of dishData) { var finalPrice; if (taxBoolean === true) { finalPrice = product.price * tax; } else if (taxBoolean === false) { finalPrice = product.price; } else { console.log("You need to pass a boolean to the getPrices call!"); return; } console.log(`Dish: ${product.name} Price: $${finalPrice}`); } } function getDiscount(taxBoolean, guests) { getPrices(taxBoolean); if (typeof guests === 'number' && guests > 0 && guests < 30) { var 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);
内容的提问来源于stack exchange,提问作者Sar
相关产品推荐
相关产品推荐

