Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Issue displaying calculation into a table

Hawkins_IN

New Coder
Hi,

I'm trying to get the following code to display the calculated output into a table however, I can't seem to get all the results to print correctly I'm only getting a few answers. I believe I've done the calculations part correctly so it could just be how I'm passing it to the table that's the issue. This is the code i have so far (I've highlighted in red where i think the issue must be):

<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- The title of the page and where the CSS file is actioned-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homepage</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Intro content of the page and navigation to other pages inside the header-->
<header class="top">
<a href=index.html><img src="images/banner.PNG" style="max-width: 100%; height: auto;" title="Homepage" alt="banner image"></a>
</header>
<!--Main contents of the page-->
<main>
<h2>Online Salary Calculator</h2>
<p>Our online salary calculator service can be used to give you a rough idea of how much you will be earning based on your income.</p>


<form>
<label>Job Title:</label>
<input type = "text" id="jobTitle" required>
<label>Income:</label>
<input type = "number" id="income" required>
<select id="rate">
<option value = "year">per year</option>
<option value = "month">per month</option>
<option value = "week">per week</option>
<option value = "hour">per hour</option>
</select>
<label>Hours Worked:</label>
<input type = "number" id="hoursWork" required>
<button id="calculateButton" type="button">Calculate</button>
</form>
<table>
<thead>
<tr>
<th>Job Title</th>
<th>Hourly Rate</th>
<th>Weekly Rate</th>
<th>Monthly Rate</th>
<th>Yearly Rate</th>
</tr>
</thead>
<tbody id="incomeResults"></tbody>
</table>
<script>
function calculate(){
const jobTitle = document.getElementById("jobTitle").value;
const income = document.getElementById("income").value;
const rate = document.getElementById("rate").value;
const hoursWork = document.getElementById("hoursWork").value;

let hourRate, weekRate, monthRate, yearRate;


if (rate=== "year"){
hourRate = income / 52 / hoursWork;
weekRate = income / 52;
monthRate = income / 12;
yearRate = income;
}else if (rate==="month"){
hourRate = income / 4 / hoursWork;
weekRate= income / 4;
monthRate = income;
yearRate = income * 12;
}else if (rate==="week"){
hourRate = income / hoursWork;
weekRate = income;
monthRate = income * 4;
yearRate = income * 52;
}else{
hourRate = income;
weekRate = income * hoursWork;
monthRate = income * hoursWork * 4;
yearRate = income * 52;
}

const incomeResults = document.getElementById("incomeResults");
const newRow = incomeResults.insertRow(-1);
const jobTitleCell = newRow.insertCell(0);
const hourRateCell = newRow.insertCell(1);
const weekRateCell = newRow.insertCell(2);
const monthRateCell = newRow.insertCell(3);
const yearRateCell = newRow.insertCell(4);

jobTitleCell.innerHTML = jobTitle;
hourRateCell.innerHTML = hourRate.toFixed(2);
weekRateCell.innerHTML = weekRate.toFixed(2);
monthRateCell.innerHTML = monthRate.toFixed(2);
yearRateCell.innerHTML = yearRate.toFixed(2);
}
document.getElementById("calculateButton").addEventListener("click",calculate);


</script>

</main>
</body>
</html>
 
Couple of suggestions :
  • Post your code in code tags using the </> button.
  • Do not highlight almost your entire page because you believe the problem might be there. Not helpful.
  • Check your HTML in the W3C Online validator (and fix any errors, obviously).
  • Check the debugger Console for script errors.
 
The first error I found:
} else {
hourRate = income;
weekRate = income * hoursWork;
monthRate = income * hoursWork * 4;
yearRate = income * 52; S/B yearRate = weekRate * 52;
}

Next, I removed the .toFixed(2); from the cell.innerHTML lines and everything worked.
It's the old string OR number dilemma solved by conversion.

jobTitleCell.innerHTML = jobTitle;
hourRateCell.innerHTML = Number(hourRate).toFixed(2);
weekRateCell.innerHTML = Number(weekRate).toFixed(2);
monthRateCell.innerHTML = Number(monthRate).toFixed(2);
yearRateCell.innerHTML = Number(yearRate).toFixed(2);

HERE'S THE JS:
Code:
<script>
function calculate() {
    const jobTitle = document.getElementById("jobTitle").value;
    const income = document.getElementById("income").value;
    const rate = document.getElementById("rate").value;
    const hoursWork = document.getElementById("hoursWork").value;
    let hourRate, weekRate, monthRate, yearRate;

    if (rate === "year") {
        hourRate = income / 52 / hoursWork;
        weekRate = income / 52;
        monthRate = income / 12;
        yearRate = income;
    } else if (rate === "month") {
        hourRate = income / 4 / hoursWork;
        weekRate = income / 4;
        monthRate = income;
        yearRate = income * 12;
    } else if (rate === "week") {
        hourRate = income / hoursWork;
        weekRate = income;
        monthRate = income * 4;
        yearRate = income * 52;
    } else {
        hourRate = income;
        weekRate = hourRate * hoursWork;
        monthRate = weekRate * 4;
        yearRate = weekRate * 52;
    }

    const incomeResults = document.getElementById("incomeResults");
    const newRow = incomeResults.insertRow(-1);
    const jobTitleCell = newRow.insertCell(0);
    const hourRateCell = newRow.insertCell(1);
    const weekRateCell = newRow.insertCell(2);
    const monthRateCell = newRow.insertCell(3);
    const yearRateCell = newRow.insertCell(4);

    jobTitleCell.innerHTML = jobTitle;
    hourRateCell.innerHTML = Number(hourRate).toFixed(2);
    weekRateCell.innerHTML = Number(weekRate).toFixed(2);
    monthRateCell.innerHTML = Number(monthRate).toFixed(2);
    yearRateCell.innerHTML = Number(yearRate).toFixed(2);
}
document.getElementById("calculateButton").addEventListener("click", calculate);
</script>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom