I have two JS functions that return a total number for me based on some properties. It works fine but feels a bit 'clunky'. Would there be a cleaner way of doing this?
getTotalTrucks: function (data) {
var noOfTrucks = 0;
if (data.truck1 == "1") {
noOfTrucks += 1;
}
if (data.truck2 == "1") {
noOfTrucks += 1;
}
if (data.truck3 == "1") {
noOfTrucks += 1;
}
if (data.truck4 == "1") {
noOfTrucks += 1;
}
if (data.truck5 == "1") {
noOfTrucks += 1;
}
return noOfTrucks ;
},
getTotalCars: function (data) {
var noOfCars = 0;
if (data.car1 == "2") {
noOfCars += 1;
}
if (data.car2 == "2") {
noOfCars += 1;
}
if (data.car3 == "2") {
noOfCars += 1;
}
if (data.car4 == "2") {
noOfCars += 1;
}
if (data.car5 == "2") {
noOfCars += 1;
}
return noOfCars ;
}