Back to Interview Prep

Warm Up · Function, if-else

Check Voting Eligibility

Problem Statement

Write a program that accepts a number (age) and checks whether the person is eligible to vote. A person is eligible if their age is 18 or more.

Example

Input: 20

Process: Check if 20 ≥ 18 → Eligible

Output: You are eligible to vote.

Approach

  • Take the age as input (or define it in a variable).
  • Use a conditional statement to check whether the age is 18 or above.
  • If yes, print "You are eligible to vote."
  • Otherwise, print "You are not eligible to vote."

Visualisation

1Store the agelet age = 20;
2Check the conditionage >= 18 ?
3Yes → eligible"You are eligible to vote."
4No → not eligible"You are not eligible to vote."

Explanation

  • Accept the age as input.
  • Use if (age >= 18) to check eligibility.
  • Exactly one branch runs: the if block when the condition is true, the else block otherwise.
  • Show the appropriate message based on the condition - the logic is the same in every language.

Your solution

Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.

// Write your solution in JavaScript
// Print the answer with console.log()


Console

No output yet. Use console.log() to print values here.

Write your own solution and press Submit once to unlock the answer.

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in