首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在javascript中捕获无效输入

如何在javascript中捕获无效输入
EN

Stack Overflow用户
提问于 2017-04-25 00:12:58
回答 4查看 1.1K关注 0票数 0

如何在javascript中实现以下逻辑:

根据限制级影片说明: 17岁以下儿童需有父母或成年监护人( 21周岁及以上)陪同,25周岁及以下成人需出示身份证,6岁以下儿童下午6点后不得入内。

《死穴》是一部限制级电影。

编写一个名为canIWatch的JavaScript函数,该函数将使用年龄作为参数。

如果年龄小于6岁,则返回您在下午6点后不能观看Deadpool。

如年龄在6岁或以上但不足17岁,则须由21岁或以上的监护人陪同。

如果年龄在17岁或以上但小于25岁,请在您出示身份证后立即返回您可以观看Deadpool。

如果年龄大于或等于25,则返回Yay!你可以在没有任何附加条件的情况下观看Deadpool!

如果age无效,则返回invalid age。

我已经写了下面的代码,但仍然不能通过测试,我做错了什么?

代码语言:javascript
复制
function canIWatch(age){
  if(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){
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
  else if(!age){
    return 'Invalid age.';
  }
}

我收到一个错误:如果提供的年龄无效,canIWatch测试应该返回相应的消息

预期是“下午6点以后你不能看死亡池”。设置为等于“无效年龄”。

EN

回答 4

Stack Overflow用户

发布于 2017-04-25 00:26:19

删除else..if处的else,包括isNaN检查是否存在无效的时间。

代码语言:javascript
复制
function canIWatch(age) {
  if (!age || isNaN(age) || !isNaN(age) && age <= 0) {
    return 'Invalid age.';
  }
  if (age < 6) {
    return 'You are not allowed to watch Deadpool after 6.00pm.';
  }
  if (age >= 6 && age < 17) {
    return 'You must be accompanied by a guardian who is 21 or older.';
  }
  if (age >= 17 && age < 25) {
    return 'You are allowed to watch Deadpool, right after you show some         ID.';
  }
  if (age >= 25) {
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
}

[5, 6, 16, 17, 25, void 0, null, -7, 0].forEach(function(age) {
  console.log(canIWatch(age))
});

票数 1
EN

Stack Overflow用户

发布于 2017-04-25 00:26:52

我建议您添加一个检查,以确保年龄是一个数字:

代码语言:javascript
复制
function canIWatch(age){
  if (typeof age !== 'number' || age < 0 || age % 1 > 0) {
    return 'Invalid age.';
  }

  if(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){
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
}

这里我检查数字的类型是否为年龄,年龄是否不是十进制数。

另请注意:如果您的每条语句都是只返回语句,那么您可能不会每次都编写else if

所以我重写了你的代码如下:

代码语言:javascript
复制
function canIWatch(age){
  if (typeof age !== 'number' || age < 0 || age % 1 > 0) {
    return 'Invalid age.';
  }

  if(age<6){
    return 'You are not allowed to watch Deadpool after 6.00pm.';
  }

  if(age>=6 && age<17){
    return 'You must be accompanied by a guardian who is 21 or older.';
  }

  if(age>=17 && age<25){
    return 'You are allowed to watch Deadpool, right after you show some         ID.';
  }

  if(age>=25){
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
}

我也格式化了你的代码。

所以最终的版本是:

代码语言:javascript
复制
function canIWatch(age) {
  if (typeof age !== 'number' || age < 0 || age % 1 > 0) {
    return 'Invalid age.';
  }

  if (age < 6) {
    return 'You are not allowed to watch Deadpool after 6.00pm.';
  }

  if (age >= 6 && age < 17) {
    return 'You must be accompanied by a guardian who is 21 or older.';
  }

  if (age >= 17 && age < 25) {
    return 'You are allowed to watch Deadpool, right after you show some         ID.';
  }

  if (age >= 25) {
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
}
票数 0
EN

Stack Overflow用户

发布于 2017-04-25 00:27:12

像这样的东西应该可以很好地工作。我只是在最后删除了else if条件。

代码语言:javascript
复制
function canIWatch(age){
  if(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){
    return 'Yay! You can watch Deadpool with no strings attached!';
  }
  else {
    return 'Invalid age.';
  }
}

console.log(canIWatch(4));
console.log(canIWatch(10));
console.log(canIWatch(22));
console.log(canIWatch(29));
console.log(canIWatch("invalid type"));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43593051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档