skip to Main Content

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


  1. 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.

    function suffle(target) {
      var chars = target.split("");
      var length = chars.length;
    
      for (var i = length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var tmp = chars[i];
        chars[i] = chars[j];
        chars[j] = tmp;
      }
    
      return chars.join("");
    }
    
    console.log(suffle("hello"));
    
    Login or Signup to reply.
  2. 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.

    function main(input: string) {
        const arr = input.toLocaleLowerCase().replace(/[ ]/g, '').split("");
        let uniqueLetters = [...new Set(arr)]
        let word = "";
        for (let i = 0; i < uniqueLetters.length; i++) {
            const letter = uniqueLetters[i];   
            const letterCount = arr.filter((l) => l == letter).length;
            word += letter.repeat(letterCount)
        }
        return word;
    }
    
    console.log(main("Learning Center")) // prints: leeearrnnnigct
    
    Login or Signup to reply.
  3. Use a Map to store a counter of all the characters in your string. Then, since Map preserves insertion order, you can simply iterate through the counter and use each key’s value to regenerate the string via the repeat() method.

    function rearrange(str) {
      str = str.toLowerCase().replace(/s/g, "");
      
      const counter = new Map();
      for (const char of str) {
        if (counter.has(char)) {
          counter.set(char, counter.get(char) + 1);
        } else {
          counter.set(char, 1);
        } 
      }
    
      const strArr = [];
      for (const [key, value] of counter) {
        strArr.push(key.repeat(value));
      }
    
      return strArr.join("");
    }
    
    rearrange("Learning Center"); // leeearrnnigct
    
    Login or Signup to reply.
  4. Here is yet another version. This one will keep blanks and distinguish between upper/lower case letters:

    const str="Learning Center".split(""),
      freq={};
    str.forEach(c=>freq[c]=(freq[c]??0)+1);
    const res=str.map(c=>{
     let s=c.repeat(freq[c]);
     freq[c]=0;
     return s
    }).join("");
    
    console.log(res);

    If you prefer a case insensitive solution without blanks then simply modify it to:

    const str="Learning Center".toLowerCase().replaceAll(" ","").split(""),
      freq={};
    str.forEach(c=>freq[c]=(freq[c]??0)+1);
    const res=str.map(c=>{
     let s=c.repeat(freq[c]);
     freq[c]=0;
     return s
    }).join("");
    
    console.log(res);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search