我有这个代码。它返回错误“运行脚本时出错”。我在终端上测试并给出了所需的结果。
function canIWatch(age) {
var newAge = parseInt(age);
var response;
switch (true) {
case (isNaN(newAge) || newAge <= 0):
response = "Invalid Age.";
break;
case (newAge < 6):
response = "You are not allowed to watch Deadpool after 6.00pm.";
break;
case (newAge >= 6 && newAge < 17):
response = "You must be accompanied by a guardian who is 21 or older.";
break;
case (newAge >= 17 && newAge < 25):
response = "You are allowed to watch Deadpool, right after you show some ID.";
break;
case (newAge >= 25):
response = "Yay! You can watch Deadpool with no strings attached!";
break;
}
return response;
}
这是单元测试代码。我不能编辑这个。
describe('canIWatch tests', function () {
it('Should return the appropriate message for age less than 6', function () {
expect(canIWatch(5)).toEqual('You are not allowed to watch Deadpool after 6.00pm.');
});
it('Should return the appropriate message for age less than 17', function () {
expect(canIWatch(15)).toEqual('You must be accompanied by a guardian who is 21 or older.');
});
it('Should return the appropriate message for age less than 25', function () {
expect(canIWatch(20)).toEqual('You are allowed to watch Deadpool, right after you show some ID.');
});
it('Should return the appropriate message for age above 25 than 6', function () {
expect(canIWatch(30)).toEqual('Yay! You can watch Deadpool with no strings attached!');
});
it('should return an appropriate message if provided age is invalid', function () {
expect(canIWatch(-1)).toEqual('Invalid age.');
});
});
我尝试使用if-else循环并仍然得到相同的错误。