2013-02-06 39 views
1

我正在构建一个计算器应用程序,并卡在检查用户选择的值与服务器上的计划数据集的部分。Javascript:如何根据数据对象数组检查混合用户输入?

我使用的是这样的四个数据滑块:每当用户滑动滑块

enter image description here

,我值发回给我的脚本进行验证和检查。

现在这里是静态的数据集,我检查了用户的输入对值:

plandata.data = [ 

      {  

       id: 'small', 

       from: { 
        cpu: 1, 
        ram: 1, 
        hd: 40, 
        bw: 10 
       }, 

       to: { 
        cpu: 2, 
        ram: 2, 
        hd: 500, 
        bw: 500 
       }, 

       price: { 
        linux: 3490, 
        windows: 4190 
       } 

      }, 


      { 

       id: 'medium', 

       from: { 
        cpu: 2, 
        ram: 2, 
        hd: 40, 
        bw: 20 
       }, 

       to: { 
        cpu: 4, 
        ram: 4, 
        hd: 500, 
        bw: 500 
       }, 

       price: { 
        linux: 5600, 
        windows: 6300 
       } 

      }, 
    ...three more plans like this 

现在我想做的是:

  • 环比计划数据,并检查其计划用户选择了:是small,medium,large
  • 抛出一个错误并重置滑块,如果这些值超出有效范围,即它们应该在之间和to范围。
  • 如果计划是正确的,然后让该计划

我停留在第一步,现在的价格。这里是我的代码:

checkPlaninRange = function(cpuVal, ramVal, hdVal, bwVal) { 
        _.each(pdata, function(plan){ 

      if (cpuVal >= plan.from.cpu && cpuVal < plan.to.cpu 
        && ramVal >= plan.from.ram && ramVal < plan.to.ram 
        && hdVal >= plan.from.hd && hdVal < plan.to.hd 
        && bwVal >= plan.from.bw && bwVal < plan.to.bw 
        ) 
      { 
       console.log(plan.id, 'Plan Found'); 
      } else { 
       console.log('plan not found'); 
      }; 

     }); 

}; 

的问题是:我得到不同的结果

plan not found plan.js:41 
medium Plan Found plan.js:39 

plan not found plan.js:41 
medium Plan Found plan.js:39 

plan not found plan.js:41 
medium Plan Found plan.js:39 

plan not found plan.js:41 
medium Plan Found plan.js:39 

plan not found 

我在做什么错?我似乎无法环绕计划数据并获得正确的计划。请帮忙。

+0

既可以通过调试器或通过CONSOLE.LOG你应该在你checkPlaninRange功能验证的参数值。如果你仍然不明白为什么逻辑失败,请在这里发布。我怀疑你那里的大逻辑检查是不明显的。这可能有助于将其分解为4个if语句(不是长期的,但仅用于调试目的)。 –

+0

@EricLaForce,传递给函数的参数与您在界面中选择的值相同。至于调试器语句,请参阅我的问题中的最后五行代码 - 即chrome控制台输出。 –

回答

1
if (cpuVal >= plan.from.cpu && cpuVal <= plan.to.cpu 
       && ramVal >= plan.from.ram && ramVal <= plan.to.ram 
       && hdVal >= plan.from.hd && hdVal <= plan.to.hd 
       && bwVal >= plan.from.bw && bwVal <= plan.to.bw 
       ) 

我想你想AND和OR不

+0

我认为你是对的,但现在我得到了有效值和无效值的混合。我下一步应该做什么来捕捉正确的计划。有效的用户输入只能在这些范围之一之间。 –

+1

查看你的数据类型将其限制为值或值,即使它 Techmonk

+0

我是否也应该为'> ='做同样的事情? –

相关问题