I Have problem in my code :
function main() {
let input = prompt("Enter a string:").toLowerCase().replace(/s/g, "");
let process = "";
for (let i = 0; i < input.length; i++) {
let word = input[i];
let repeatWord = "";
for (let j = 0; j < word.length; j++) {
repeatWord += word[j].repeat(j + 1);
}
process += repeatWord;
}
console.log(process);
}
How to move letters based on existing index letters,
for example I want to enter the word: Learning Center
then my expected result is
leeearrnnigct
Thank You
4
Answers
When you run through each characters in the string, create a new index and then use a third variable to swap the characters.
You can try the code below.
My approach would be to create an array with all unique characters. Then iterate over it and count how many siblings each character has and then repeat the character as often as it appears.
Use a
Map
to store a counter of all the characters in your string. Then, sinceMap
preserves insertion order, you can simply iterate through the counter and use each key’s value to regenerate the string via therepeat()
method.Here is yet another version. This one will keep blanks and distinguish between upper/lower case letters:
If you prefer a case insensitive solution without blanks then simply modify it to: