
- 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 each character number of times their one based index in a string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string of english lowercase alphabets.
Our function should construct a new string in which each character is repeated the number of times their 1-based index in the string in capital case and different character sets should be separated by dash ‘-’.
Therefore, the string ‘abcd’ should become −
"A-Bb-Ccc-Dddd"
Example
Following is the code −
const str = 'abcd'; const repeatStrings = (str) => { const res = []; for(let i = 0; i < str.length; i++){ const el = str[i]; let temp = el.repeat(i + 1); temp = temp[0].toUpperCase() + temp.substring(1, temp.length); res.push(temp); }; return res.join('-'); }; console.log(repeatStrings(str));
Output
A-Bb-Ccc-Dddd
- Related Articles
- Return index of first repeating character in a string - JavaScript
- Repeating string for specific number of times using JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- First non-repeating character using one traversal of string in C++
- Finding the 1-based index of a character in alphabets using JavaScript
- Replacing vowels with their 1-based index in a string in JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Find first repeating character using JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- Sorting string of words based on the number present in each word using JavaScript
- Trying to get number for each character in string - JavaScript
- Encoding string based on character frequency in JavaScript
- Maximum consecutive repeating character in string in C++
- How to build a string with no repeating character n separate list of characters? in JavaScript
- Python program to change character of a string using given index

Advertisements