feat: today's freecodecamp challenge
Node.js CI / Lint and Test (push) Successful in 39s

This commit is contained in:
2025-11-20 08:31:50 -08:00
parent 1f44f2ddd9
commit 12a3ee7c21
2 changed files with 46 additions and 0 deletions
@@ -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");
});
});
+23
View File
@@ -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 };