Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding number plate based on registration number in JavaScript
The car registering system of a city assigns unique identifiers to customers and corresponding number plates to their vehicles. We need to create a JavaScript function that converts a customer ID into its corresponding number plate format.
Problem Statement
The car registration system assigns two types of identifiers:
-
Customer ID ? A natural number between 0 and 17,558,423 (inclusive), assigned sequentially starting from 0
-
Number Plate ? Contains a series of three Latin lowercase letters (a-z) followed by a three-digit serial number (000-999)
Example: aaa001, aaa002, etc. Each number plate corresponds to a specific Customer ID in sequential order.
How the Mapping Works
The relationship between Customer ID and Number Plate follows this pattern:
- Customer ID 0 ? aaa001
- Customer ID 1 ? aaa002
- Customer ID 999 ? aab001
- Customer ID 1000 ? aab002
Solution
const findNumberPlate = (id = 0) => {
const letters = 'abcdefghijklmnopqrstuvwxyz';
// Calculate the numeric part (001-999, then reset)
let num = String((id % 999) + 1);
// Pad with leading zeros to make it 3 digits
while(num.length !== 3) {
num = '0' + num;
}
// Calculate letter combination based on groups of 999
const letterIndex = Math.floor(id / 999);
// Generate three letters using base-26 conversion
const firstLetter = letters[letterIndex % 26];
const secondLetter = letters[Math.floor(letterIndex / 26) % 26];
const thirdLetter = letters[Math.floor(letterIndex / (26 * 26)) % 26];
return firstLetter + secondLetter + thirdLetter + num;
};
// Test examples
console.log(findNumberPlate(0)); // First customer
console.log(findNumberPlate(1)); // Second customer
console.log(findNumberPlate(999)); // 1000th customer
console.log(findNumberPlate(545664)); // Given example
aaa001 aaa002 aab001 ava665
Step-by-Step Breakdown
The algorithm works as follows:
-
Numeric Part: Calculate
(id % 999) + 1to get numbers 1-999, then pad with zeros -
Letter Group: Use
Math.floor(id / 999)to determine which letter combination group - Base-26 Conversion: Convert the group number to three letters using modulo operations
Key Points
- Each group of 999 customer IDs shares the same three letters
- The numeric part cycles from 001 to 999
- Letters follow alphabetical order: aaa, aab, aac, ..., aba, abb, etc.
- The function handles the full range of 17,558,423+ customer IDs
Conclusion
This solution efficiently converts customer IDs to number plates using modulo arithmetic and base-26 conversion. The algorithm ensures each customer gets a unique, sequential number plate in the required format.
