- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Longest string consisting of n consecutive strings in JavaScript
- Limiting elements occurrences to n times in JavaScript
- Is element repeated more than n times in JavaScript
- Explain the following properties:i) ($-$a1) $times$ ($-$a2) $times$ ($-$a3) $times$ ... $times$ ($-$an) = $-$ (a1 $times$ a2 $times$ a3 $times$ ... $times$ an), when n is odd.ii) ($-$a1) $times$ ($-$a2) $times$ ($-$a3) $times$ ... $times$ ($-$an) = (a1 $times$ a2 $times$ a3 $times$ ... $times$ an), when n is even.iii) ($-$a) $times$ ($-$a) $times$ ($-$a) $times$ ... n times = $-$ an, when n is odd. iv) (-$a) $times$ ($-$a) $times$ ($-$a) $times$ ... n times = an, when n is even. v) ($-$1) $times$ ($-$1) $times$ ($-$1) $times$ ... n times = $-$ 1, when n is odd.v) ($-$1) $times$ ($-$1) $times$ ($-$1) $times$ ... n times = 1, when n is even.
- Simplify the following:( frac{5^{n+3}-6 times 5^{n+1}}{9 times 5^{n}-2^{2} times 5^{n}} )
- Simplify the following:( frac{3^n times 9^{n+1}}{3^{n-1} times 9^{n-1}} )
- Simplify the following:( frac{5 times 25^{n+1}-25 times 5^{2 n}}{5 times 5^{2 n+3}-(25)^{n+1}} )
- Add n binary strings?
- Add n binary strings in C++?
- JavaScript array: Find all elements that appear more than n times
- Formatted Strings Using Template Strings in JavaScript
- Calculate n + nn + nnn + ? + n(m times) in Python
- Are the strings anagrams in JavaScript
- Repeating tuples N times in Python
- Template strings in JavaScript.

Advertisements