Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Text Editor JavaScript issue with additional comma's

The code below takes input with a command and then appends it to the array. The result should show the history as if you kept typing and added it. I'm running into an issue where their extra comma appears right after the Hey on the 3rd iteration of the array and not 100% sure why that happens, I was able to remove some of the under commas by replacing but I'm not sure that right approach in this case.

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

output:

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

Expected:

[
  '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 answers
Answer question

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

just join your history :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!