Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Created March 31, 2025 18:28
Show Gist options
  • Select an option

  • Save tcelestino/24933b6c1990d8fb83e41e7c3ac41264 to your computer and use it in GitHub Desktop.

Select an option

Save tcelestino/24933b6c1990d8fb83e41e7c3ac41264 to your computer and use it in GitHub Desktop.

Revisions

  1. tcelestino created this gist Mar 31, 2025.
    20 changes: 20 additions & 0 deletions manipulate-text.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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}`;
    }