-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters