私は電卓アプリを構築していますが、サーバー上にある計画のデータセットに対してユーザーが選択した値をチェックする部分で立ち往生しています。
次のような 4 つのデータ スライダーを使用しています。
ユーザーがスライダーをスライドするたびに、検証とチェックのために値をスクリプトに送り返します。
ここで、ユーザーの入力をチェックしている値の静的データセットを次に示します。
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
今、私がやりたいことは次のとおりです。
- 計画データをループして、ユーザーが選択した計画を確認します: Is it
small
、medium
などlarge
- 値が有効な範囲外の場合、つまり と の
from
範囲内にある必要がある場合は、エラーをスローしてスライダーをリセットし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
私は何を間違っていますか?計画データをループして正しい計画を取得することに頭を悩ませているようには見えません。助けてください。