在javascript中为函数设置参数typeof

在javascript中为函数设置参数typeof,javascript,function,Javascript,Function,如果函数中给出的参数不是一个数字,我希望我的代码返回'invalid age',相反,我得到的是一个系统引用错误。有人能帮忙吗 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 &&

如果函数中给出的参数不是一个数字,我希望我的代码返回'invalid age',相反,我得到的是一个系统引用错误。有人能帮忙吗

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.";
  }
} 
问题是,

《死亡之池》是一部R级电影。 编写一个名为canIWatch的JavaScript函数,该函数将年龄作为参数。 如果年龄小于6岁,那么在下午6点之后,您不允许观看Deadpool。 如果您的年龄为6岁或以上但小于17岁,您必须由21岁或以上的监护人陪同。 如果年龄在17岁或以上但小于25岁,在出示身份证后,您可以观看Deadpool。 如果年龄在25岁或以上,返回Yay!您可以观看没有附加任何字符串的死池!。 如果年龄无效,请返回无效年龄


给你。您的代码无法工作,因为函数会自动将字符串解析为整数。您只需在控制台中键入:
'123'>23
false<32
即可

使用数学运算符总是将其组件解析为整数。这就是为什么验证必须放在第一位

age='12';
如果(年龄类型!=='number'){
console.log(“无效年龄”);
}否则,如果(年龄>=1&&age=7&&age=18&&age=25){
log(“耶!你可以在没有附加任何字符串的情况下观看死池!”);

}
如果传入的值不是一个数字,那么它肯定不会>=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!";
  }
} 
功能表(年龄){
如果(年龄类型!=='number')返回“无效年龄”;
这个年龄
如果(年龄>=1&&age=7&&age=18&&age=25){
return“耶!你可以在没有任何附加条件的情况下观看Deadpool!”;
}
} 
功能表(年龄){
如果(年龄>=1&&age=7&&age=18&&age=25){
return“耶!你可以在没有任何附加条件的情况下观看Deadpool!”;
}否则如果(isNaN(年龄)){
返回“无效年龄”;
}否则,如果(!年龄){
return“必须告知年龄。”;
}
}
函数checkMyAge(){
var age=document.querySelector(“#ageInput”).value;
警觉(年龄);;
}


检查
我已经编辑了代码,但是现在收到一条错误消息,我的代码无法提交,因为它没有通过所有测试…但是当我测试它的功能时。我已经在问题中添加了测试代码。我非常感谢您的解释。@owezzy我只是不知道这些测试是什么。你是在为codewars做一个新的kata吗?这是一个家庭作业,函数是通过测试,让我来发帖。@owezzy但是你的家庭作业没有提到测试代码,你只需要写一个函数。而且很可能是你做的。测试在我应该提交的网站上进行。
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 (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!";
  }
}