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.
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
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...
Use HTML&CSS& JavaScript to: 3-4) Write a JQuery Ajax program which can communicate with flicker to...
Use HTML&CSS& JavaScript to: 3-4) Write a JQuery Ajax program which can communicate with flicker to get the last pictures taken from any place or related to any concept(s) the user has written in a textbox, say Sydney, train. Some information associated with the image like its topic, time taken, and its link needs to be appeared with it. 3-5) Develop a server which can serve the file you developed in 3-4. 3-6) Add a plugin to the real jQuery...
Calculator in JavaScript. No use of switch statements or CSS. Project Standards: Students will use click...
Calculator in JavaScript. No use of switch statements or CSS. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building a simple calculator in the browser. It should be able to add, subtract, divide, and multiply. Your program should do the following: Display a standard calculator to...
Assignment - Store Design and code a HTML/CSS/JS based web application for your online store. Assignment...
Assignment - Store Design and code a HTML/CSS/JS based web application for your online store. Assignment Specification · Minimum of SIX web pages are required for this assignment: § One Landing/Container Page ( a page with logo) § One Home Page § Three or more Product Line Pages (pages with product and it information ) § One Order Page (for customer to order a product after view the profuct on the product page) · Build internal CSS style sheets in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT