diff --git a/lesson_07/conditionals/src/part_f.test.ts b/lesson_07/conditionals/src/part_f.test.ts new file mode 100644 index 000000000..bdee84727 --- /dev/null +++ b/lesson_07/conditionals/src/part_f.test.ts @@ -0,0 +1,79 @@ +import * as jest from "@jest/globals"; +import { countCharacter, getLetterGrade, sumEvenNumbers } from "./part_f.js"; + +const describe = + !process.env.HW_VERSION || process.env.HW_VERSION === "F" + ? jest.describe + : jest.describe.skip; + +describe("getLetterGrade", () => { + it("should return 'A' for scores 90-100", () => { + expect(getLetterGrade(95)).toBe("A"); + expect(getLetterGrade(90)).toBe("A"); + expect(getLetterGrade(100)).toBe("A"); + }); + + it("should return 'B' for scores 80-89", () => { + expect(getLetterGrade(85)).toBe("B"); + expect(getLetterGrade(80)).toBe("B"); + expect(getLetterGrade(89)).toBe("B"); + }); + + it("should return 'C' for scores 70-79", () => { + expect(getLetterGrade(75)).toBe("C"); + expect(getLetterGrade(70)).toBe("C"); + expect(getLetterGrade(79)).toBe("C"); + }); + + it("should return 'D' for scores 60-69", () => { + expect(getLetterGrade(65)).toBe("D"); + expect(getLetterGrade(60)).toBe("D"); + expect(getLetterGrade(69)).toBe("D"); + }); + + it("should return 'F' for scores below 60", () => { + expect(getLetterGrade(59)).toBe("F"); + expect(getLetterGrade(0)).toBe("F"); + expect(getLetterGrade(45)).toBe("F"); + }); +}); + +describe("sumEvenNumbers", () => { + it("should return 0 for an empty array", () => { + expect(sumEvenNumbers([])).toBe(0); + }); + + it("should return the sum of even numbers", () => { + expect(sumEvenNumbers([1, 2, 3, 4, 5, 6])).toBe(12); // 2 + 4 + 6 + expect(sumEvenNumbers([2, 4, 6, 8])).toBe(20); + }); + + it("should return 0 if no even numbers exist", () => { + expect(sumEvenNumbers([1, 3, 5, 7])).toBe(0); + }); + + it("should handle negative even numbers", () => { + expect(sumEvenNumbers([-2, -1, 0, 1, 2])).toBe(0); // -2 + 0 + 2 + }); +}); + +describe("countCharacter", () => { + it("should count occurrences of a character", () => { + expect(countCharacter("hello", "l")).toBe(2); + expect(countCharacter("hello", "o")).toBe(1); + expect(countCharacter("hello", "h")).toBe(1); + }); + + it("should return 0 if character is not found", () => { + expect(countCharacter("hello", "x")).toBe(0); + }); + + it("should handle empty string", () => { + expect(countCharacter("", "a")).toBe(0); + }); + + it("should be case sensitive", () => { + expect(countCharacter("Hello", "h")).toBe(0); + expect(countCharacter("Hello", "H")).toBe(1); + }); +}); diff --git a/lesson_07/conditionals/src/part_f.ts b/lesson_07/conditionals/src/part_f.ts new file mode 100644 index 000000000..fdd8cf052 --- /dev/null +++ b/lesson_07/conditionals/src/part_f.ts @@ -0,0 +1,31 @@ +/** + * Determines the grade letter based on a numeric score. + * A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: below 60 + * + * @param score + * @returns + */ +export function getLetterGrade(score: number): string { + return ""; +} + +/** + * Calculates the sum of all even numbers in the given array. + * + * @param numbers + * @returns + */ +export function sumEvenNumbers(numbers: number[]): number { + return 0; +} + +/** + * Counts how many times a specific character appears in a string. + * + * @param text + * @param character + * @returns + */ +export function countCharacter(text: string, character: string): number { + return 0; +} diff --git a/lesson_07/conditionals/src/part_g.test.ts b/lesson_07/conditionals/src/part_g.test.ts new file mode 100644 index 000000000..437b30d9f --- /dev/null +++ b/lesson_07/conditionals/src/part_g.test.ts @@ -0,0 +1,72 @@ +import * as jest from "@jest/globals"; +import { daysUntilBirthday, findLargestNumber, isPalindrome } from "./part_g.js"; + +const describe = + !process.env.HW_VERSION || process.env.HW_VERSION === "G" + ? jest.describe + : jest.describe.skip; + +describe("findLargestNumber", () => { + it("should find the largest number in an array", () => { + expect(findLargestNumber([1, 5, 3, 9, 2])).toBe(9); + expect(findLargestNumber([10, 20, 5, 30])).toBe(30); + }); + + it("should handle arrays with one element", () => { + expect(findLargestNumber([42])).toBe(42); + }); + + it("should handle negative numbers", () => { + expect(findLargestNumber([-1, -5, -3])).toBe(-1); + }); + + it("should handle arrays with duplicate largest values", () => { + expect(findLargestNumber([5, 5, 5])).toBe(5); + }); +}); + +describe("isPalindrome", () => { + it("should return true for palindromes", () => { + expect(isPalindrome("racecar")).toBe(true); + expect(isPalindrome("A man a plan a canal Panama")).toBe(true); + expect(isPalindrome("race a car")).toBe(false); + }); + + it("should ignore case", () => { + expect(isPalindrome("Racecar")).toBe(true); + expect(isPalindrome("RaceCar")).toBe(true); + }); + + it("should ignore spaces", () => { + expect(isPalindrome("race car")).toBe(false); + expect(isPalindrome("taco cat")).toBe(true); + }); + + it("should handle empty string", () => { + expect(isPalindrome("")).toBe(true); + }); + + it("should handle single character", () => { + expect(isPalindrome("a")).toBe(true); + }); +}); + +describe("daysUntilBirthday", () => { + it("should calculate days until birthday in same year", () => { + // Birthday is later in the same year + expect(daysUntilBirthday(3, 15, 6, 15)).toBe(92); // March 15 to June 15 (roughly) + }); + + it("should calculate days until birthday next year", () => { + // Birthday already passed this year + expect(daysUntilBirthday(6, 15, 3, 15)).toBe(273); // June 15 to March 15 next year (roughly) + }); + + it("should return 0 if today is the birthday", () => { + expect(daysUntilBirthday(5, 20, 5, 20)).toBe(0); + }); + + it("should handle birthdays tomorrow", () => { + expect(daysUntilBirthday(5, 20, 5, 21)).toBe(1); + }); +}); diff --git a/lesson_07/conditionals/src/part_g.ts b/lesson_07/conditionals/src/part_g.ts new file mode 100644 index 000000000..766376b61 --- /dev/null +++ b/lesson_07/conditionals/src/part_g.ts @@ -0,0 +1,40 @@ +/** + * Finds the largest number in an array. + * + * @param numbers + * @returns + */ +export function findLargestNumber(numbers: number[]): number { + return 0; +} + +/** + * Determines if a string is a palindrome (reads the same forwards and backwards). + * Ignore case and spaces. + * + * @param text + * @returns + */ +export function isPalindrome(text: string): boolean { + return false; +} + +/** + * Calculates the number of days until the next birthday. + * Assume currentMonth and currentDay represent today's date, + * and birthMonth and birthDay represent the birthday. + * + * @param currentMonth (1-12) + * @param currentDay (1-31) + * @param birthMonth (1-12) + * @param birthDay (1-31) + * @returns + */ +export function daysUntilBirthday( + currentMonth: number, + currentDay: number, + birthMonth: number, + birthDay: number +): number { + return 0; +} diff --git a/lesson_07/conditionals/src/part_h.test.ts b/lesson_07/conditionals/src/part_h.test.ts new file mode 100644 index 000000000..e7c5661ca --- /dev/null +++ b/lesson_07/conditionals/src/part_h.test.ts @@ -0,0 +1,96 @@ +import * as jest from "@jest/globals"; +import { convertTemperature, findGCD, generateMultiplicationTable } from "./part_h.js"; + +const describe = + !process.env.HW_VERSION || process.env.HW_VERSION === "H" + ? jest.describe + : jest.describe.skip; + +describe("generateMultiplicationTable", () => { + it("should generate multiplication table for a given number", () => { + const result = generateMultiplicationTable(3); + expect(result).toEqual([ + "3 x 1 = 3", + "3 x 2 = 6", + "3 x 3 = 9", + "3 x 4 = 12", + "3 x 5 = 15", + "3 x 6 = 18", + "3 x 7 = 21", + "3 x 8 = 24", + "3 x 9 = 27", + "3 x 10 = 30" + ]); + }); + + it("should handle multiplication table for 1", () => { + const result = generateMultiplicationTable(1); + expect(result[0]).toBe("1 x 1 = 1"); + expect(result[9]).toBe("1 x 10 = 10"); + expect(result.length).toBe(10); + }); +}); + +describe("findGCD", () => { + it("should find GCD of two positive numbers", () => { + expect(findGCD(12, 8)).toBe(4); + expect(findGCD(15, 25)).toBe(5); + expect(findGCD(17, 13)).toBe(1); + expect(findGCD(100, 75)).toBe(25); + }); + + it("should handle when one number is zero", () => { + expect(findGCD(5, 0)).toBe(5); + expect(findGCD(0, 7)).toBe(7); + }); + + it("should handle identical numbers", () => { + expect(findGCD(7, 7)).toBe(7); + expect(findGCD(20, 20)).toBe(20); + }); + + it("should handle when one number divides the other", () => { + expect(findGCD(20, 5)).toBe(5); + expect(findGCD(6, 18)).toBe(6); + }); + + it("should handle order independence", () => { + expect(findGCD(8, 12)).toBe(4); + expect(findGCD(12, 8)).toBe(4); + }); +}); + +describe("convertTemperature", () => { + it("should convert Celsius to Fahrenheit", () => { + expect(convertTemperature(0, "C", "F")).toBe(32); + expect(convertTemperature(100, "C", "F")).toBe(212); + expect(convertTemperature(25, "C", "F")).toBe(77); + }); + + it("should convert Fahrenheit to Celsius", () => { + expect(convertTemperature(32, "F", "C")).toBe(0); + expect(convertTemperature(212, "F", "C")).toBe(100); + }); + + it("should convert Celsius to Kelvin", () => { + expect(convertTemperature(0, "C", "K")).toBe(273.15); + expect(convertTemperature(25, "C", "K")).toBe(298.15); + }); + + it("should convert Kelvin to Celsius", () => { + expect(convertTemperature(273.15, "K", "C")).toBe(0); + expect(convertTemperature(298.15, "K", "C")).toBe(25); + }); + + it("should return same temperature for same scales", () => { + expect(convertTemperature(25, "C", "C")).toBe(25); + expect(convertTemperature(77, "F", "F")).toBe(77); + }); + + it("should handle complex conversions", () => { + // F to K: (77°F - 32) × 5/9 + 273.15 = 298.15K + expect(convertTemperature(77, "F", "K")).toBeCloseTo(298.15, 2); + // K to F: (298.15K - 273.15) × 9/5 + 32 = 77°F + expect(convertTemperature(298.15, "K", "F")).toBeCloseTo(77, 2); + }); +}); diff --git a/lesson_07/conditionals/src/part_h.ts b/lesson_07/conditionals/src/part_h.ts new file mode 100644 index 000000000..0d5db91c9 --- /dev/null +++ b/lesson_07/conditionals/src/part_h.ts @@ -0,0 +1,38 @@ +/** + * Generates a multiplication table for the given number up to 10. + * Returns an array of strings in the format "number x 1 = result". + * + * @param number + * @returns + */ +export function generateMultiplicationTable(number: number): string[] { + return []; +} + +/** + * Finds the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. + * + * @param a First number + * @param b Second number + * @returns The greatest common divisor of a and b + */ +export function findGCD(a: number, b: number): number { + return 0; +} + +/** + * Converts a temperature from one scale to another. + * fromScale and toScale can be "C" (Celsius), "F" (Fahrenheit), or "K" (Kelvin). + * + * @param temperature + * @param fromScale + * @param toScale + * @returns + */ +export function convertTemperature( + temperature: number, + fromScale: string, + toScale: string +): number { + return 0; +}