From c47b1ec2ccaa35ea89ade0860bc246a10857c1a1 Mon Sep 17 00:00:00 2001 From: koronya Date: Sun, 12 Apr 2026 19:04:28 +0900 Subject: [PATCH] [JS][6kyu] The Shell Game --- codewars/6kyu/the-shell-game/koronya.js | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 codewars/6kyu/the-shell-game/koronya.js diff --git a/codewars/6kyu/the-shell-game/koronya.js b/codewars/6kyu/the-shell-game/koronya.js new file mode 100644 index 000000000..260dd4d7c --- /dev/null +++ b/codewars/6kyu/the-shell-game/koronya.js @@ -0,0 +1,27 @@ +// [JS][6kyu] The Shell Game +// the-shell-game +// https://www.codewars.com/kata/546a3fea8a3502302a000cd2/train/javascript + +const findTheBall = (start, swaps) => { + let position = start + swaps.forEach(([start, end]) => { + if (position === start) { + position = end + } else if (position === end) { + position = start + } + }) + return position +} + +findTheBall(0, [[0, 1]]) === 1 +findTheBall(1, [[0, 1]]) === 0 + +// prettier-ignore +findTheBall(0, [[0, 1], [2, 1], [0, 1]]) === 2 +// prettier-ignore +findTheBall(0, [[0, 1], [1, 2], [2, 0], [0, 1], [1, 2], [2, 1], [2, 0], [0, 2]]) === 1 +// prettier-ignore +findTheBall(0, [[0, 2], [1, 0]]) === 2 +// prettier-ignore +findTheBall(1, [[0, 2], [1, 0]]) === 0