2015-10-14 55 views
0

我試圖簡化這個功能,因爲可以有多個type數據對象,並且每種類型都有一個男性版本和一個女性版本。 對象中元素的編號和名稱始終相同。通過重複if/else部分來簡化函數

正如你看到的,大部分代碼是重複......

function calculate(type, j, value, s) { 
    for (var i = j; i > 4; i--) { 
     if (type == 'weight') { 
      if (s == 'f') { 
       if (weightFemale.hasOwnProperty(i)) { 
        var m = weightFemale[i][0], 
         l = weightFemale[i][1], 
         s = weightFemale[i][2];  
        return getcalc(m,l,s); 
       } 
      } 
      else { 
       if (weightMale.hasOwnProperty(i)) { 
        var m = weightMale[i][0], 
         l = weightMale[i][1], 
         s = weightMale[i][2];  
        return getcalc(m,l,s); 
       }    
      } 
     } 
     else if (type == 'length') { 
      if (s == 'f') { 
       if (lengthFemale.hasOwnProperty(i)) { 
        var m = lengthFemale[i][0], 
         l = lengthFemale[i][1], 
         s = lengthFemale[i][2], 
        return getcalc(m,l,s); 
       } 
      } 
      else { 
       if (lengthMale.hasOwnProperty(i)) { 
        var m = lengthMale[i][0], 
         l = lengthMale[i][1], 
         s = lengthMale[i][2], 
        return getcalc(m,l,s); 
       }    
      } 
     } 
    } 
    return false; 
} 

我怎麼能簡化的if/else零件的類型和性?

+1

爲你的代碼似乎是你可能會考慮在codereview.stackexchange.com上詢問這個問題。至於這個問題,爲重複部分製作一個函數可能會有所幫助。 –

+0

我投票結束這個問題作爲題外話,因爲它應該在像codereview.stackexchange – rlemon

+1

這樣的網站首先這個代碼工作正常嗎?因爲's ='f''需要's''f''。 –

回答

1

既然你正在做的每一個對象同樣的事情,只是讓你的條件語句定義一個對象的引用,只調用計算一次。

喜歡的東西:

var obj; 

if (type == 'weight') { 
    obj = s == 'f' ? weightFemale : weightMale; 
} else if (type == 'length') { 
    obj = s == 'f' ? lengthFemale : lengthMale; 
} 

if (obj.hasOwnProperty(i)) { 
    var m = obj[i][0], 
     l = obj[i][1], 
     s = obj[i][2]; 
    return getcalc(m, l, s); 
} 
1

我想創建一個自己的功能,並創建一個開關

switch(type) { 
    case "weight": 
     getValues(); 
     break; 
    case "length": 
     getValues(); 
     break; 
} 
0

問問自己,什麼是非常相似的部分?它看起來如果.hasOwnProperty()是真的,你從數組中獲得m,l和s,然後調用getcalc()。首先將該部分提取到一個函數中,並將不同的部分作爲參數傳入。

另一種模式是,您正在使用基於特定條件的特定數組。獲取你想要的數組可以放入一個函數中。

此外,與問題不太相關,但您可能希望給變量提供更好的名稱。這使得代碼更具可讀性並且更易於推理。

這就是我想出了:

function getArray(type, s){ 
    if(type == 'weight') { 
     return s == 'f' ? weightFemale : weightMale; 
    } 
    else if(type == 'length') { 
     return s == 'm' ? lengthFemale : lengthMale;  
    } 
} 


function makeCalculation(array, i) { 
    if(array.hasOwnProperty(i)) { 
     var m = lengthMale[i][0], 
      l = lengthMale[i][1], 
      s = lengthMale[i][2], 
     return getcalc(m,l,s); 
    } 
} 

function calculate(type, j, value, s) { 
    for (var i = j; i > 4; i--) { 
     var array = getArray(type, s); 
     return makeCalculation(array, i); 
    } 
} 
0

首先你可以這樣創建功能:

function _getcalc(arr, i) { 
    var m = arr[i][0], 
    l = arr[i][1], 
    s = arr[i][2]; 
    return getcalc(m, l, s); 
} 

然後你就可以縮短你的函數是這樣的:

function calculate(type, j, value, s) { 
    for (var i = j; i > 4; i--) { 
     switch(type) { 
      case 'weight': 
       if (s == 'f' && weightFemale.hasOwnProperty(i)) { 
        return _getcalc(weightFemale, i); 
       } else if(weightMale.hasOwnProperty(i)) { 
        return _getcalc(weightMale, i); 
       } 
       break; 
      case 'length': 
       if (s == 'f' && lengthFemale.hasOwnProperty(i)) { 
        return _getcalc(lengthFemale, i); 
       } else if(lengthMale.hasOwnProperty(i)) { 
        return _getcalc(lengthMale, i); 
       } 
       break; 
     } 
    } 
    return false; 
} 
+1

需要'_getcalc()'返回'' – charlietfl

+1

當然,我已經解決了這個問題。謝謝。 –