generated from nhcarrigan/template
This commit is contained in:
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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 };
|
||||
Reference in New Issue
Block a user