0
<script>
function showCost(){
    let age = document.getElementById("age");
    if (age <= 6) {
         cost = "Free !";
    }
    else if (age <= 12) {
         cost = "$5";
    }
    else if (age < 18) {
         cost = "$8";
    }
    else if (age <= 64) {
         cost = "$12";
    }    
    else if (age >= 65) {
         cost = "$5";
    } 
alert(cost);
}


</script>


<input id="age" type="number" placeholder="Text">
    <button onclick="showCost()">Submit</button>

Uncaught ReferenceError ReferenceError: cost is not defined at showCost (c:\Users\Dillon\OneDrive\Desktop\folder\index.html:19:7) at onclick (c:\Users\Dillon\OneDrive\Desktop\folder\index.html:27:34)

  • 2
    Because you haven't declared the `cost` variable anywhere, see: [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let). – T.J. Crowder May 27 '22 at 14:33
  • Side note: The next problem after `cost` will be [this](https://stackoverflow.com/questions/43432375/javascript-value-not-working-with-input-text-box). :-) (You need `.value` to get the value of the element. And you'll want to parse it from string to number, my answer [here](https://stackoverflow.com/questions/28994839/why-does-string-to-number-comparison-work-in-javascript/28994875#28994875) describes your options for that.) – T.J. Crowder May 27 '22 at 14:34

1 Answers1

0

Credit to T.J. Crowder

I forgot to add .value at the end of my element haha !

before: let age = document.getElementById("age");

after: let age = document.getElementById("age").value;

  • Since that issue isn't the issue you asked about, and is covered by previous questions and answers, there's no need to post an answer about it. Your best bet here is to delete the question (and answer). Happy coding! – T.J. Crowder May 27 '22 at 14:53