From 468af2092991dea1465289fef849a5a157b55258 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 7 Jan 2025 02:32:20 -0800 Subject: [PATCH 1/4] Update: Created more tests for invalid values --- .../algo-testing/anagram/anagram.test.js | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js index ab45b9b1..e75980cb 100644 --- a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js +++ b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js @@ -1,20 +1,24 @@ -const anagram = require('./anagram'); +const anagram = require("./anagram"); -describe('Anagram', () => { - it('should be a function', () => { - expect(typeof anagram).toEqual('function'); +describe("Anagram", () => { + it("should be a function", () => { + expect(typeof anagram).toEqual("function"); }); - it('should return a boolean', () => { - expect(typeof anagram('ram', 'arm')).toEqual('boolean'); + it("should return a boolean", () => { + expect(typeof anagram("ram", "arm")).toEqual("boolean"); }); - it('should return true if anagram', () => { - expect(anagram('ram', 'arm')).toBeTruthy(); - expect(anagram('cinema', 'iceman')).toBeTruthy(); - expect(anagram('god', 'dog')).toBeTruthy(); + it("should return true if anagram", () => { + expect(anagram("ram", "arm")).toBeTruthy(); + expect(anagram("cinema", "iceman")).toBeTruthy(); + expect(anagram("god", "dog")).toBeTruthy(); }); - it('should return false if not anagram', () => { - expect(anagram('hello', 'fellow')).toBeFalsy(); - expect(anagram('world', 'twirl')).toBeFalsy(); - expect(anagram('cost', 'lost')).toBeFalsy(); + it("should return false if not anagram", () => { + expect(anagram("hello", "fellow")).toBeFalsy(); + expect(anagram("world", "twirl")).toBeFalsy(); + expect(anagram("cost", "lost")).toBeFalsy(); + expect(anagram("hello", "Hello")).toBeFalsy(); // same word + expect(anagram("", "")).toBeFalsy(); // empty strings + expect(anagram(" ", " ")).toBeFalsy(); // whitespace + expect(anagram("3rd!", "!dr3")).toBeFalsy(); // invalid characters }); }); From 73f7fb12125137010ec50186e866dfdf24ef2534 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 7 Jan 2025 02:35:58 -0800 Subject: [PATCH 2/4] Update: anagram function now prevents invalid values --- .../06-anagrams/algo-testing/anagram/anagram.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.js b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.js index 15dbcca7..ac26a850 100644 --- a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.js +++ b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.js @@ -1,4 +1,13 @@ function anagram(str1, str2) { + // Clean the string of spaces, numbers, & special characters + // Return false when both words are the same + const pattern = /^[^A-Za-z]+/g; + const cleanStr1 = str1.replaceAll(pattern, "").toLowerCase(); + const cleanStr2 = str2.replaceAll(pattern, "").toLowerCase(); + if (cleanStr1 === cleanStr2) { + return false; + } + const aCharMap = buildCharMap(str1); const bCharMap = buildCharMap(str2); From 7c6f1a38ed44af254ae3814d3911c242b6077866 Mon Sep 17 00:00:00 2001 From: fewdn <111025827+fewdn@users.noreply.github.com> Date: Mon, 20 Jan 2025 03:13:14 -0800 Subject: [PATCH 3/4] Update anagram.test.js Reverted to single quotes to match original --- .../algo-testing/anagram/anagram.test.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js index e75980cb..0e5c99b1 100644 --- a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js +++ b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js @@ -1,24 +1,24 @@ -const anagram = require("./anagram"); +const anagram = require('./anagram'); -describe("Anagram", () => { - it("should be a function", () => { - expect(typeof anagram).toEqual("function"); +describe('Anagram', () => { + it('should be a function', () => { + expect(typeof anagram).toEqual('function'); }); - it("should return a boolean", () => { - expect(typeof anagram("ram", "arm")).toEqual("boolean"); + it('should return a boolean', () => { + expect(typeof anagram('ram', 'arm')).toEqual('boolean'); }); it("should return true if anagram", () => { - expect(anagram("ram", "arm")).toBeTruthy(); - expect(anagram("cinema", "iceman")).toBeTruthy(); - expect(anagram("god", "dog")).toBeTruthy(); + expect(anagram('ram', 'arm')).toBeTruthy(); + expect(anagram('cinema', 'iceman')).toBeTruthy(); + expect(anagram('god', 'dog')).toBeTruthy(); }); it("should return false if not anagram", () => { - expect(anagram("hello", "fellow")).toBeFalsy(); - expect(anagram("world", "twirl")).toBeFalsy(); - expect(anagram("cost", "lost")).toBeFalsy(); - expect(anagram("hello", "Hello")).toBeFalsy(); // same word - expect(anagram("", "")).toBeFalsy(); // empty strings - expect(anagram(" ", " ")).toBeFalsy(); // whitespace - expect(anagram("3rd!", "!dr3")).toBeFalsy(); // invalid characters + expect(anagram('hello', 'fellow')).toBeFalsy(); + expect(anagram('world', 'twirl')).toBeFalsy(); + expect(anagram('cost', 'lost')).toBeFalsy(); + expect(anagram('hello', 'Hello')).toBeFalsy(); // same word + expect(anagram('', '')).toBeFalsy(); // empty strings + expect(anagram(' ', ' ')).toBeFalsy(); // whitespace + expect(anagram('3rd!', '!dr3')).toBeFalsy(); // invalid characters }); }); From 19c9af92eef0444eb053c182ffd51feb4993c5f8 Mon Sep 17 00:00:00 2001 From: fewdn <111025827+fewdn@users.noreply.github.com> Date: Mon, 20 Jan 2025 03:17:51 -0800 Subject: [PATCH 4/4] Update anagram.test.js --- .../06-anagrams/algo-testing/anagram/anagram.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js index 0e5c99b1..cc7039be 100644 --- a/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js +++ b/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.test.js @@ -7,12 +7,12 @@ describe('Anagram', () => { it('should return a boolean', () => { expect(typeof anagram('ram', 'arm')).toEqual('boolean'); }); - it("should return true if anagram", () => { + it('should return true if anagram', () => { expect(anagram('ram', 'arm')).toBeTruthy(); expect(anagram('cinema', 'iceman')).toBeTruthy(); expect(anagram('god', 'dog')).toBeTruthy(); }); - it("should return false if not anagram", () => { + it('should return false if not anagram', () => { expect(anagram('hello', 'fellow')).toBeFalsy(); expect(anagram('world', 'twirl')).toBeFalsy(); expect(anagram('cost', 'lost')).toBeFalsy();