Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
I wanna make my code better but don't know how at this point. I'd like someone else's opnion to tell me how can I improve

So a friend of mine asked if I could do a function that compresses strings. That function compresses a string if one character repeats itself more than four times, like: "abbbbbbcccdddd" will turn into "a#6bccc#4d". I'm noob and I wanna improve. Thank u for who is answering me :).

let text = '92HSSSS9U27888YSGGGGGG28SLKHFKLSPPPPSKWWW'

function compressText(text){
    return searchIqualLetters(text)
}

function searchIqualLetters(originalText){
    let modifyedText = ''

    for(let i = 0; i < originalText.length - 1; i++){

        for(let j = i + 1; j < originalText.length; j++){
            
            modifyedText = repeatFourTimes(i, j, originalText)
            if(modifyedText !== undefined && modifyedText !== ''){
                originalText = modifyedText
            }
        }
    }
    return originalText
}

function checkTextModifyed(){

}

function repeatFourTimes(i, j, text){
    if(text[i] == text[j] && text[i] == text[j + 1] && text[i] == text[j + 2]){
        return getLastIqualLetter(i, j, text)
    }
}

function getLastIqualLetter(i, j, text){
    let lastIqualLetterPosition = 0

    for(let k = 0; text[i] == text[j + k]; k++){
        lastIqualLetterPosition = j + k
    }

    return makeNewText(i, lastIqualLetterPosition, text)
}

function makeNewText(i, lastLetter, text){
    let cutedPart = text.slice(i, lastLetter + 1)

    let startPart = text.slice(0, i)
    let centerPart = '#' + cutedPart.length + cutedPart[0]
    let endPart = text.slice(lastLetter + 1, text.length)
    
    return startPart + centerPart + endPart 
}

console.log(compressText(text)) // Result: 92H#4S9U27888YS#6G28SLKHFKLS#4PSKWWW

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here's my take on the problem:

function condense(text) {
    let count = 0;
    let out = "";
    text.split("").forEach((el, i, arr) => { // el = current letter, i = index of letter, arr = the input text as an array
        if (arr[i + 1] != el) { // if the next letter is not the same as the current one
            if (count < 3) {
                out += el.repeat(count + 1) // if the sequence is less than 3 letters, then just do it normally
            } else {
                out += "#" + (count + 1) + el; // else, condense it
            }
            count = 0; // reset sequence length
        } else {
            count += 1; // increase count of repeated letter sequence length
        }
    })
    return out;
}
console.log(condense("aaabbbbcdddddee")); // Output: "aaab#4cd#5ee"

console.log(condense('92HSSSS9U27888YSGGGGGG28SLKHFKLSPPPPSKWWW'));
console.log("92H#4S9U27888YS#6G28SLKHFKLS#4PSKWWW (reference)");

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is my take on it:

const text = '92HSSSS9U27888YSGGGGGG28SLKHFKLSPPPPSKWWW';

function compr(txt){
  let  cnt=1,arr=[];
  txt.split("").reduce((p,c,i)=>{
    if(c==p) cnt++;
    if(c!=p || i==txt.length-1) {
     arr.push(cnt>3?`#${cnt}${p}`:p.repeat(cnt));
     cnt=1;
    }
    return c;
   });
   return arr.join("");
}

console.log(compr(text));
console.log("92H#4S9U27888YS#6G28SLKHFKLS#4PSKWWW (reference)");

The core of the action happens in the .reduce() loop. Here I compare the current character c with the previous one(p). If it is the same I simply increase the counter cnt otherwise - or in case I have reached the end of the string (i==txt.length-1) - I add the latest group of character in its suitable form to arr and reset cnt to 1. The arr array is eventually .join()ed and returned as the resulting string.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda