1

I am building a calculator app and am stuck at the part where I check the values chosen by the user against the data set of plans I have on the server.

I am using four data sliders that look like this:

enter image description here

Every time the user slides the slider, I send back values to my script for validation and checking.

Now here is the static data set of values I am checking the user's input against:

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

Now what I want to do is:

  • loop over the plan data and check which plan the user has chosen: Is it small, medium, large etc
  • throw an error and reset the slider if the values are out of valid range i.e. they should be between the from and to range.
  • if plan is correct, then get the price of that plan

I am stuck on the first step right now. Here is my code:

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');
            };

        });

}; 

The problem is: I am getting mixed results

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 

What am I doing wrong? I can't seem to wrap my head around looping through the plan data and getting the right plan. Please help.

4

1 回答 1

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

于 2013-02-06T12:52:54.853 回答