How to return the singular/plural form of word based on input number in JavaScript?

JavascriptProgramming ScriptsWeb DevelopmentFront End Technology

In this tutorial, we will see how to return the singular or plural form of the word based on the user entered number.

Creating a plural form will help in replacing the words which are being repeated in the sentence. Generally, we will count all the occurrences of the words and if that count is multiple means more than once then we will replace that word with its plural form.

Procedure

Step 1 − Create a dictionary containing all the plurals words

Step 2 − If the word is repeated more than once then we will return its plural version

Example

Here is how our program will be

<!DOCTYPE html> <html> <head> <title>Singular/plural form of a word</title> </head> <body> <script> let pluralDict = { "apple": "apples", "pen": "pens", "car": "cars", "woman": "women", "lion": "lions", "school": "schools", "water": "water" } function make_plural(singularWord, count) { return count > 1 ? pluralDict[singularWord] : singularWord; } document.write(make_plural("school", 4)); document.write("<br>") document.write(make_plural("woman", 4)); document.write("<br>") document.write(make_plural("pen", 4)); document.write("<br>") document.write(make_plural("lion",4)); document.write("<br>") document.write(make_plural("water", 4)); document.write("<br>") document.write(make_plural("car", 4)); document.write("<br>") document.write(make_plural("water", 4)); </script> </body> </html>
raja
Updated on 11-Oct-2022 09:19:04

Advertisements