From 374f11e987a6bb81652f98c66c6f596e41b98ddd Mon Sep 17 00:00:00 2001 From: Valery Malyshev Date: Sat, 15 Jun 2024 01:46:37 +0300 Subject: [PATCH] A Needle in the Haystack --- 8_kyu/A Needle in the Haystack/README.md | 17 +++++ 8_kyu/A Needle in the Haystack/index.test.ts | 67 ++++++++++++++++++++ 8_kyu/A Needle in the Haystack/index.ts | 3 + README.md | 4 +- kata.md | 1 - 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 8_kyu/A Needle in the Haystack/README.md create mode 100644 8_kyu/A Needle in the Haystack/index.test.ts create mode 100644 8_kyu/A Needle in the Haystack/index.ts diff --git a/8_kyu/A Needle in the Haystack/README.md b/8_kyu/A Needle in the Haystack/README.md new file mode 100644 index 0000000..89257dc --- /dev/null +++ b/8_kyu/A Needle in the Haystack/README.md @@ -0,0 +1,17 @@ +## A Needle in the Haystack + +https://www.codewars.com/kata/56676e8fabd2d1ff3000000c + +Can you find the needle in the haystack? + +Write a function findNeedle() that takes an array full of junk but containing one "needle" + +After your function finds the needle it should return a message (as a string) that says: + +"found the needle at position " plus the index it found the needle, so: + +**Example(Input --> Output)** + +```js +["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5" +``` diff --git a/8_kyu/A Needle in the Haystack/index.test.ts b/8_kyu/A Needle in the Haystack/index.test.ts new file mode 100644 index 0000000..fa9f564 --- /dev/null +++ b/8_kyu/A Needle in the Haystack/index.test.ts @@ -0,0 +1,67 @@ +import { findNeedle } from "./index"; + +describe("Tests", () => { + it("example", () => { + const haystack1 = ["3", "123124234", undefined, "needle", "world", "hay", 2, "3", true, false]; + const haystack2 = [ + "283497238987234", + "a dog", + "a cat", + "some random junk", + "a piece of hay", + "needle", + "something somebody lost a while ago", + ]; + const haystack3 = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 8, + 7, + 5, + 4, + 3, + 4, + 5, + 6, + 67, + 5, + 5, + 3, + 3, + 4, + 2, + 34, + 234, + 23, + 4, + 234, + 324, + 324, + "needle", + 1, + 2, + 3, + 4, + 5, + 5, + 6, + 5, + 4, + 32, + 3, + 45, + 54, + ]; + + expect(findNeedle(haystack1)).toBe("found the needle at position 3"); + expect(findNeedle(haystack1)).toBe("found the needle at position 3"); + expect(findNeedle(haystack2)).toBe("found the needle at position 5"); + expect(findNeedle(haystack3)).toBe("found the needle at position 30"); + }); +}); diff --git a/8_kyu/A Needle in the Haystack/index.ts b/8_kyu/A Needle in the Haystack/index.ts new file mode 100644 index 0000000..4b2995e --- /dev/null +++ b/8_kyu/A Needle in the Haystack/index.ts @@ -0,0 +1,3 @@ +export function findNeedle(haystack: (string | boolean | number | undefined)[]): string { + return "found the needle at position " + haystack.indexOf("needle"); +} diff --git a/README.md b/README.md index fd28721..413572b 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ ### Katas solved -`Total`: 83 \ -`8_kyu`: 66 \ +`Total`: 84 \ +`8_kyu`: 67 \ `7_kyu`: 13 \ `6_kyu`: 4 \ `5_kyu`: 0 \ diff --git a/kata.md b/kata.md index 40b5e10..b4dbce7 100644 --- a/kata.md +++ b/kata.md @@ -1,4 +1,3 @@ -8 kyu - A Needle in the Haystack - https://www.codewars.com/kata/56676e8fabd2d1ff3000000c 8 kyu - Convert a string to an array - https://www.codewars.com/kata/57e76bc428d6fbc2d500036d 8 kyu - Fake Binary - https://www.codewars.com/kata/57eae65a4321032ce000002d 8 kyu - Calculate BMI - https://www.codewars.com/kata/57a429e253ba3381850000fb