如何在javascript

时间:2017-04-24 16:12:58

标签: javascript

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

根据R级电影的描述:17岁以下的儿童需要陪同的父母或成人监护人(21岁或以上),25岁及以下的成年人必须出示身份证。 6点以下的儿童不得在下午6点之后进入。

Deadpool是一部R级电影。

编写一个名为canIWatch的JavaScript函数,该函数将age作为参数。

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

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

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

如果年龄在25岁以上,请退回!您可以在没有附加条件的情况下观看Deadpool!。

如果年龄无效,请返回无效年龄。

我已经编写了以下代码,但仍然无法通过测试,我做错了什么?

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点之后观看Deadpool。'等于'年龄无效。'。

4 个答案:

答案 0 :(得分:0)

else移除else..if,包括isNaN检查无效年龄。

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 :(得分:0)

我建议你添加支票以确保年龄是一个数字:

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

所以我改写了你的代码:

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

我也格式化了你的代码。

所以最终版本是:

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

答案 2 :(得分:0)

这样的事应该可以正常工作。我刚刚删除了else if条件。

&#13;
&#13;
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"));
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

我会把你的最后一句话作为:

else (age === undefined) {
 return "Invalid age";
}