Question

In: Computer Science

JS Bin / Tax Calculator / using HTML | CSS | JavaScript ------------------------------------------------------------------------------------------- How can I...

JS Bin / Tax Calculator / using HTML | CSS | JavaScript

-------------------------------------------------------------------------------------------

How can I edit the JavaScript below so that when the calculate button is clicked the total price only shows two decimal places by using the toFixed() method?

-----------------------------------------------------------------------------------------------------------

JavaScript:

// Variables
var tax = "tax"; // Tax percentage
var taxRate = document.getElementById('tax'); // Selecting tax element

// On the time of loading the DOM we are setting the tax rate
window.addEventListener('DOMContentLoaded', (event) => {
taxRate.value = tax;
});


// Calculate button
var btn = document.getElementById('calculate');

btn.addEventListener('click', (event) => {
// Stopping form to submit data
event.preventDefault();
  
// Selecting dom elements
var quantity = document.getElementById('quantity').value;
var price = document.getElementById('price').value;
var discount = document.getElementById('discount').value;
var total = document.getElementById('total');
  
// Calculating the total price
var totalPrice = quantity * price;
var totalTax = (totalPrice * taxRate.value) / 100;
var totalDiscount = (totalPrice * discount) / 100;
var finalPrice = totalPrice + totalTax - totalDiscount;

// Setting the total amount
total.value = finalPrice;

});

Solutions

Expert Solution

you have to add this line before the line

"total.value=finalPrice;

Syntax

number.toFixed(number of decimal place)

NOW ADD THIS LINE

finalprice.toFixed(2);

Here is the updated code

// Variables
var tax = "tax"; // Tax percentage
var taxRate = document.getElementById('tax'); // Selecting tax element

// On the time of loading the DOM we are setting the tax rate
window.addEventListener('DOMContentLoaded', (event) => {
taxRate.value = tax;
});


// Calculate button
var btn = document.getElementById('calculate');

btn.addEventListener('click', (event) => {
// Stopping form to submit data
event.preventDefault();
  
// Selecting dom elements
var quantity = document.getElementById('quantity').value;
var price = document.getElementById('price').value;
var discount = document.getElementById('discount').value;
var total = document.getElementById('total');
  
// Calculating the total price
var totalPrice = quantity * price;
var totalTax = (totalPrice * taxRate.value) / 100;
var totalDiscount = (totalPrice * discount) / 100;
var finalPrice = totalPrice + totalTax - totalDiscount;

//fixed number of decimal point
finalPrice.toFixed(2);
// Setting the total amount
total.value = finalPrice;

});


Related Solutions

Build an application using HTML, CSS, javascript, json. It is not necessary for backend to be...
Build an application using HTML, CSS, javascript, json. It is not necessary for backend to be working but the frontend should be good. App should to simple and useful in our day to day life. For eg- grocery shopping list or alarm or weather forecast or reminder.
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML...
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML tags to design your webpage: <h1>...</h1>,<h3>...</h3>, <h6>...</h6>, <p>...</p>, <b>...</b>, <i>...</i>, <a>...</a>, <img...>, <table>... </table>, <div>...</div>, <form>...</form>, <input type="text">, and <input type= "submit"> Use an external css to change the default style of your webpage. You must use at least one element selector, one id selector, and one class selector Using text input and submit button, allow the user to change the background color of...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any...
Create a Web Page Using HTML, CSS, JS, jQuery • Create a web profile, including any of the following: • Your hobbies, likes/dislikes, career aspirations, dream job, favorite animals/ pets, favorite superhero, etc. • You do not have to share personal information that you are uncomfortable with sharing. Follow these guidelines when creating your page: • Include at least one heading (h1, h2, etc.) in your web page. • Provide a quote from a book, movie, etc. • Include at...
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
Task 1: HTML and CSS Create a website using HTML5 and CSS3 only. Please no JavaScript...
Task 1: HTML and CSS Create a website using HTML5 and CSS3 only. Please no JavaScript or Bootstrap. Website theme can be anything you want: a country, a town, a place, a hobby, people (yourself, your family...), pets, flowers, food, or anything that you find interesting or useful. It may be about real people/places/things or fictitious. Part 1: Content (HTML) After you decide the theme of your website, create HTML pages with the content you want to present. Remember to...
suggests on how to save the web portfolio Programming languages Chosen: HTML, CSS, and JavaScript. Project...
suggests on how to save the web portfolio Programming languages Chosen: HTML, CSS, and JavaScript. Project Name: Auto Portfolio Project Goal: Create a website that will generate a web portfolio using information inputted by the user. Product Backlog using user stories: As a user, I want to be able to save my web portfolio in a format which I can save so I can use the website later for my own needs. Example: http://www-personal.umich.edu/~hamatilo/
suggests on how to save the web portfolio Programming languages Chosen: HTML, CSS, and JavaScript. Project...
suggests on how to save the web portfolio Programming languages Chosen: HTML, CSS, and JavaScript. Project Name: Auto Portfolio Project Goal: Create a website that will generate a web portfolio using information inputted by the user. Product Backlog using user stories: As a user, I want to be able to save my web portfolio in a format which I can save so I can use the website later for my own needs.
Write a log-in log-out session using PHP, CSS, and HTML only. NO MYSQL and JavaScript allowed....
Write a log-in log-out session using PHP, CSS, and HTML only. NO MYSQL and JavaScript allowed. 1. Need an HTML form and a login form; separate file. 2. need a CSS file to make it look good 3. Must store visitors' info in a .txt file and validate from there if the user exists before granting access to the dashboard. If the user does not exist, render the form to signup. If the user exists take them to their dashboard....
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Assignment You should already have some experience with jQuery and some simple experience with JSON (from charting). In this assignment we will be creating some JSON data, parsing it, and displaying it. Step 1 – If you are not familiar with JSON you should complete the JSON tutorial at w3schools Step 2- You will now create a JSON file to represent some data of your...
Build a html form (making use of CSS and Javascript) with the following elements. The form...
Build a html form (making use of CSS and Javascript) with the following elements. The form must have validations and within a table format. • Name: a text box where the content must start with 1 upper case characters and no special character (i.e. !, @, #, $, %, &, *). Number is not allowed. The text box must not be empty. • Module code: a text box where the content must start with 3 lower case alphabets and follows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT