Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Follow Instructions by dbirmajer
import assert from "assert";
function follow(instructions: string): [number, number] {
let x = 0;
let y = 0;
for (let d of instructions)
d === "f" ? y++ : d === "b" ? y-- : d === "r" ? x++ : x--;
return [x, y];
}
console.log('Example:');
console.log(follow('fflff'));
// These "asserts" are used for self-checking
assert.deepEqual(follow('fflff'), [-1, 4]);
assert.deepEqual(follow('ffrff'), [1, 4]);
assert.deepEqual(follow('fblr'), [0, 0]);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
Jan. 20, 2021