2014-01-15 26 views
0

我已經寫了一個函數來放置「,」和「和」之間的三個鏈接 我怎麼能減少if else語句。 在JavaScript中,我得到的計數,如果計數不爲零意味着鏈接必須以其他方式表現出來768,16被隱藏我怎麼能減少,如果其他語句

在以下情況下

function inst_grammer() 
{ 
var otherCount = parseInt($('.global_other_count').html()); 
var initCount = parseInt($('.global_init_count').html()); 
var signCount = parseInt($('.global_sign_count').html()); 

var init_class = $('.inst_init'); 
var sign_class = $('.inst_sign'); 

if (signCount != 0 && initCount != 0 && otherCount == 0) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html(' and ').fadeIn(); 
} else if (signCount == 0 && initCount != 0 && otherCount != 0) 
{ 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(''); 
} else if (signCount != 0 && initCount != 0 && otherCount != 0) 
{ 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(' , ').fadeIn(); 
} 
else if (signCount != 0 && initCount == 0 && otherCount == 0) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
} 
else if (signCount == 0 && initCount != 0 && otherCount == 0) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
} 
else if (signCount == 0 && initCount == 0 && otherCount != 0) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
} 
else if (signCount != 0 && initCount == 0 && otherCount != 0) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html(' and ').fadeIn(); 
} 
} 
+2

使用switch語句。 –

+0

@MahmoodRehman也許你應該提供一個如何打開3個變量組合的例子? – Ryan

+0

將它們放在一個數組中並使用一個循環...這只是一個簡單的問題,在所選項目之間添加「和」並淡出具有0值的元素。 –

回答

1

可以使錯誤與對應的當時塊功能碼, 然後計算指數爲數組是這樣的:

$index = 4*(signCount%2) + 2*(initCount%1) + (otherCount%2); 
$then[$index](); 
+0

你真的計算過這些產生的組合數嗎?他想簡化他的代碼。 –

+0

@TrueBlueAussie:當然。 8種組合。並且有幾個具有相同的行爲 –

+0

現在,如果您添加此解決方案所需的其餘代碼,我們可以看到它是否實際上代碼較少? :) –

0

更新:更方便,更簡單的解決方法就是拼接的3變種iables(1真,0爲假):

var mycode = "" + (signCount) ? "1":"0" + (initCount)?"1":"0" + (otherCount)?"1":"0"; // Concatenate as string 
switch(mycode) { 
case "111": 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(' , ').fadeIn(); 
    break; 
case "110": 
    init_class.html('').fadeOut(); 
    sign_class.html(' and ').fadeIn(); 
    break; 
case "101": 
    init_class.html('').fadeOut(); 
    sign_class.html(' and ').fadeIn(); 
    break; 
case "100": 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
    break; 
case "011": 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(''); 
    break; 
case "010": 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
    break; 
case "001": 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
    break; 
} 

原來的答覆:這是一個更容易貫徹,並注意可能出現的錯誤:

if (signCount) { 
    if(initCount) { 
     if(otherCount) { 
      init_class.html(' and ').fadeIn(); 
      sign_class.html(' , ').fadeIn(); 
     } 
     else { 
      init_class.html('').fadeOut(); 
      sign_class.html(' and ').fadeIn(); 
     } 
    } 
    else { 
     if(otherCount) { 
      init_class.html('').fadeOut(); 
      sign_class.html(' and ').fadeIn(); 
     } 
     else { 
      init_class.html('').fadeOut(); 
      sign_class.html('').fadeOut(); 
     } 
    } 
} 
else { 
    if (initCount) { 
     if(otherCount) { 
      init_class.html(' and ').fadeIn(); 
      sign_class.html(''); 
     } 
     else { 
      init_class.html('').fadeOut(); 
      sign_class.html('').fadeOut(); 
     } 
    } 
    else { 
     if(otherCount) { 
      init_class.html('').fadeOut(); 
      sign_class.html('').fadeOut(); 
     } 
    } 
} 

其他然後,我很害怕有沒有簡單的方法來簡化這個結。

+0

你的交換機假定他們只有0或1的計數(我收集它們的值爲0 *或更大*)。 –

+0

OP只有在值不同於0時纔有興趣,所以這看起來有效。將值轉換爲布爾值轉換爲答案。謝謝。 – user1853181

0

試試這個。我剛剛使用||因爲無論如何他們都在做同樣的工作。

具體這份工作

init_class.html('').fadeOut(); 
sign_class.html(' and ').fadeIn(); 

和這份工作

init_class.html('').fadeOut(); 
sign_class.html('').fadeOut(); 

在你的代碼被稱爲幾次。所以我只用||爲那些條件。

if ((signCount != 0 && initCount != 0 && otherCount == 0) || (signCount != 0 && initCount == 0 && otherCount != 0)) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html(' and ').fadeIn(); 
} 
else if (signCount == 0 && initCount != 0 && otherCount != 0) 
{ 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(''); 
} 
else if (signCount != 0 && initCount != 0 && otherCount != 0) 
{ 
    init_class.html(' and ').fadeIn(); 
    sign_class.html(' , ').fadeIn(); 
} 
else if ((signCount != 0 && initCount == 0 && otherCount == 0) || (signCount == 0 && initCount != 0 && otherCount == 0) || (signCount == 0 && initCount == 0 && otherCount != 0)) 
{ 
    init_class.html('').fadeOut(); 
    sign_class.html('').fadeOut(); 
} 
+1

這似乎幾乎不可能調試。 – user1853181

+0

是的,相同的結果scnerios應該在其他語句中。 – user3197473

0

這個選項怎麼樣?

$arr = array(
    array(1, 1, 1), array(1, 1, 0), array(1, 0, 0), array(0, 0, 0), 
    array(0, 0, 1), array(0, 1, 1), array(1, 0, 1), array(0, 1, 0) 
); 
$option = array_search(array($signCount?1:0, $initCount?1:0, $otherCount?1:0); 

switch($option, $arr)) { 
    case 0: 
     init_class.html(' and ').fadeIn(); 
     sign_class.html(' , ').fadeIn(); 
     break; 
    case 1: 
    case 6: 
     init_class.html('').fadeOut(); 
     sign_class.html(' and ').fadeIn(); 
     break; 
    case 2: 
    case 4: 
    case 7: 
     init_class.html('').fadeOut(); 
     sign_class.html('').fadeOut(); 
     break; 
    case 3: // none 
    break; 
    case 5: 
     init_class.html(' and ').fadeIn(); 
     sign_class.html(''); 
     break; 
} 
+0

很好地結合相同的結果。 (y) – user3197473

4

我想大家都是以錯誤的方式看待這個問題。它不是關於簡化if的,而是關於用語法順序插入「,」和「和」分隔符的算法。

此問題的任何解決方案應允許任何數量的項目(不只是指定3)。否則,如果規格更改,您可能會獲得大大增加的if測試數。當然更可重用(即如果業務需要改變)。

我收集的意圖,在本例中,是提供一種顯示,表示了這些選項:

  • 「A,B和C」
  • 「A和B」
  • 「一和c」
  • 「b和c」
  • 「一」
  • 「b」
  • 「C」

所以規則是:

  • 如果顯示項的數目是1,顯示沒有隔膜
  • 如果顯示項的數目是2,顯示「和」該項目之間
  • 如果項目數爲3,則使用「,」而不是「和」,除了最後一個。

所以基本上n> 1,最後一個分隔符是「和」,所有其他分隔符都是「,」。這個簡單的規則可以應用於任何數量的項目。

您可以通過簡單計數非零項目的數量來獲得此效果。 正如我在評論中提到的,把你的數據放在一個數組中,這樣你就可以簡單地迭代它。這意味着你的輸出字段也應該在數組中,所以你只能按順序顯示你想要的字段。

如果您想提供一個HTML示例,很高興提供代碼,但您應該能夠從這些簡化的規則中自己弄清楚這一點。 :)

+0

+1用於解決根本問題而不是效果問題。 – Tibos