Reputation: 1
I have a basic calculator to calculate the cost of work, but I need to factor in different prices with discounts. I need to calculate cost per hour/day/month.
I have three categories of workers (light/med/max equip) and three categories of schedule (8/12/24 hour). Depending on schedule, the price varies. For light 75$/24h, 100$/12h, 110$/8h. For medium 80$/24h, 130$/12h, 140$/8h. and for max is 130$/24h, 150$/12h, 180$/8h.
I have no problem to calculate cost per day and month, but no idea how to use different prices for different schedules.
<script type="text/javascript">
function calculate() {
var number = document.getElementsByName("number")[0].value;
var equip = document.getElementsByName("equip")[0].value;
var schedule = document.getElementsByName("schedule")[0].value;
var day = (number * equip * schedule);
var hour = (number * equip);
var month = (30.5 * equip * number * schedule);
power = Math.round( day * 100 ) / 100;
if(check(["number"])){
document.getElementsByName("perday")[0].value = day + " $";
document.getElementsByName("permonth")[0].value = month + " $";
document.getElementsByName("perhour")[0].value = hour + " $";
}
}
function check(elems){
var f = true;
for(var i = 0; i < elems.length; i++){
if(document.getElementsByName(elems[i])[0].value == ""){
alert("enter all fields!");
f = false;
break;
}
}
return f;
}
</script>
<table border="0" cellpadding="1" cellspacing="0" style="width: 500px;">
<tbody>
<tr>
<td>
number of employees
</td>
<td>
<select name="number"><option value="1" selected>1</option>
<option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>
<option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select>
</td>
</tr>
<tr>
<td>
Equipment
</td>
<td>
<select name="equip"><option value="75" selected>light</option><option value="80">medium</option><option value="130">maximum</option></select>
</td>
</tr>
<tr>
<td>
Schedule
</td>
<td>
<select name="schedule"><option value="8" selected>8-hour</option><option value="12">12-hour</option><option value="24">24-hour</option></select>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<input type="button" onclick="calculate();" value="calculate price" />
</td>
</tr>
</tbody>
</table>
<div>cost per hour: <input name="perhour"/></div>
<div>cost per day: <input name="perday"/></div>
<div>cost per month: <input name="permonth"/></div>
https://jsfiddle.net/DOOOS/fdLxu4sv/17/
Upvotes: 0
Views: 69
Reputation: 754
So instead of using the price as the value on the equipment drop down you would want to use the equipment type. Then you could create a price object and use a function with a switch to get the price per equipment and schedule.
I'd suggest reading up a little more on objects, arrays, and switches. So that you understand why this works.
function getPrice(equip, schedule) {
var price = {
light: [75, 100, 110],
medium: [80, 130, 140],
maxium: [130, 150, 180]
};
switch (schedule) {
case '8':
return price[equip][2];
case '12':
return price[equip][1];
case '24':
return price[equip][0];
}
}
function calculate() {
var number = document.getElementsByName("number")[0].value;
var equip = document.getElementsByName("equip")[0].value;
var schedule = document.getElementsByName("schedule")[0].value;
var day = (number * getPrice(equip, schedule) * schedule);
var hour = (number * getPrice(equip, schedule));
var month = (30.5 * getPrice(equip, schedule) * number * schedule);
power = Math.round(day * 100) / 100;
if (check(["number"])) {
document.getElementsByName("perday")[0].value = day + " $";
document.getElementsByName("permonth")[0].value = month + " $";
document.getElementsByName("perhour")[0].value = hour + " $";
}
}
function check(elems) {
var f = true;
for (var i = 0; i < elems.length; i++) {
if (document.getElementsByName(elems[i])[0].value == "") {
alert("enter all fields!");
f = false;
break;
}
}
return f;
}
<table border="0" cellpadding="1" cellspacing="0" style="width: 500px;">
<tbody>
<tr>
<td>
number of employees
</td>
<td>
<select name="number">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
</tr>
<tr>
<td>
Equipment
</td>
<td>
<select name="equip">
<option value="light" selected>light</option>
<option value="medium">medium</option>
<option value="maximum">maximum</option>
</select>
</td>
</tr>
<tr>
<td>
Schedule
</td>
<td>
<select name="schedule">
<option value="8" selected>8-hour</option>
<option value="12">12-hour</option>
<option value="24">24-hour</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<input type="button" onclick="calculate();" value="calculate price" />
</td>
</tr>
</tbody>
</table>
<div>cost per hour: <input name="perhour" /></div>
<div>cost per day: <input name="perday" /></div>
<div>cost pre month: <input name="permonth" /></div>
Upvotes: 1