Created
March 31, 2025 18:28
-
-
Save tcelestino/24933b6c1990d8fb83e41e7c3ac41264 to your computer and use it in GitHub Desktop.
manipulate text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function manipulateText(text, command) { | |
let cursor = 0; | |
let result = text.split(''); | |
for (let i = 0; i < command.length; i++) { | |
const currentCommand = command[i]; | |
if (currentCommand === 'h') { | |
cursor = Math.max(0, cursor - 1); | |
} else if (currentCommand === 'l') { | |
cursor = Math.min(result.length - 1, cursor + 1); | |
} else if (currentCommand === 'r' && i + 1 < command.length) { | |
const newChar = command[i + 1]; | |
result[cursor] = newChar; | |
i++; | |
} | |
} | |
return `${result.join('')} - cursor: ${cursor}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment