
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Appending suffix to numbers in JavaScript
Problem
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument.
The task of our function is to append ‘st’, ‘nd’, ‘rd’, ‘th’ to the number according to the following rules:
- st is used with numbers ending in 1 (e.g. 1st, pronounced first)
- nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second)
- rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)
- As an exception to the above rules, all the "teen" numbers ending with 11, 12 or 13 use - th (e.g. 11th, pronounced eleventh, 112th, pronounced one hundred [and] twelfth)
- th is used for all other numbers (e.g. 9th, pronounced ninth).
For example, if the input to the function is −
Input
const num = 4513;
Output
const output = '4513th';
Output Explanation
Even though 4513 ends with three, 13 is an exception case that must be appended with th
Example
Following is the code −
const num = 4513; const appendText = (num = 1) => { let suffix = "th"; if (num == 0) suffix = ""; if (num % 10 == 1 && num % 100 != 11) suffix = "st"; if (num % 10 == 2 && num % 100 != 12) suffix = "nd"; if (num % 10 == 3 && num % 100 != 13) suffix = "rd"; return num + suffix; }; console.log(appendText(num));
Output
4513th
- Related Articles
- How to keep appending subdocuments in MongoDB?
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- Suffix Array
- Appending an entry in one to many embedded documents with MongoDB
- How to add suffix to column names in R?
- Appending data to a MySQL field that already has data in it?
- Python – Convert Suffix denomination to Values
- match_results prefix() and suffix() in C++
- How to declare numbers in JavaScript?
- Map numbers to characters in JavaScript
- Indexing numbers to alphabets in JavaScript
- How to generate random numbers between two numbers in JavaScript?
- How to validate decimal numbers in JavaScript?
- How to generate Prime Numbers in JavaScript?
- How to compare two numbers in JavaScript?

Advertisements