関数で指定されたパラメータが数値ではなく、代わりにシステムの参照を取得している場合、私のコードは 'invalid age'を返すようにしたいと思います。javascriptの関数のパラメータtypeofを設定する
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.";
}
}
以下のコードは、以下のテストに合格することです。 編集コード。
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!";
}
}
機能のテストコード。
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.');});});
質問があります。
DeadpoolはR定格の映画です。 年齢をパラメータとするcanIWatchというJavaScript関数を作成します。 年齢が6歳未満の場合は、午後6時以降にDeadpoolを視聴することはできません。 年齢が6歳以上17歳未満の場合は、21歳以上の保護者を同伴する必要があります。 年齢が17歳以上25歳未満の場合は、IDを表示した直後にDeadpoolを見ることができます。 年齢が25歳以上の場合は、Yay!あなたは紐が付いていないDeadpoolを見ることができます! 年齢が無効な場合は、無効な年齢を返します。
を、今取得しています私のコードはすべてのテストを通過しないので、私のコードは送信できません...しかし、私はそれをテストするとき、function.iが質問にテストコードを追加しました。本当に説明を感謝します。 – owezzy
@owezzy私はこれらのテストが何であるか分かりません。おそらくコードワー用の新しいカタを作っていますか? –
宿題の割り当て、機能は、テストを渡すことです、私は質問を投稿させてください。 – owezzy