I am running the following script and getting a different result than expected. My desktop calculator calculates $28.84, but the code shows $29.07. I’m not sure if there’s something wrong with my code.
// Compute Semi-monthly
function calcSalaryS() {
var sppp3_element = document.getElementById('txt_sppp3');
var sppp3 = parseInt(sppp3_element.value);
var hours3_element = document.getElementById('txt_hours3');
var hours3 = parseInt(hours3_element.value);
var wwpy3_element = document.getElementById('txt_wwpy3');
var wwpy3 = parseInt(wwpy3_element.value);
var calculateS = sppp3 / wwpy3;
var calculateSS = calculateS / hours3;
var calculateS1 = sppp3 / 260;
if (hours3 == 86.67) {
document.getElementById('resultsS').innerHTML = 'Daily Rate $' + calculateSS.toLocaleString() + '<br>' + 'Hourly Rate $' + calculateS1.toLocaleString();
event.preventDefault();
console.log;
} else {
document.getElementById('resultsS').innerHTML = 'Daily Rate $' + calculateSS.toLocaleString() + '<br>' + 'Hourly Rate $' + calculateS1.toLocaleString();
event.preventDefault();
console.log;
}
}
<p>Yearly Salary:<input type="text" name="sppp3" id="txt_sppp3" value="60000" size="12" title="Whole numbers only,e.g. '20000'" /></p>
<p>Hours Per Week:<input type="text" name="hours3" id="txt_hours3" value="86.67" size="10" disabled /></p>
<p>Work Week per Year:<input type="text" name="wwpy3" id="txt_wwpy3" value="24" size="7" disabled /></p>
<button value="calculate" onclick="calcSalaryS()" class="block">Compute Daily and Hourly Rate</button></p>
<p id="resultsS"></p>
>Solution :
what you expect as result with the line
var hours3 = parseInt(hours3_element.value);
and as value "86.87" ?
The result is: hours3 = 86 !
You should use parseFloat() !