 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Padding a string with random lowercase alphabets to fill length in JavaScript
We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string.
Example
Let’s write the code for this function −
const padString = (str, len) => {    if(str.length < len){       const random = Math.floor(Math.random() * 26);       const randomAlpha = String.fromCharCode(97 + random);       return padString(str + randomAlpha, len);    };    return str; }; console.log(padString('abc', 10)); console.log(padString('QWERTY', 10)); console.log(padString('HELLO', 30)); console.log(padString('foo', 10));  Output
The output in the console −
abckoniucl QWERTYcwaf HELLOdnulywbogqhypgmylqlvmckhg Foofhfnhon
Advertisements
 