2016-10-16 2 views
-1

関数で指定されたパラメータが数値ではなく、代わりにシステムの参照を取得している場合、私のコードは '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を見ることができます! 年齢が無効な場合は、無効な年齢を返します。

答えて

1

ここに移動します。関数が自動的に文字列を整数に解析するため、コードが機能しませんでした。コンソールに入力することで自分で試してみてください:'123' > 23またはfalse < 32

算術演算子を使用すると、常にそのコンポーネントが整数に解析されます。そのため、検証は最初の位置になければなりません。

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

+0

を、今取得しています私のコードはすべてのテストを通過しないので、私のコードは送信できません...しかし、私はそれをテストするとき、function.iが質問にテストコードを追加しました。本当に説明を感謝します。 – owezzy

+0

@owezzy私はこれらのテストが何であるか分かりません。おそらくコードワー用の新しいカタを作っていますか? –

+0

宿題の割り当て、機能は、テストを渡すことです、私は質問を投稿させてください。 – owezzy

0

に渡された値が、それは間違いなく> = 1にはないだろうか、それをテスト時点での必要は本当にありませんどのような数ではない場合。あなたが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!"; 
    } 
} 
0

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>

関連する問題