From 64b8e4615398672fe1d26ca00d2b380ebf97aebc Mon Sep 17 00:00:00 2001 From: Valery Malyshev Date: Sun, 19 May 2024 00:04:27 +0300 Subject: [PATCH] Enumerable Magic #1 - True for All? --- .../Enumerable Magic #1 - True for All?/README.md | 15 +++++++++++++++ .../index.test.ts | 8 ++++++++ .../Enumerable Magic #1 - True for All?/index.ts | 3 +++ README.md | 4 ++-- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 8_kyu/Enumerable Magic #1 - True for All?/README.md create mode 100644 8_kyu/Enumerable Magic #1 - True for All?/index.test.ts create mode 100644 8_kyu/Enumerable Magic #1 - True for All?/index.ts diff --git a/8_kyu/Enumerable Magic #1 - True for All?/README.md b/8_kyu/Enumerable Magic #1 - True for All?/README.md new file mode 100644 index 0000000..1f23a45 --- /dev/null +++ b/8_kyu/Enumerable Magic #1 - True for All?/README.md @@ -0,0 +1,15 @@ +## Enumerable Magic #1 - True for All? + +Create a method all which takes two params: + +- a sequence +- a function (function pointer in C) + +and returns true if the function in the params returns true for every element in the sequence. Otherwise, it should return false. If the sequence is empty, it should return true, since technically nothing failed the test. + +### Example + +``` +all((1, 2, 3, 4, 5), greater_than_9) -> false +all((1, 2, 3, 4, 5), less_than_9) -> True +``` diff --git a/8_kyu/Enumerable Magic #1 - True for All?/index.test.ts b/8_kyu/Enumerable Magic #1 - True for All?/index.test.ts new file mode 100644 index 0000000..f5e46d7 --- /dev/null +++ b/8_kyu/Enumerable Magic #1 - True for All?/index.test.ts @@ -0,0 +1,8 @@ +import { all } from "./index"; + +describe("Tests", () => { + it("all", () => { + expect(all([1, 2, 3, 4, 5], (v) => v < 9)).toBe(true); + expect(all([1, 2, 3, 4, 5], (v) => v > 9)).toBe(false); + }); +}); diff --git a/8_kyu/Enumerable Magic #1 - True for All?/index.ts b/8_kyu/Enumerable Magic #1 - True for All?/index.ts new file mode 100644 index 0000000..0aee583 --- /dev/null +++ b/8_kyu/Enumerable Magic #1 - True for All?/index.ts @@ -0,0 +1,3 @@ +export function all(arr: number[], fun: (n: number) => boolean): boolean { + return arr.every(fun); +} diff --git a/README.md b/README.md index c2b7496..2c6d9d7 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ ### Katas solved -`Total`: 34 \ -`8_kyu`: 33 \ +`Total`: 35 \ +`8_kyu`: 34 \ `7_kyu`: 0 \ `6_kyu`: 1 \ `5_kyu`: 0 \