A pixel-art editor lets you move the selected cell with arrow keys. The canvas is width cells wide and height cells tall; columns count from 0 on the left and rows from 0 at the top. The cursor starts at start, given as [x, y] (column, row).
keys is a string of U, D, L, and R: up (row - 1), down (row + 1), left (column - 1), and right (column + 1). A key that would move the cursor off the canvas does nothing.
Write moveCursor(width, height, start, keys) that returns the final [x, y].
Input: width = 3, height = 3, start = [0,0], keys = "RRRDDL" Output: [1,2]
The third R would leave the canvas, so it is ignored. Then down, down, left.
Input: width = 4, height = 2, start = [3,1], keys = "DDRU" Output: [3,0]
Both D keys and the R would leave the canvas; only U moves.
1 <= width, height <= 1000start is on the canvas.0 <= keys.length <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #10 Keyboard Cursor on a Canvas. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.