
- 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
Repeating letter string - JavaScript
We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times.
For example: If the string is −
const str = 'how are you'
And the number n is 2
Then the output should be −
const output = 'hhooww aarree yyoouu'
Example
Following is the code −
const str = 'how are you'; const repeatNTimes = (str, n) => { let res = ''; for(let i = 0; i < str.length; i++){ // using the String.prototype.repeat() function res += str[i].repeat(n); }; return res; }; console.log(repeatNTimes(str, 2));
Output
Following is the output in the console −
hhooww aarree yyoouu
- Related Articles
- JavaScript Count repeating letter
- Finding missing letter in a string - JavaScript
- Repeating string for specific number of times using JavaScript
- Array filtering using first string letter in JavaScript
- JavaScript construct an array with elements repeating from a string
- Detecting the first non-repeating string in Array in JavaScript
- Return index of first repeating character in a string - JavaScript
- Inserting empty string in place of repeating values in JavaScript
- Check if a string is repeating in itself in JavaScript
- Make first letter of a string uppercase in JavaScript?
- Finding the first non-repeating character of a string in JavaScript
- Change every letter to next letter - JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- Mapping the letter of a string to an object of arrays - JavaScript
- Finding the immediate next character to a letter in string using JavaScript

Advertisements