-1

I got a script in which if you click on an apple, grape, or banana, it will alert you the price, with the tax from the state that you selected. But, for some reason I keep on getting NaN. It started coming NaN when I added the script that gets the percent from the number.

var apple = 2
var bananna = 3
var grapes = 4
var NJ = 6.625
var NY = 8.225
var PA = 6
var FL = 8
var e = document.getElementById("state").value

var oi;
if (e == NJ) {
  oi = NJ
} else if (e == NY) {
  oi == NY
} else if (e == PA) {
  oi == PA
} else {
  oi == FL
}

function myFunction() {
  var percent = (oi / 100) * grapes;
  alert(percent)
}

function yFunction() {
  var percent = (oi / 100) * apple;
  alert(percent)
}

function mFunction() {
  var percent = (oi / 100) * bananna;
  alert(percent)
}
<h2>Enter state of residence.</h2>
<select name="state" class="state" id="state">
  <option value="NJ">NJ</option>
  <option value="NY">NY</option>
  <option value="PA">PA</option>
  <option value="FL">FL</option>
</select>

<h2> Click on what you wish to buy.</h2>
<button type="button" id="i"> <img src="https://www.applesfromny.com/wp-content/uploads/2020/05/Jonagold_NYAS-Apples2.png" onclick = "yFunction()" height ="80" width="100" /></button>
<button type="button" id="o"> <img src="https://th-thumbnailer.cdn-si-edu.com/xK6NAJHiv_51fzn5sDiQt0eD5Is=/fit-in/1600x0/https://tf-cmsv2-smithsonianmag-media.s3.amazonaws.com/filer/d5/24/d5243019-e0fc-4b3c-8cdb-48e22f38bff2/istock-183380744.jpg" onclick = "mFunction()" height ="80" width="100" /></button>
<button type="button" id="h"> <img src="https://www.meijer.com/content/dam/meijer/product/0000/00/0004/02/0000000004022_2_A1C1_1200.png" onclick = "myFunction()" height ="80" width="100" /></button><br>
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Ga mer
  • 11
  • 2
  • I think there is value attribute for select too. You can set the select value = selected Option. this way you can get the selected value. – Wasif Ali Feb 22 '22 at 21:14
  • 2
    You're not setting `oi` to anything for most conditions. `==` is a comparison operator, non an assignment operator. If you want to assign a value, use `=`, a single equals sign. You're also not evaluating the select value on the fly, to it's going to be whatever it is at load, e.g.: "NJ". – canon Feb 22 '22 at 21:18
  • Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Feb 22 '22 at 21:20
  • Also, listen for events on elements which naturally emit the events. For instance, a `button` element naturally emits a `click` event. `img` does not. It will, but then you might as well just have three `img` elements rather than three `button` elements with nested `img` elements within them. – Heretic Monkey Feb 22 '22 at 21:24
  • `` is quite useless; use `` instead; change the other options accordingly. IDs are not appropriate for storing arbitrary data; replace `id="i"` by a custom attribute like `data-item="apple"`; replace other IDs accordingly. Alternatively, do `data-cost="2"` for apples; update other custom attributes accordingly. Prefer `console.log` over `alert`; use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`). – Sebastian Simon Feb 22 '22 at 21:34
  • Then the entire JS can be rewritten as `const items = { apple: 2, banana: 3, grapes: 4 }; addEventListener("click", ({ target }) => { const button = target.closest("[data-item]"); if(button){ console.log(document.getElementById("state").value / 100 * items[button.dataset.item]); } });`. Alternatively, using `data-cost`: `addEventListener("click", ({ target }) => { const button = target.closest("[data-cost]"); if(button){ console.log(document.getElementById("state").value / 100 * button.dataset.cost); } });`. You could also utilize the `value` attribute on ` – Sebastian Simon Feb 22 '22 at 21:34
  • `bananna` should be `banana` – Roko C. Buljan Feb 22 '22 at 21:37
  • Math. Not sure what you're calculating but `(oi / 100) * grapes` can be expressed like `oi / 100 * grapes` – Roko C. Buljan Feb 22 '22 at 21:39
  • @canon ty you're comment fixed it. – Ga mer Feb 22 '22 at 21:39
  • @RokoC.Buljan yeah wasn't paying attention – Ga mer Feb 22 '22 at 21:40
  • 1
    _Sigh_. [Duplicate](//google.com/search?q=site:stackoverflow.com+js+input+value+always+empty) of [Why is the value of my input always empty if I store it in a variable?](/q/58078160/4642212) and [What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)](/q/11871616/4642212), among several other issues. – Sebastian Simon Feb 22 '22 at 21:52

2 Answers2

1
  • Use Strings not variable names when comparing the value
  • Use the Assignment operator when doing oi = NY etc, just like you did for oi = NJ
if (e == NJ) {         // should be a String: "NJ"
  oi = NJ              // (this one is correct)
} else if (e == NY) {  // should be a String: "NY"
  oi == NY             // should use assignment operator = not ==
// etc...
  • You should get the Select box value on click, not beforehand.
  • Don't use inline on* attributes. JS should be in one place only and that's its respective tag or file. Use Element.addEventListener() instead.
  • Don't copy/paste functions. That's not the meaning of programming. Detect instead the similarities and create a single reusable function.
  • Store your Data into objects.
  • Use HTML data-* attribute on your buttons to define the item target property. use JS's Element.dataset to than retrieve the stored value.

Remake suggestion:

const oi = {
  NJ: 6.625,
  NY: 8.225,
  PA: 6,
  FL: 8
};

const prices = {
  apple: 2,
  banana: 3,
  grape: 4
};

const calc = (item) => {
  const state = document.querySelector("#state").value;
  const result = oi[state] / 100 * prices[item];
  alert(result);
};

document.querySelectorAll("[data-item]").forEach(el => {
  el.addEventListener("click", () => calc(el.dataset.item));
});
<select id="state">
  <option value="NJ">NJ</option>
  <option value="NY">NY</option>
  <option value="PA">PA</option>
  <option value="FL">FL</option>
</select>
<h2> Click on what you wish to buy.</h2>
<button type="button" data-item="apple">apple</button>
<button type="button" data-item="banana">banana</button>
<button type="button" data-item="grape">grape</button>

Disclaimer: I have no clue what you're actually calculating, but there you go.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
-1

The state value is never set in your codes, so oi is never set also. Here are modified codes. Your codes can be optimized/simplified. For example, you don't need three functions to calculate price (btw your calculation is incorrect; it only calculated the tax).

var apple = 2
var bananna = 3
var grapes = 4
var NJ = 6.625
var NY = 8.225
var PA = 6
var FL = 8
// var e = document.getElementById("state").value
var e = ''
var oi = NJ  // initial state

function state(o)
{
   if(o.value == 'NJ') {
      oi = NJ
   } else if(o.value == 'NY') {
      oi = NY
   } else if(o.value == 'PA') {
      oi = PA
   } else {
      oi = FL
   }
}
function myFunction() {
  var percent = (oi / 100) * grapes;
  alert(percent);
}

function yFunction() {
  var percent = (oi / 100) * apple;
  alert(percent)
}

function mFunction() {
  var percent = (oi / 100) * bananna;
  alert(percent)
}


<h2>Enter state of residence.</h2>
<select name="state" class="state" id="state" onChange="state(this);">
  <option value="NJ">NJ</option>
  <option value="NY">NY</option>
  <option value="PA">PA</option>
  <option value="FL">FL</option>
</select>

<h2> Click on what you wish to buy.</h2>
<button type="button" id="i"> <img src="https://www.applesfromny.com/wp-content/uploads/2020/05/Jonagold_NYAS-Apples2.png" onclick = "yFunction()" height ="80" width="100" /></button>
<button type="button" id="o"> <img src="https://th-thumbnailer.cdn-si-edu.com/xK6NAJHiv_51fzn5sDiQt0eD5Is=/fit-in/1600x0/https://tf-cmsv2-smithsonianmag-media.s3.amazonaws.com/filer/d5/24/d5243019-e0fc-4b3c-8cdb-48e22f38bff2/istock-183380744.jpg" onclick = "mFunction()" height ="80" width="100" /></button>
<button type="button" id="h"> <img src="https://www.meijer.com/content/dam/meijer/product/0000/00/0004/02/0000000004022_2_A1C1_1200.png" onclick = "myFunction()" height ="80" width="100" /></button><br>


Shiping
  • 1,203
  • 2
  • 11
  • 21