Javascript函数程序
Javascript function program
有人能帮我结账吗?我仍然可以提交这个,即使通过了所有五个测试。
问题根据r级电影的描述:17岁以下的儿童需要父母或21岁以上的成年监护人陪同,25岁及以下的成年人必须出示身份证。6点以后,6岁以下儿童不得入内。
《死侍》是一部限制级电影。
编写一个名为canIWatch的JavaScript函数,将age作为参数。
如果年龄小于6岁,返回。下午6点以后不准看《死侍》
6岁及以上未满17岁者,回程须由21岁及以上监护人陪同。
如果年龄是17岁或以上但小于25岁,返回。你可以看死侍,只要你出示身份证。
如果年龄大于等于25岁,返回Yay!你可以免费观看《死侍》!
如果年龄不合法,返回invalid age。
Unittest
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.');
});
});
这是我的解决方案:
function canIWatch(age) {
if(age > 0 && age < 6) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if(age > 6 && age < 17) {
return "You must be accompanied by a guardian who is 21 or older.";
}
else if(age > 17 && age < 25) {
return "You are allowed to watch Deadpool, right after you show some ID.";
}
else if(age > 25 && age < 200) {
return "Yay! You can watch Deadpool with no strings attached!";
}
else {
return "Invalid age.";
}
}
变化:
else if(age > 6 && age < 17) {
:
else if(age < 17) {
,其他else if
语句也类似。
当年龄正好为6时,它不匹配之前的age < 6
,也不匹配age > 6 && age < 17
。如果你想检查年龄至少6岁,它应该是age >= 6
。但是你不需要这样做,因为前面的if
在age小于6时已经运行了,所以当你到达这个else if
时,你可以保证age至少是6。
年龄小于1岁的还需要一开始的case,无效。年龄小于200岁的,不需要在结尾处核对。
function canIWatch(age) {
if (age <= 0) {
return "Invalid age.";
}
else if(age < 6) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if(age < 17) {
return "You must be accompanied by a guardian who is 21 or older.";
}
else if(age < 25) {
return "You are allowed to watch Deadpool, right after you show some ID.";
}
else {
return "Yay! You can watch Deadpool with no strings attached!";
}
}
这是对基于测试的Andela问题的完美回答。
function canIWatch(age) {
if (age <= 0) {
return "Invalid age.";
}
else if(age < 6) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if(age < 17) {
return "You must be accompanied by a guardian who is 21 or older.";
}
else if(age < 25) {
return "You are allowed to watch Deadpool, right after you show some ID.";
}
else if(age >= 25) {
return "Yay! You can watch Deadpool with no strings attached!";
}
else {
return "Invalid age.";
}
}
function canIWatch(age){
if (age <= 0) {
return "Invalid age.";
}
else if(age < 6) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if(age < 17) {
return "You must be accompanied by a guardian who is 21 or older.";
}
else if(age < 25) {
return "You are allowed to watch Deadpool, right after you show some ID.";
}
else {
return "Yay! You can watch Deadpool with no strings attached!";
}
}
相关文章:
- 如何从另一个处理程序内部取消JavaScript事件处理程序函数的执行
- 使用jqGrid列格式化程序函数使ng-click工作
- GAS:在处理程序函数中使用变量时,如何更改全局变量值并保留其更改后的值
- AJAX没有使用成功处理程序函数
- extjs中事件处理程序函数中的THIS上下文
- 为什么document.ready中的事件处理程序函数有效但无效;取出后不起作用
- 确定公开JS实用程序函数的最佳模式
- 我在哪里放置我的帮助程序函数
- 在命名处理程序函数中使用 $(this) & event
- 模拟 Web 浏览器方向更改事件以使用 Jasmine 测试绑定事件处理程序函数
- 带参数的移相器按钮处理程序函数
- Angular.js:为什么使用简单的帮助程序函数改变$scope会导致未插值的 {{..}} 表达式
- 循环引用似乎在事件处理程序函数中不起作用
- 在帮助程序函数中使用 for 循环的正确方法 |流星
- 如何通过jQuery $.load()实用程序函数获取PHP将utf-8字符串传递给JavaScript
- JavaScript 事件处理程序函数范围控件
- 何时有时将对象类型定义为将实例绑定方法镜像为构造函数上的静态实用程序函数很有用
- jQuery调用其他事件处理程序中的事件处理程序函数
- 使用 Jasmine 测试 Meteor 帮助程序函数时出错
- 原型方法中的事件处理程序函数,为什么它认为.keyCode是JavaScript中未定义的属性