BarBar288 commited on
Commit
77e60cf
·
verified ·
1 Parent(s): 2595494

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +25 -8
script.js CHANGED
@@ -12,10 +12,20 @@ const stateTaxRates = {
12
  "District of Columbia": 0.06
13
  };
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  window.onload = () => {
16
  const stateSelect = document.getElementById("state");
17
-
18
- // Populate dropdown
19
  for (const state in stateTaxRates) {
20
  const option = document.createElement("option");
21
  option.value = state;
@@ -27,7 +37,7 @@ window.onload = () => {
27
  fetch("https://ipapi.co/json/")
28
  .then(response => response.json())
29
  .then(data => {
30
- const userState = data.region; // Full state name
31
  if (stateTaxRates[userState]) {
32
  stateSelect.value = userState;
33
  }
@@ -39,15 +49,22 @@ window.onload = () => {
39
 
40
  function calculateTax() {
41
  const state = document.getElementById("state").value;
 
42
  const price = parseFloat(document.getElementById("price").value);
43
  const resultDiv = document.getElementById("result");
44
 
45
- if (!state || isNaN(price)) {
46
- resultDiv.textContent = "Please select a state and enter a valid price.";
47
  return;
48
  }
49
 
50
- const taxRate = stateTaxRates[state];
51
- const finalPrice = price * (1 + taxRate);
52
- resultDiv.textContent = `Final price in ${state}: $${finalPrice.toFixed(2)}`;
 
 
 
 
 
 
53
  }
 
12
  "District of Columbia": 0.06
13
  };
14
 
15
+ const taxExemptions = {
16
+ "grocery": ["Alabama", "Florida", "Texas", "California", "New York", "Pennsylvania"],
17
+ "clothing": ["Minnesota", "New Jersey", "Pennsylvania"],
18
+ "prescription": ["All"]
19
+ };
20
+
21
+ function isTaxExempt(state, itemType) {
22
+ if (itemType === "prescription") return true;
23
+ const exemptStates = taxExemptions[itemType] || [];
24
+ return exemptStates.includes(state);
25
+ }
26
+
27
  window.onload = () => {
28
  const stateSelect = document.getElementById("state");
 
 
29
  for (const state in stateTaxRates) {
30
  const option = document.createElement("option");
31
  option.value = state;
 
37
  fetch("https://ipapi.co/json/")
38
  .then(response => response.json())
39
  .then(data => {
40
+ const userState = data.region;
41
  if (stateTaxRates[userState]) {
42
  stateSelect.value = userState;
43
  }
 
49
 
50
  function calculateTax() {
51
  const state = document.getElementById("state").value;
52
+ const itemType = document.getElementById("itemType").value;
53
  const price = parseFloat(document.getElementById("price").value);
54
  const resultDiv = document.getElementById("result");
55
 
56
+ if (!state || !itemType || isNaN(price)) {
57
+ resultDiv.textContent = "Please select a state, item type, and enter a valid price.";
58
  return;
59
  }
60
 
61
+ let finalPrice;
62
+ if (isTaxExempt(state, itemType)) {
63
+ finalPrice = price;
64
+ resultDiv.textContent = `Final price in ${state} for ${itemType}: $${finalPrice.toFixed(2)} (Tax-Exempt)`;
65
+ } else {
66
+ const taxRate = stateTaxRates[state];
67
+ finalPrice = price * (1 + taxRate);
68
+ resultDiv.textContent = `Final price in ${state} for ${itemType}: $${finalPrice.toFixed(2)}`;
69
+ }
70
  }