Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Created March 31, 2025 18:28
Show Gist options
  • Save tcelestino/24933b6c1990d8fb83e41e7c3ac41264 to your computer and use it in GitHub Desktop.
Save tcelestino/24933b6c1990d8fb83e41e7c3ac41264 to your computer and use it in GitHub Desktop.
manipulate text
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