有没有更好的方法来使用我的 'if' 函数中的分配变量?
Is there a better way to use assigned variables from my 'if' function?
我是 javascript 的新手,我一直在使用这个 'if' 函数时遇到问题。它应该根据提交的表单数据为变量赋值。这是我的代码:
var carSurv = {};
global.survValue = function(){
if ((req.body.price == 10000)){
carSurv.price= 1;
} else if((req.body.price == 20000)){
carSurv.price= 2;
} else if((req.body.price == 30000)){
carSurv.price= 3;
} else if((req.body.price == 40000)){
carSurv.price= 4;
} else if((req.body.price == 50000)){
carSurv.price= 5;
} else if((req.body.price === 'above 50000')){
carSurv.price= 6;
};
if ((req.body.seat == 2) ){
carSurv.seat= 2;
} else if((req.body.seat == 4)){
carSurv.seat= 4;
} else if((req.body.seat == 5)){
carSurv.seat= 5;
} else if((req.body.seat == 7)){
carSurv.seat= 7;
} else if((req.body.seat == 8)){
carSurv.seat= 8;
};
if ((req.body.carType === 'Compact' )){
carSurv.body= 'compact';
} else if((req.body.carType === 'Sedan')){
carSurv.body= 'sedan';
} else if((req.body.carType === 'SUV')){
carSurv.body= 'suv';
} else if((req.body.carType === 'Truck')){
carSurv.body= 'truck';
} else if((req.body.carType === 'Sports')){
carSurv.body= 'sports';
};
if ((req.body.personality === 'Chill')){
carSurv.personality= 'chill';
} else if((req.body.personality === 'Compassionate')){
carSurv.personality= 'compassionate';
} else if((req.body.personality === 'Assertive')){
carSurv.personality= 'assertive';
} else if((req.body.personality === 'Short-tempered')){
carSurv.personality= 'angry';
};
if ((req.body.activity === 'Family roadtrips and outgoing' )){
carSurv.activity= 'roadtrip';
} else if((req.body.activity === 'Going to music festivals')){
carSurv.activity= 'music';
} else if((req.body.activity === 'Commuting to work')){
carSurv.activity= 'commute';
} else if((req.body.activity === 'Date nights for two')){
carSurv.activity= 'date';
};
if ((req.body.driving === 'Being Safe' )){
carSurv.driving= 'safe';
} else if((req.body.driving === 'Getting from point A to point B')){
carSurv.driving= 'point';
} else if((req.body.driving === 'Going fast')){
carSurv.driving= 'fast';
} else if((req.body.driving === 'Looking good while doing it')){
carSurv.driving= 'looks';
} else if((req.body.driving === 'Having the ability to drive with groups')){
carSurv.driving= 'passengers';
};
if ((req.body.attributes === 'Longevity: Car durability, Gas mileage/Fuel efficiency' )){
carSurv.attrib= 'stam';
} else if((req.body.attributes === 'Versatility: Off-road and work use')){
carSurv.attrib= 'vers';
} else if((req.body.attributes === 'Exterior Design: Look and style purely to impress')){
carSurv.attrib= 'style';
} else if((req.body.attributes === 'Internal Design: Spacious and compact')){
carSurv.attrib= 'comfort';
} else if((req.body.attributes === 'Brand name manufacturing: American or foreign made')){
carSurv.attrib= 'mfg';
}
};
console.log(carSurv.attrib);
删节了一点,但这是我主要关心的地方。我用于测试的 console.log 不断返回 'undefined'。我做错了什么吗?
global.survValue = function(){
if ((req.body.price > 50000)){
carSurv.price= req.body.price/10000;
} else if((req.body.price < 50000)){
carSurv.price= 6;
};
carSurv.seat= req.body.seat;
carSurv.body= req.body.carType;
carSurv.personality= req.body.personality;
if((req.body.personality === 'Short-tempered')){
carSurv.personality= 'angry';
};
if ((req.body.activity === 'Family roadtrips and outgoing' )){
carSurv.activity= 'roadtrip';
} else if((req.body.activity === 'Going to music festivals')){
carSurv.activity= 'music';
} else if((req.body.activity === 'Commuting to work')){
carSurv.activity= 'commute';
} else if((req.body.activity === 'Date nights for two')){
carSurv.activity= 'date';
};
if ((req.body.driving === 'Being Safe' )){
carSurv.driving= 'safe';
} else if((req.body.driving === 'Getting from point A to point B')){
carSurv.driving= 'point';
} else if((req.body.driving === 'Going fast')){
carSurv.driving= 'fast';
} else if((req.body.driving === 'Looking good while doing it')){
carSurv.driving= 'looks';
} else if((req.body.driving === 'Having the ability to drive with groups')){
carSurv.driving= 'passengers';
};
if ((req.body.attributes === 'Longevity: Car durability, Gas mileage/Fuel efficiency' )){
carSurv.attrib= 'stam';
} else if((req.body.attributes === 'Versatility: Off-road and work use')){
carSurv.attrib= 'vers';
} else if((req.body.attributes === 'Exterior Design: Look and style purely to impress')){
carSurv.attrib= 'style';
} else if((req.body.attributes === 'Internal Design: Spacious and compact')){
carSurv.attrib= 'comfort';
} else if((req.body.attributes === 'Brand name manufacturing: American or foreign made')){
carSurv.attrib= 'mfg';
}
};
console.log(carSurv.attrib);
您可以使用类似这样的登录方式。 if条件不用放太多
我是 javascript 的新手,我一直在使用这个 'if' 函数时遇到问题。它应该根据提交的表单数据为变量赋值。这是我的代码:
var carSurv = {};
global.survValue = function(){
if ((req.body.price == 10000)){
carSurv.price= 1;
} else if((req.body.price == 20000)){
carSurv.price= 2;
} else if((req.body.price == 30000)){
carSurv.price= 3;
} else if((req.body.price == 40000)){
carSurv.price= 4;
} else if((req.body.price == 50000)){
carSurv.price= 5;
} else if((req.body.price === 'above 50000')){
carSurv.price= 6;
};
if ((req.body.seat == 2) ){
carSurv.seat= 2;
} else if((req.body.seat == 4)){
carSurv.seat= 4;
} else if((req.body.seat == 5)){
carSurv.seat= 5;
} else if((req.body.seat == 7)){
carSurv.seat= 7;
} else if((req.body.seat == 8)){
carSurv.seat= 8;
};
if ((req.body.carType === 'Compact' )){
carSurv.body= 'compact';
} else if((req.body.carType === 'Sedan')){
carSurv.body= 'sedan';
} else if((req.body.carType === 'SUV')){
carSurv.body= 'suv';
} else if((req.body.carType === 'Truck')){
carSurv.body= 'truck';
} else if((req.body.carType === 'Sports')){
carSurv.body= 'sports';
};
if ((req.body.personality === 'Chill')){
carSurv.personality= 'chill';
} else if((req.body.personality === 'Compassionate')){
carSurv.personality= 'compassionate';
} else if((req.body.personality === 'Assertive')){
carSurv.personality= 'assertive';
} else if((req.body.personality === 'Short-tempered')){
carSurv.personality= 'angry';
};
if ((req.body.activity === 'Family roadtrips and outgoing' )){
carSurv.activity= 'roadtrip';
} else if((req.body.activity === 'Going to music festivals')){
carSurv.activity= 'music';
} else if((req.body.activity === 'Commuting to work')){
carSurv.activity= 'commute';
} else if((req.body.activity === 'Date nights for two')){
carSurv.activity= 'date';
};
if ((req.body.driving === 'Being Safe' )){
carSurv.driving= 'safe';
} else if((req.body.driving === 'Getting from point A to point B')){
carSurv.driving= 'point';
} else if((req.body.driving === 'Going fast')){
carSurv.driving= 'fast';
} else if((req.body.driving === 'Looking good while doing it')){
carSurv.driving= 'looks';
} else if((req.body.driving === 'Having the ability to drive with groups')){
carSurv.driving= 'passengers';
};
if ((req.body.attributes === 'Longevity: Car durability, Gas mileage/Fuel efficiency' )){
carSurv.attrib= 'stam';
} else if((req.body.attributes === 'Versatility: Off-road and work use')){
carSurv.attrib= 'vers';
} else if((req.body.attributes === 'Exterior Design: Look and style purely to impress')){
carSurv.attrib= 'style';
} else if((req.body.attributes === 'Internal Design: Spacious and compact')){
carSurv.attrib= 'comfort';
} else if((req.body.attributes === 'Brand name manufacturing: American or foreign made')){
carSurv.attrib= 'mfg';
}
};
console.log(carSurv.attrib);
删节了一点,但这是我主要关心的地方。我用于测试的 console.log 不断返回 'undefined'。我做错了什么吗?
global.survValue = function(){
if ((req.body.price > 50000)){
carSurv.price= req.body.price/10000;
} else if((req.body.price < 50000)){
carSurv.price= 6;
};
carSurv.seat= req.body.seat;
carSurv.body= req.body.carType;
carSurv.personality= req.body.personality;
if((req.body.personality === 'Short-tempered')){
carSurv.personality= 'angry';
};
if ((req.body.activity === 'Family roadtrips and outgoing' )){
carSurv.activity= 'roadtrip';
} else if((req.body.activity === 'Going to music festivals')){
carSurv.activity= 'music';
} else if((req.body.activity === 'Commuting to work')){
carSurv.activity= 'commute';
} else if((req.body.activity === 'Date nights for two')){
carSurv.activity= 'date';
};
if ((req.body.driving === 'Being Safe' )){
carSurv.driving= 'safe';
} else if((req.body.driving === 'Getting from point A to point B')){
carSurv.driving= 'point';
} else if((req.body.driving === 'Going fast')){
carSurv.driving= 'fast';
} else if((req.body.driving === 'Looking good while doing it')){
carSurv.driving= 'looks';
} else if((req.body.driving === 'Having the ability to drive with groups')){
carSurv.driving= 'passengers';
};
if ((req.body.attributes === 'Longevity: Car durability, Gas mileage/Fuel efficiency' )){
carSurv.attrib= 'stam';
} else if((req.body.attributes === 'Versatility: Off-road and work use')){
carSurv.attrib= 'vers';
} else if((req.body.attributes === 'Exterior Design: Look and style purely to impress')){
carSurv.attrib= 'style';
} else if((req.body.attributes === 'Internal Design: Spacious and compact')){
carSurv.attrib= 'comfort';
} else if((req.body.attributes === 'Brand name manufacturing: American or foreign made')){
carSurv.attrib= 'mfg';
}
};
console.log(carSurv.attrib);
您可以使用类似这样的登录方式。 if条件不用放太多