Home > other >  How to add a tax rate to my cost under javascript?
How to add a tax rate to my cost under javascript?

Time:10-18

I'm not sure how to add tax (7%) to my cost ($6) under Javascript. Can anyone help me?

html

<div class="item main">
<h1>Enter a Title</h1>
<p>Please enter a title to calculate how much it will cost<br>
<input id = "titleBox" type = "text">
<button onclick="calculateCost()">Enter</button> 
</p>
<p id= "output">Result</p>

    </div>

Javascript

var titleName;
var cost = 6;


function calculateCost() {

titleName = document.getElementById("titleBox").value;



var titleLetters; 
titleLetters = titleName.length;
var spaceCount = (titleName.split(" ").length - 1);
document.getElementById("output").innerHTML = "$"   (titleLetters - spaceCount) * cost;
/* "Red Car" "Red" "Car" */
}

CodePudding user response:

It is pretty basic(just simple math), here is the implementation:

    var titleName;
var cost = 6;
const tax = 7/100; // Added the tax

function calculateCost() {

titleName = document.getElementById("titleBox").value;



var titleLetters; 
titleLetters = titleName.length;
var spaceCount = (titleName.split(" ").length - 1);
document.getElementById("output").innerHTML = "$"   ((titleLetters - spaceCount) * cost*(1 tax)); // Since we have to add tax we have to use 1 tax
/* "Red Car" "Red" "Car" */
}

Note: scroll to the right to see the upgraded fromula since it got out of the stack overflow code box

  • Related