Finding number plate based on registration number in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

The car registering system of a city N assigns two types of numbers −

  • The Customer ID − a natural number between 0 and 17558423 inclusively, which is assigned to the car buyers in the following order: the first customer receives ID 0, the second customer receives ID 1, the third customer receives ID 2, and so on;

  • Number Plate − assigned to the car and contains the series ( three Latin lowercase letters from a to z) and the serial number (three digits from 0 to 9).

    Example − aaa001. Each Number Plate is related to the given Customer ID. For example: Number Plate aaa001 is related to Customer ID 0; Number Plate aaa002 is related to Customer ID 1, and so on.

We are required to write a JavaScript function that takes in the customerID and calculates the Number Plate corresponding to this ID and returns it as a string.

Example

Following is the code −

 Live Demo

const id = 545664;
const findNumberPlate = (id = 0) => {
   const letters = 'abcdefghijklmnopqrstuvwxyz';
   let num = String(id % 999 + 1);
   if(num.length !== 3);
      while(num.length !== 3){
         num = '0' + num;
      };
      const l = Math.floor(id / 999);
      return letters[l % 26] + letters[(Math.floor(l / 26)) % 26] + letters[(Math.floor(l / (26 * 26))) % 26] + num;
};
console.log(findNumberPlate(id));

Output

Following is the console output −

ava211
raja
Published on 17-Apr-2021 11:08:27
Advertisements