0

I need to output sales tax and total using a subtotal and its tax rate

I am able to output sales tax but im not sure how to get it to output tax rate as well

here is what I have:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sales Tax Calculator</title>
    <link rel="stylesheet" href="sales_tax.css" />
</head>
<body>
    <main>
        <h1>Sales Tax Calculator</h1>
        <p>Enter Subtotal and Tax Rate and click "Calculate".</p> 
        <div>
            <label for="subtotal">Subtotal:</label>
            <input type="text" id="subtotal" >
        </div>
        <div>
            <label for="tax_rate">Tax Rate:</label>
            <input type="text" id="tax_rate" >
        </div>
        <div>
            <label for="sales_tax">Sales Tax:</label>
            <input type="text" id="sales_tax" disabled >
        </div>
        <div>
            <label for="total">Total:</label>
            <input type="text" id="total" disabled >
        </div>
        <div>
            <label></label>
            <input type="button" id="calculate" value="Calculate" >
            <input type="button" id="clear" value="Clear" >
        </div>
    </main>
    <script src="sales_tax.js"></script>
</body>
</html>

...

"use strict";

const $ = selector => document.querySelector(selector);

const getErrorMsg = val => `${val} must be a valid number greater than zero.`;

const focusAndSelect = selector => {
    const elem = $(selector);
    elem.focus();
    elem.select();
};

const processEntries = () => {
    const subtotal = parseFloat($("#subtotal").value);
    const taxrate = parseFloat($("#tax_rate").value);

    if (isNaN(subtotal) || subtotal <= 0) {
        alert(getErrorMsg("Subtotal"));
        focusAndSelect("#subtotal");
    } else if (isNaN(taxrate) || taxrate <= 0) {
        alert(getErrorMsg("tax rate"));
        focusAndSelect("#tax_rate");
    } 
    else {
       $("#sales_tax").value = (subtotal * taxrate) / 100;
         

         $("#total").value = (subtotal + salestax);
    }
   
}

document.addEventListener("DOMContentLoaded", () => {
    $("#calculate").addEventListener("click", processEntries);
    $("#subtotal").focus() ;
   
});

code is not fully completed but all i can get it to output is the sales tax and im not sure how to get it to output the total

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Can you please share your HMTL? – Ethan Nov 16 '22 at 00:32
  • 1
    `salestax` isn’t defined anywhere. Did you mean `$("#sales_tax").valueAsNumber` (or `Number($("#sales_tax").value)` if you’re, for whatever reason, not using ``)? – Sebastian Simon Nov 16 '22 at 00:34
  • 1
    I am sure there is a nice error in your developer console. Use your developer tools! – epascarello Nov 16 '22 at 00:40
  • sales tax is defined in html. Thats what I am struggling with, im not sure if it needs to be defined and where i would put that in my java script. Im sure thats why its not calculating because it isnt defined anywhere stating it equals subtotal * taxrate /100 – Ava Ritter Nov 16 '22 at 00:51
  • @AvaRitter No, this is about JavaScript. The JavaScript variable `salestax` isn’t defined anywhere. Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). Use linters like [JSHint](//jshint.com) to find problems with your code immediately. – Sebastian Simon Nov 16 '22 at 02:14

0 Answers0