From 12a3ee7c21f1e97039c8147690aff7b588c20559 Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Thu, 20 Nov 2025 08:31:50 -0800 Subject: [PATCH] feat: today's freecodecamp challenge --- .../daily/2025-11-20/main.spec.ts | 23 +++++++++++++++++++ src/freecodecamp/daily/2025-11-20/main.ts | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/freecodecamp/daily/2025-11-20/main.spec.ts create mode 100644 src/freecodecamp/daily/2025-11-20/main.ts diff --git a/src/freecodecamp/daily/2025-11-20/main.spec.ts b/src/freecodecamp/daily/2025-11-20/main.spec.ts new file mode 100644 index 0000000..5303c73 --- /dev/null +++ b/src/freecodecamp/daily/2025-11-20/main.spec.ts @@ -0,0 +1,23 @@ +/** + * @copyright NHCarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ + +import { describe, it, expect } from "vitest"; +import { longestWord } from "./main.js"; + +describe("longestWord", () => { + it("should return the correct longest word", () => { + expect(longestWord("The quick red fox")).toBe("quick"); + expect(longestWord("Hello coding challenge.")).toBe("challenge"); + expect(longestWord("Do Try This At Home.")).toBe("This"); + expect( + longestWord( + "This sentence... has commas, ellipses, and an exlamation point!", + ), + ).toBe("exlamation"); + expect(longestWord("A tie? No way!")).toBe("tie"); + expect(longestWord("Wouldn't you like to know.")).toBe("Wouldnt"); + }); +}); diff --git a/src/freecodecamp/daily/2025-11-20/main.ts b/src/freecodecamp/daily/2025-11-20/main.ts new file mode 100644 index 0000000..0a4ca85 --- /dev/null +++ b/src/freecodecamp/daily/2025-11-20/main.ts @@ -0,0 +1,23 @@ +/** + * @copyright NHCarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ + +/** + * Given a sentence string, return the longest word in the sentence. + * @param sentence - The sentence to find the longest word in. + * @returns The longest word in the sentence. + * @see https://www.freecodecamp.org/learn/daily-coding-challenge/2025-11-20 + */ +const longestWord = (sentence: string): string => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- We know the array will not be empty. + return sentence.split(" ").map((word) => { + return word.replaceAll(/[^A-Za-z]/g, ""); + }). + sort((a, b) => { + return b.length - a.length; + })[0]!; +}; + +export { longestWord };