0

Hello sorry if this is a duplicate question but I was wondering if someone could help me out with my code I don't know what i'm missing i'm supposed to create a page that the user enters a age and it returns the result of the ticket prices.

Under age 5 entry is free Between ages 5 and 12 (inclusive) a child’s ticket costs $5.00 Older than 12 an adult ticket costs $9.00

Here's my code:

function myFunction() {
    var age; 
    var older;
    var young;
    
    age = document.getElementById("age").value;
    
    
    older = (age >= 13) ? "$9":"$5";
    
    document.getElementById("demo").innerHTML = older + " movie";
}
<body>

<p>Input your age and click the button:</p>

<input id="age" />

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
<p> Under age 5 entry is free

Between ages 5 and 12 (inclusive) a childís ticket costs $5.00

Older than 12 an adult ticket costs $9.00 </p>
<body>

3
  • 1
    What is wrong with it? Commented Sep 30, 2017 at 15:18
  • 1
    And what's the issure? Commented Sep 30, 2017 at 15:18
  • @stybl The "free" option is not evaluated Commented Sep 30, 2017 at 15:22

2 Answers 2

3

You can add check for age < 5 at beginning of conditional operator

function myFunction() {
    var age; 
    var older;
    var young;
    
    age = document.getElementById("age").value;       
    
    older = age < 5 ? "free" : (age >= 13) ? "$9" : "$5";
    
    document.getElementById("demo").innerHTML = older + " movie";
}
<body>

<p>Input your age and click the button:</p>

<input id="age" />

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
<p> Under age 5 entry is free

Between ages 5 and 12 (inclusive) a childís ticket costs $5.00

Older than 12 an adult ticket costs $9.00 </p>
<body>

Sign up to request clarification or add additional context in comments.

1 Comment

To be more understandable (with multiple tests and conditional operator, I allway add some () for each condition and for child conditional operation: older = (age < 5) ? "Free" : ((age >= 13) ? "$9" : "$5")
0

You can very easily achieve what you want by chaining ifs:

function myFunction() {
    var age = document.getElementById("age").value;
    var price;

    if (age >= 13) {
        price = "$9";
    }
    else if (age >= 5) {
        price = "$5";
    }
    else {
        price = "Free";
    }

    document.getElementById("demo").innerHTML = price + " movie";
}

Or, if you really, really want to use shorthand:

function myFunction() {
    var age = document.getElementById("age").value;
    var price = (age >= 13) ? "$9" : ((age < 5) ? "Free" : "$5");

    document.getElementById("demo").innerHTML = price + " movie";
}

The parentheses are optional. I've only included them to make it clear what is going on.

2 Comments

You have to switch the 5$ price with the Free result on the first example
Whoops. Edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.