

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
The n times dribbling strings in 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.
Output
Then the output should be −
const output = 'hhooww aarree yyoouu'
Therefore, let’s write the code for this function −
Example
The code for this will be −
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));
The output in the console will be −
hhooww aarree yyoouu
- Related Questions & Answers
- Limiting elements occurrences to n times in JavaScript
- Is element repeated more than n times in JavaScript
- Repeating tuples N times in Python
- Calculate n + nn + nnn + ? + n(m times) in Python
- Longest string consisting of n consecutive strings in JavaScript
- JavaScript array: Find all elements that appear more than n times
- Add n binary strings?
- Calculate n + nn + nnn + … + n(m times) in Python program
- Calculate n + nn + nnn + u + n(m times) in Python Program
- Python Program to calculate n+nm+nmm.......+n(m times).
- Add n binary strings in C++?
- Are the strings anagrams in JavaScript
- Checking for overlapping times JavaScript
- Deleting occurrences of an element if it occurs more than n times using JavaScript
- Program to rotate a string of size n, n times to left in Python
Advertisements