简体   繁体   English

在javascript中为函数设置参数typeof

[英]setting parameter typeof for a function in javascript

i would like my code to return 'invalid age' if the parameter given in the function is not a number,instead am getting a system referenceError.any one who can help out?如果函数中给定的参数不是数字,我希望我的代码返回“无效年龄”,而是收到系统引用错误。谁能提供帮助?

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.";
  }
} 

the code below is to pass the tests below.下面的代码是为了通过下面的测试。 edited code;编辑过的代码;

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!";
}
}

test codes for the function;功能的测试代码;

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.');});});

the question is;问题是;

Deadpool is an R-rated movie.死侍是R级电影。 Write a JavaScript function named canIWatch that will take age as a parameter.编写一个名为 canIWatch 的 JavaScript 函数,该函数将接受年龄作为参数。 If the age is less than 6, return You are not allowed to watch Deadpool after 6.00pm.如果年龄小于 6 岁,返回 6.00pm 之后不允许观看 Deadpool。 If the age is 6 or more but less than 17, return You must be accompanied by a guardian who is 21 or older.如果年龄在 6 岁或以上但未满 17 岁,返回 您必须由 21 岁或以上的监护人陪同。 If the age is 17 or more but less than 25, return You are allowed to watch Deadpool, right after you show some ID.如果年龄是 17 岁或以上但小于 25 岁,返回您可以在显示某些 ID 后立即观看 Deadpool。 If the age is 25 or greater, return Yay!如果年龄是 25 或更大,返回 Yay! You can watch Deadpool with no strings attached!.您可以无条件观看死侍! If the age is invalid, return Invalid age.如果年龄无效,则返回无效年龄。

Here you go.干得好。 Your code didn't work because the function automatically parses the string into an integer.您的代码不起作用,因为该函数会自动将字符串解析为整数。 Just try by yourself by typing in the console: '123' > 23 or false < 32 .只需在控制台中输入自己尝试: '123' > 23false < 32

Using mathematical operators will always parse it's components into integers.使用数学运算符将始终将其组件解析为整数。 That's why the validation have to be on the first position.这就是验证必须放在第一位的原因。

 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!"); }

If the value passed in isn't a number then it's definitely not going to >= 1 or whatever so there's really no need at the point testing it.如果传入的值不是数字,那么它肯定不会 >= 1 或其他任何值,因此实际上没有必要对其进行测试。 If you move your test for your typeof number up the top your function I'm tipping it will probably solve your problem:如果您将 typeof 数字的测试移到函数的顶部,我认为它可能会解决您的问题:

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!";
  }
} 

 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)); }
 <input id="ageInput" type="text" /> <button onclick="checkMyAge()">Check</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM