我希望我的代码返回'无效年龄',如果函数中给出的参数不是数字,而是获得系统referenceError.any谁可以帮忙?
function canIWatch(age){
this.age = age
if ( age >=1 && age <= 6 ) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if (age >= 7 && age <= 17){
return "You must be accompanied by a guardian who is 21 or older.";
}
else if (age >= 18 && age <= 24){
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 if (typeof age !== 'number'){
return "Invalid age.";
}
}
以下代码是通过以下测试。 编辑过的代码;
function canIWatch(age){
this.age = age
if (typeof age !== 'number' || age <=0){
return "Invalid age.";
}
else if ( age >=1 && age <= 6 ) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if (age >= 7 && age <= 17){
return "You must be accompanied by a guardian who is 21 or older.";
}
else if (age >= 18 && age <= 24){
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!";
}
}
该功能的测试代码;
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.');});});
问题是;
Deadpool是一部R级电影。 编写一个名为canIWatch的JavaScript函数,它将age作为参数。 如果年龄小于6岁,则返回您不得在下午6点之后观看Deadpool。 如果年龄为6岁或以上但不足17岁,则必须由21岁或以上的监护人陪同。 如果年龄为17岁或以上但小于25岁,则返回您可以在显示一些身份证后立即观看Deadpool。 如果年龄在25岁或以上,请退回!您可以在没有任何附加条件的情况下观看Deadpool! 如果年龄无效,请返回无效年龄。
答案 0 :(得分:1)
你走了。您的代码不起作用,因为该函数会自动将字符串解析为整数。只需在控制台中输入即可自行尝试:'123' > 23
或false < 32
。
使用数学运算符将始终将其组件解析为整数。这就是为什么验证必须在第一个位置。
age = '12';
if (typeof age !== 'number') {
console.log("Invalid age.");
} else if (age >= 1 && age <= 6) {
console.log("You are not allowed to watch Deadpool after 6.00pm.");
} else if (age >= 7 && age <= 17) {
console.log("You must be accompanied by a guardian who is 21 or older.");
} else if (age >= 18 && age <= 24) {
console.log("You are allowed to watch Deadpool, right after you show some ID.");
} else if (age >= 25) {
console.log("Yay! You can watch Deadpool with no strings attached!");
}
&#13;
答案 1 :(得分:0)
如果传入的值不是数字,那么它肯定不会达到&gt; = 1或其他任何情况,那么在测试它时真的没有必要。如果你将你的数字类型的测试移到最上面你的功能我倾向于解决你的问题:
function canIWatch(age){
if (typeof age !== 'number') return "Invalid age.";
this.age = age
if ( age >=1 && age <= 6 ) {
return "You are not allowed to watch Deadpool after 6.00pm.";
}
else if (age >= 7 && age <= 17){
return "You must be accompanied by a guardian who is 21 or older.";
}
else if (age >= 18 && age <= 24){
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!";
}
}
答案 2 :(得分:0)
function canIWatch(age) {
if (age >= 1 && age <= 6) {
return "You are not allowed to watch Deadpool after 6.00pm.";
} else if (age >= 7 && age <= 17) {
return "You must be accompanied by a guardian who is 21 or older.";
} else if (age >= 18 && age <= 24) {
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 if (isNaN(age)) {
return "Invalid age.";
} else if (!age) {
return "Age must be informed.";
}
}
function checkMyAge() {
var age = document.querySelector('#ageInput').value;
alert(canIWatch(age));
}
&#13;
<input id="ageInput" type="text" />
<button onclick="checkMyAge()">Check</button>
&#13;