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

112
Visualizações
Problema de JavaScript del editor de texto con comas adicionales

El siguiente código toma la entrada con un comando y luego lo agrega a la matriz. El resultado debería mostrar el historial como si siguiera escribiendo y lo agregara. Me encontré con un problema en el que su coma adicional aparece justo después de Hey en la tercera iteración de la matriz y no estoy 100% seguro de por qué sucede eso. Pude eliminar algunas de las comas inferiores reemplazándolas, pero no estoy seguro. ese enfoque correcto en este caso.

 class TextEditor { constructor(input) { this.input = input; this.output = []; this.history = []; this.forUndo = []; this.select = []; } textEditor() { let sortedInstructions = this.input.sort((a, b) => a[0] - b[0]); for (let instructor of sortedInstructions) { // Append if (instructor[0] === "APPEND") { if (this.select.length && this.output.length > 0) { let last = this.output.pop(); let newLast = last.slice(0, this.select[0]) + instructor[1] + last.slice(this.select[1]); this.output.push(newLast); } else { this.output.push(this.history + instructor[1]); this.forUndo.push(instructor[1], "APPEND"); this.history.push(instructor[1]); } } return this. output; } } } let input = [["APPEND", "Hey"], ["APPEND", " you"], ["APPEND", ", don't"], ["APPEND", " "], ["APPEND", "let me down"]]; let t = new TextEditor(input); console.log(t.textEditor()); let input = [["APPEND", "Hey"], ["APPEND", " you"], ["APPEND", ", don't"], ["APPEND", " "], ["APPEND", "let me down"]]; let t = new TextEditor(input); console.log(t.textEditor());

producción:

 [ 'Hey', 'Hey you', "Hey, you, don't", "Hey, you,, don't ", "Hey, you,, don't, let me down" ]

Esperado:

 [ 'Hey', 'Hey you', "Hey you, don't", "Hey you, don't ", "Hey you, don't let me down" ]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

class TextEditor { constructor(input) { this.input = input; this.output = []; this.history = []; this.forUndo = []; this.select = []; } textEditor() { let sortedInstructions = this.input.sort((a, b) => a[0] - b[0]); for (let instructor of sortedInstructions) { // Append if (instructor[0] === "APPEND") { if (this.select.length && this.output.length > 0) { /* let last = this.output.pop(); let newLast = last.slice(0, this.select[0]) + instructor[1] + last.slice(this.select[1]); this.output.push(newLast); */ } else { this.output.push(this.history.join("") + instructor[1]); this.forUndo.push(instructor[1], "APPEND"); this.history.push(instructor[1]); } } } return this.output; } } let input = [["APPEND", "Hey"], ["APPEND", " you"], ["APPEND", ", don't"], ["APPEND", " "], ["APPEND", "let me down"]]; let t = new TextEditor(input); console.log(t.textEditor());

Solo join a tu history :)

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