/    Sign up×
Community /Pin to ProfileBookmark

Tax Calculator Prototype Issues

I want to turn this school assignment into the perfect JavaScript, but I am new at this and am having trouble. No thanks to the idiots that teach at ITT Tech, I have been working with this for a couple days now. If anyone could help I would be grateful.

First of all, the document.write is not working in IE, though it works in all other browsers. So, to view the page, you will have to use another browser. Second of all I tried using .replace at the prompt with regular expressions and was able to strip the $ and letters and commas out of numbers, but I must not be doing the regular expressions right with regards to special characters. I need to use the same method to strip everything but letters and periods from county names. The method I am using for example is like so:

var mypurchase = eval(prompt(‘Please enter the purchase price of the product:’.replace(/regularexpressions/g, ”)));

I also found that the textbook’s code for adding the right number of zeroes doesn’t do it correctly. If the result should be something like $450.00, it comes out $450.0. If the result should be $450.50, it is $450.50.

I also saw something on the Web that very vaguely attempted to describe how to control the size and position of the prompt box, but as you probably know most posts on the Web are not easy to pick up on, unless you are the author, of course.

I need to also know how to keep it from return $NaN.0 or $undefined as a result.

I also want it to prompt the user to enter a tax rate for a county that is not listed as an option, so that it can perform a calculation anyway. I am sure I can figure out how to do that when I get back to it later on tonight. I am going to just add some code to the default of the switch statement.

I know I don’t have to use document.write in the body, but I plan to add code to determine whether or not it will appear or I will replace it with other text if the visitor ignores the prompts. I am going to change the places to real places and have it calculate the cost of cigarettes in different areas. That is the kind of thing I do when I get bored. LOL!

<html>
<head>
<title>Tax Rate JavaScript</title>
<script language = “javascript” type = “text/javascript”>
<!–
var mycounty = prompt(‘Please enter your countyn(Cuyahoga, Franklin, Hamilton, Lucas, Montgomery, Stark, or Summit):’, ”);
mycounty = mycounty.toUpperCase();
if (mycounty == null) {
entercounty();
}
else if (mycounty == ”) {
entercounty();
}
else {
var mypurchase = eval(prompt(‘Please enter the purchase price of the product:’, ”));
if (mypurchase == null) {
enteramount();
}
else if (mypurchase == ”) {
enteramount();
}
else {
var totalprice = calculatetax(mycounty, mypurchase);
var goround = displayedprice(totalprice, 2);
alert(‘The total price with taxes is: $’ + goround);
}
}
function displayedprice(pricetoround, decimals) {
var result1 = pricetoround * Math.pow(10, decimals);
var result2 = Math.round(result1);
var result3 = result2 / Math.pow(10, decimals);
return addzeroes(result3, decimals);
}
function addzeroes(roundedprice, decimalplaces) {
var pricetostring = roundedprice.toString();
var decimallocation = pricetostring.indexOf(‘.’);
if (decimallocation == -1) {
decimalparts = 0;
pricetostring += decimalplaces > 0 ? ‘.’ : ”;
}
else {
decimalparts = pricetostring.length – decimallocation -1;
}
var zeroesadded = decimalplaces – decimalparts;
if (zeroesadded > 0) {
for (var counter = 1; counter <= zeroesadded; counter++);
pricetostring += ‘0’
}
return pricetostring;
}
function entercounty() {
alert(‘No county was entered!’);
}
function enteramount() {
alert(‘No amount was entered!’);
}
function calculatetax(county, taxableprice) {
var taxedprice;
switch(county) {
case ‘CUYAHOGA’:
taxedprice = taxableprice *
1.08
break;
case ‘FRANKLIN’:
taxedprice = taxableprice * 1.0675
break;
case ‘HAMILTON’:
taxedprice = taxableprice *
1.07
break;
case ‘LUCAS’:
taxedprice = taxableprice * 1.0725
break;
case ‘MONTGOMERY’:
taxedprice = taxableprice *
1.075
break;
case ‘STARK’:
taxedprice = taxableprice * 1.065
break;
case ‘SUMMIT’:
taxedprice = taxableprice *
1.0675
break;
default :
alert(‘The county ‘ + county + ‘ is not listed!’)
taxedprice = ‘0.00’;
}
return taxedprice;
}
var cuyahogaprice = mypurchase * 1.08
var docuyahoga = displayedprice(cuyahogaprice, 2);
var franklinprice = mypurchase *
1.0675
var dofranklin = displayedprice(franklinprice, 2);
var hamiltonprice = mypurchase * 1.07
var dohamilton = displayedprice(hamiltonprice, 2);
var lucasprice = mypurchase *
1.0725
var dolucas = displayedprice(lucasprice, 2);
var montgomeryprice = mypurchase * 1.075
var domontgomery = displayedprice(montgomeryprice, 2);
var starkprice = mypurchase *
1.065
var dostark = displayedprice(starkprice, 2);
var summitprice = mypurchase * 1.0675
var dosummit = displayedprice(summitprice, 2);
//–>
</script>
<style type = “text/css”>
table {width:220px; background-color:#8b0000; border-color:#ff0000; border-style:groove; border-width:6px; text-align:left; font-family:”trebuchet ms”; font-size:100%; color:#ffffff; padding:5px; margin-left:auto; margin-right:auto}
h3 {text-align:center}
</style>
</head>
<body>
<noscript>JavaScripts are not enabled on this browser.</noscript>
<script type=”text/javascript”>
document.open();
document.write(‘<table><tr><td colspan = “2”><h3>All County Taxes</h3></td></tr><tr><td>Cuyahoga</td><td>$’);
document.write(docuyahoga);
document.write(‘</td></tr><tr><td>Franklin</td><td>$’);
document.write(dofranklin);
document.write(‘</td></tr><tr><td>Hamilton</td><td>$’);
document.write(dohamilton);
document.write(‘</td></tr><tr><td>Lucas</td><td>$’);
document.write(dolucas);
document.write(‘</td></tr><tr><td>Montgomery</td><td>$’);
document.write(domontgomery);
document.write(‘</td></tr><tr><td>Stark</td><td>$’);
document.write(dostark);
document.write(‘</td></tr><tr><td>Summit</td><td>$’);
document.write(dosummit);
document.write(‘</td></tr></table>’);
document.close();
</script>
</body>
</html>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Badgodwand spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...