Skip to content

Commit

Permalink
A Needle in the Haystack
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed Jun 14, 2024
1 parent 3c1bf20 commit 374f11e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
17 changes: 17 additions & 0 deletions 8_kyu/A Needle in the Haystack/README.md
Original file line number Diff line number Diff line change
@@ -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"
```
67 changes: 67 additions & 0 deletions 8_kyu/A Needle in the Haystack/index.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
3 changes: 3 additions & 0 deletions 8_kyu/A Needle in the Haystack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function findNeedle(haystack: (string | boolean | number | undefined)[]): string {
return "found the needle at position " + haystack.indexOf("needle");
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion kata.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 374f11e

Please sign in to comment.