2011-09-14 75 views
1

我需要根據價格矩陣計算窗簾價格,這取決於寬度和高度。如何從數組中計算而不是if語句?Javascript價格矩陣數組

function checkPrice() { 

     var price = calculatePrice();  

     function calculatePrice(price) { 

    var width = parseInt(document.getElementById('customid0').value); 
    var height = parseInt(document.getElementById('customid1').value); 

      // width up to 100 
      if (width <= 100){ 

      if (height <=50){       
      price = 67.22;       
      } 

      if (height >50 && height <=100){        
      price = 103.34;        
      } 

      if (height >100 && height <=130){       
      price = 133.11;        
      } 
      } 

      // width between 101 and 125 
      if (width > 100 && width <= 125){ 

      if (height <=50){       
      price = 76.69;       
      } 

      if (height >50 && height <=100){        
      price = 113.01;        
      } 

      if (height >100 && height <=130){       
      price = 146.05;      
      } 
      } 

      // width between 126 and 150 
      if (width > 125 && width <= 150){ 

      if (height <=50){       
      price = 83.69;       
      } 

      if (height >50 && height <=100){        
      price = 124.74;        
      } 

      if (height >100 && height <=130){       
      price = 161.28;      
      } 
      } 

       return price; 
      } 

document.getElementById('product_addtocart_form').action = '<?php echo $this->getAddToCartUrl($_product) ?>&price='+price; 
    optionsPrice.changePrice('options', price); 
    optionsPrice.reload(); 
    Price.changePrice(price); 
} 
+0

不要使用小數錢。在數學完成後,將它乘以100併除以100。嘗試添加0.02和0.01,看看爲什麼。 – Incognito

+0

謝謝,會做。 – Gumtee

+0

這是功課嗎? – Incognito

回答

1
var width = parseInt(document.getElementById('customid0').value, 10); 
var height = parseInt(document.getElementById('customid1').value, 10); 

var conditions = [ 
     [0, 100, [  // width = 0..100 
      [0, 50, [  //  height = 0..50 
       67.22  //   price = 67.02 
      ]], 
      [50, 100, [ //  height = 50..100 
       103.34 //   price = 103.34 
      ]], 
      [100, 130, [ //  height = 100..130 
       133.11 //   price = 133.11 
      ]] 
     ]], 
     [101, 125, [  // width = 101..125 
      [0, 50, [  //  height = 0..50 
       76.69  //   ... 
      ]], 
      [51, 100, [ 
       113.01 
      ]], 
      [101, 130, [ 
       146.05 
      ]] 
     ]], 
     [126, 150, [ 
      [0, 50, [ 
       83.69 
      ]], 
      [51, 100, [ 
       124.74 
      ]], 
      [101, 130, [ 
       161.28 
      ]] 
     ]] 
    ]; 

for (var w = 0, w_len = conditions.length; w < w_len; w += 1) 
{ 
    if (width >= conditions[w][0] && width <= conditions[w][1]) 
    { 
     for (var h = 0, h_len = conditions[w][2].length; h < h_len; h += 1) 
     { 
      if (height >= conditions[w][2][h][0] && height <= conditions[w][2][h][1]) 
      { 
       price = conditions[w][2][h][2]; 
       break; 
      } 
     } 
    } 
} 

檢查Fiddle here

+0

使每個組都成爲擁有'min','max'和'data'的對象可能會使生活更輕鬆。 – Incognito

+0

@Incognito:的確,我認爲存儲和讀取數據的方法比使用多維數組有更好的方法。但這是問題的答案。你認爲我的方法可以優化嗎? (總是在談論數組) –

+0

是的,最重要的是你的'parseInt'不是將基數設置爲十進制系統,你很容易將它解釋爲一個八進制數。我沒有真正看過循環的優化,你可能有辦法做出這個單循環。 – Incognito