We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
function readLine(): string {
return inputLines[currentLine++];
}
class TextEditor {
private currentText:string;
// Enter your code here
constructor(){
this.currentText =""
}
append(w:string){
// Enter your code here
}
delete(k:number){
// Enter your code here
}
print(k:number){
// Enter your code here
}
undo(){
// Enter your code here
}
}
type EditorMethod = "append" | "delete" | "print" | "undo";
function main() {
const commandMap =
{"1": "append",
"2": "delete",
"3" : "print",
"4": "undo" } as const;
const textEditor = new TextEditor();
inputLines.shift()
inputLines.map((inputLine:string) => {
const [command, input] = inputLine.split(" ") as ["1"|"2"|"3"|"4", string]
const method = commandMap[command]
if (method == "undo") return textEditor.undo()
if (method == "append") return textEditor.append(input);
return textEditor[method](Number(input))
} );
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
typescript boilerplate: 'use strict';
process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString: string = ''; let inputLines: string[] = []; let currentLine: number = 0; process.stdin.on('data', function(inputStdin: string): void { inputString += inputStdin; });
process.stdin.on('end', function(): void { inputLines = inputString.split('\n'); inputString = ''; main(); });
function readLine(): string { return inputLines[currentLine++]; }
class TextEditor { private currentText:string; // Enter your code here constructor(){ this.currentText ="" } append(w:string){ // Enter your code here } delete(k:number){ // Enter your code here } print(k:number){ // Enter your code here } undo(){ // Enter your code here } }
type EditorMethod = "append" | "delete" | "print" | "undo";
function main() { const commandMap = {"1": "append", "2": "delete", "3" : "print", "4": "undo" } as const;
}