Javascript函数程序

Javascript function program

本文关键字:程序 函数 Javascript      更新时间:2023-09-26

有人能帮我结账吗?我仍然可以提交这个,即使通过了所有五个测试。

问题

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

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

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

如果年龄小于6岁,返回。下午6点以后不准看《死侍》

6岁及以上未满17岁者,回程须由21岁及以上监护人陪同。

如果年龄是17岁或以上但小于25岁,返回。你可以看死侍,只要你出示身份证。

如果年龄大于等于25岁,返回Yay!你可以免费观看《死侍》!

如果年龄不合法,返回invalid age。

Unittest

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(age > 0 && 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 && age < 200) {
        return "Yay! You can watch Deadpool with no strings attached!";
    }
    else {
        return "Invalid age.";
    }
}

变化:

else if(age > 6 && age < 17) {

:

else if(age < 17) {

,其他else if语句也类似。

当年龄正好为6时,它不匹配之前的age < 6,也不匹配age > 6 && age < 17。如果你想检查年龄至少6岁,它应该是age >= 6。但是你不需要这样做,因为前面的if在age小于6时已经运行了,所以当你到达这个else if时,你可以保证age至少是6。

年龄小于1岁的还需要一开始的case,无效。年龄小于200岁的,不需要在结尾处核对。

function canIWatch(age) {
    if (age <= 0) {
        return "Invalid age.";
    }
    else if(age < 6) {
        return "You are not allowed to watch Deadpool after 6.00pm.";
    }
    else if(age < 17) {
        return "You must be accompanied by a guardian who is 21 or older.";
    }
    else if(age < 25) {
        return "You are allowed to watch Deadpool, right after you show some ID.";
    }
    else {
        return "Yay! You can watch Deadpool with no strings attached!";
    }
}

这是对基于测试的Andela问题的完美回答。

function canIWatch(age) {
    if (age <= 0) {
        return "Invalid age.";
    }
    else if(age < 6) {
        return "You are not allowed to watch Deadpool after 6.00pm.";
    }
    else if(age < 17) {
        return "You must be accompanied by a guardian who is 21 or older.";
    }
    else if(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.";
    }
}
function canIWatch(age){
    if (age <= 0) {
        return "Invalid age.";
    }
    else if(age < 6) {
        return "You are not allowed to watch Deadpool after 6.00pm.";
    }
    else if(age < 17) {
        return "You must be accompanied by a guardian who is 21 or older.";
    }
    else if(age < 25) {
        return "You are allowed to watch Deadpool, right after you show some          ID.";
    }
    else  {
        return "Yay! You can watch Deadpool with no strings attached!";
    }
  }