Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Quiero mejorar mi código, pero no sé cómo en este momento. Me gustaria la opinion de alguien mas para que me diga como puedo mejorar

Entonces, un amigo mío me preguntó si podía hacer una función que comprimiera cadenas. Esa función comprime una cadena si un carácter se repite más de cuatro veces, como: "abbbbbbcccdddd" se convertirá en "a#6bccc#4d". Soy novato y quiero mejorar. Gracias por quien me está respondiendo :).

 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 Respostas
Responde à pergunta

0

Aquí está mi opinión sobre el problema:

 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 Relatório

0

Aquí está mi opinión al respecto:

 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)");

El núcleo de la acción ocurre en el .reduce() . Aquí comparo el carácter actual c con el anterior ( p ). Si es lo mismo, simplemente aumento el contador cnt de lo contrario, o en caso de que haya llegado al final de la cadena ( i==txt.length-1 ), agrego el último grupo de caracteres en su forma adecuada para arr y restablecer cnt a 1 . La matriz arr finalmente se .join() ed y se devuelve como la cadena resultante.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda