2015-06-04 16 views
4

是下面的代碼正確:(如何)我可以使用「或」 switch語句?

var user = prompt("Is programming awesome?").toUpperCase(); 
switch (user) { 
    case 'HELL YEAH' || 'YES' || 'YEAH' || 'YEP' || 'YEA': 
     console.log("That's what I'd expected!"); 
    break; 
    case 'KINDA'||'CANT SAY': 
     console.log("Oh, you'd like it when you'll be a pro!"); 
    break; 
    case 'HELL NO'||'NO'||'NAH'||'NOPE'||'NA': 
     console.log("You can't say that!"); 
    break; 
    default: 
     console.log("Wacha tryna say mate?"); 
    break; 
} 

我試了一下。但它沒有按照我的意願工作。它運作良好,如果輸入的是「該死的」或「地獄啊」,但所有後來的絲線都不理!

+0

簡單的回答是不,你不能。 – Andy

+0

分開那些||與案件 – AkshayJ

回答

6

我會使用含有你的答案字典(對象),然後你就可以用在功能檢查處理這switch語句。它比具有多例查驗整潔了很多:

var user = prompt("Is programming awesome?").toUpperCase(); // 'HELL YEAH'; 

// 'dictionary' is just a JS object that has key/value pairs 
// In this instance we have keys that map the case checks you were 
// making and each has a corresponding array for its value, filled with 
// the OR checks you wanted to make. 
// We pass 'dict' in as the second parameter when we call checkAnswer 
var dict = { 
    yes: ['HELL YEAH', 'YES', 'YEAH', 'YEP', 'YEA'], 
    maybe: ['KINDA', 'CANT SAY'], 
    no: ['HELL NO', 'NO', 'NAH', 'NOPE', 'NA'] 
}; 

// checkAnswer returns 'yes' for the user prompt 'HELL YEAH' 
// It loops over the 'dict' object and if it finds an instance of 
// your user input in one of the arrays it returns the key 
// in this instance 'yes' for 'HELL YEAH' (indexOf returns 0) 
// otherwise it returns false (for your 'default' case) 
function checkAnswer(user, dict) { 
    for (var p in dict) { 
     if (dict[p].indexOf(user) > -1) return p; 
    } 
    return false; 
} 

// All we do is use checkAnswer to return the key where 
// the user input is found in one of the dictionary arrays. 
// That will always be a single string which is what switch expects 
switch (checkAnswer(user, dict)) { 
    case 'yes': 
     console.log("That's what I'd expected!"); 
     break; 
    case 'maybe': 
     console.log("Oh, you'd like it when you'll be a pro!"); 
     break; 
    case 'no': 
     console.log("You can't say that!"); 
     break; 
    default: 
     console.log("Wacha tryna say mate?"); 
     break; 
} 

DEMO

+0

那麼,我目前正在學習JS。所以我根本無法理解你的代碼!但是,謝謝你。希望有一天它能幫助我! :) – Perceptioner

+0

我會更好地註釋該代碼以幫助您。 – Andy

+0

@Perceptioner,我已經更新了我的答案與一羣指針,以幫助你的未來。 – Andy

3

我猜你要做的:

var user = prompt("Is programming awesome?").toUpperCase(); 
    switch (user) { 
    case 'HELL YEAH': 
    case 'YES': 
    case 'YEAH': 
    case 'YEP': 
    case 'YEA': 
     console.log("That's what I'd expected!"); 
     break; 
    case 'KINDA': 
     ... 
} 

等等......

0

集團案件沒有破。

var user = prompt("Is programming awesome?").toUpperCase(); 
    switch (user) { 
     case 'HELL YEAH': 
     case 'YES': 
     case 'YEAH': 
     case 'YEP': 
     case 'YEA': 
      console.log("That's what I'd expected!"); 
     break; 
     case 'KINDA': 
     case 'CANT SAY': 
      console.log("Oh, you'd like it when you'll be a pro!"); 
     break; 
     case 'HELL NO': 
     case 'NO': 
     case 'NAH': 
     case 'NOPE': 
     case 'NA': 
      console.log("You can't say that!"); 
     break; 
     default: 
      console.log("Wacha tryna say mate?"); 
     break; 
    } 
1

JavaScript的開關有落空的行爲,你可以利用這個來獲得你所描述的:

var user = prompt("Is programming awesome?").toUpperCase(); 
switch (user) { 
    case 'HELL YEAH': 
    case 'YES': 
    case 'YEAH': 
    case 'YEP' 
    case 'YEA': 
     console.log("That's what I'd expected!"); 
     break; 
    case 'KINDA': 
    case 'CANT SAY': 
     console.log("Oh, you'd like it when you'll be a pro!"); 
     break; 
    case 'HELL NO': 
    case 'NO': 
    case 'NAH': 
    case 'NOPE': 
    case 'NA': 
     console.log("You can't say that!"); 
     break; 
    default: 
     console.log("Wacha tryna say mate?"); 
     break; 
} 

不花哨的使用運營商,但它的工作原理。 隨着落空,直到達到一些實際的代碼具體case的執行將下降跳過在同一組中的不匹配的。

0

我相信交換機能夠將案件匹配字符串,所以||操作員將無法工作。

但你可以有多個病例定義相同的結果,您可以通過ommitting的break;做。

switch(word) { 
    case "yes": 
    case "yeah": 
    goThroughWithIt(); 
    break; 

    case "no": 
    case "nuh uh": 
    abort(); 
    break; 
} 
1

沒有,但在C++,JAVA等,你可以做到以下幾點:

switch (a_variable) { 
    case CONST_1: 
    case CONST_2: 
    case CONST_3: 
     do_something(); 
     break; 
    case CONST_4: 
     do_some_stuff(); 
     break; 
    dafeult: 
     do_default_stuff(); 
     break; 
} 

這就像一個orswitch-case。這應該在JS工作太^^

0

我想你應該修改如下:

var user = prompt("Is programming awesome?").toUpperCase(); 
switch (user) { 
    case 'HELL YEAH': 
    case 'YES': 
    case 'YEAH': 
    case 'YEP': 
    case 'YEA': 
     console.log("That's what I'd expected!"); 
    break; 

    case 'KINDA': 
    case 'CANT SAY': 
     console.log("Oh, you'd like it when you'll be a pro!"); 
    break; 

    case 'HELL NO': 
    case'NO': 
    case'NAH': 
    case'NOPE': 
    case'NA': 
     console.log("You can't say that!"); 
    break; 

    default: 
     console.log("Wacha tryna say mate?"); 
    break; 
}