DEV Community

Cover image for How To Build A Simple Voting Eligibility Age Checker With JavaScript
BELLO ABD'QUADRI BOLAJI
BELLO ABD'QUADRI BOLAJI

Posted on

How To Build A Simple Voting Eligibility Age Checker With JavaScript

I'm a CS student currently documenting my learning journey. If you found this helpful or have a suggestion on how to improve the code, let me know!!!

Checking for eligibility is one of the first things we learn in programming as a student. Whether it is for a voting system, a driver's license app, or a simple age gate, the logic is the same.

In this tutorial of mine, I’ll show you how to build a basic Voters Eligibility Age Checker using JavaScript with if/else statements.

Requirement
To follow along, you just need:

  1. A basic text editor (like VS Code or even Notepad).
  2. A web browser (to run the code in the Console). The Logic: How it Works Before we code, let's look at the Plain English Logic:

We ask the user for their age.
If the age is 18 or older, they can vote.

Else (if they are younger than 18), they are not eligible yet.

The Java Script Code:

'''javascript
let ageInput = prompt("Please enter your age to check your voting eligibility:");

let userAge = Number(ageInput);

if (isNaN(userAge)) {
console.log("Error: Please enter a valid number for your age. ⚠️");
} else if (userAge >= 18) {
console.log("You are " + userAge + " years old. You're eligible to vote! ✅");
} else {
console.log("You are " + userAge + " years old. You aren't eligible to vote yet. ❌");
}

My Challenge:
Can you modify this code to tell the user exactly how many years they need to wait if they aren't 18 yet?
Post your solution in the comments!!

Top comments (0)