From 7f5583d37c3b0a041c4255f963a2addbf51ed83d Mon Sep 17 00:00:00 2001 From: Dasiame Date: Fri, 11 Oct 2024 15:44:39 +0000 Subject: [PATCH 1/4] working on lesson_07 --- lesson_07/conditionals/src/lesson7.ts | 51 +++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..2dfee04e7 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,7 +7,11 @@ import { computeLexicographicDistance } from "./util.js"; * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { - return false; + if (age >= 18) { + return true; + } else { + return false; + } } /** @@ -18,13 +22,19 @@ export function canVote(age: number): boolean { * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. */ export function compareStrings(a: string, b: string): number { + const distance = computeLexicographicDistance(a, b); + + if (a > b) { + return -1; + } else if (b > a) { + return 1; + } else { + return 0; + } // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 // if it is greater, and 0 if the strings are equal. - const distance = computeLexicographicDistance(a, b); // TODO(you): Finish this method. - - return 0; } /** @@ -37,7 +47,31 @@ export function compareStrings(a: string, b: string): number { * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ export function convertGpaToLetterGrade(gpa: number): string { - return "F"; + if (gpa < 0 || gpa > 4.0) { + return "Invaild GPA"; + } else if (gpa <= 0.9) { + return "F"; + } else if (gpa < 1.25 && gpa >= 1.0) { + return "D"; + } else if (gpa < 1.75 && gpa >= 1.25) { + return "D+"; + } else if (gpa < 2.0 && gpa >= 1.75) { + return "C-"; + } else if (gpa < 2.3 && gpa >= 2.0) { + return "C"; + } else if (gpa < 2.7 && gpa >= 2.3) { + return "C+"; + } else if (gpa < 3.0 && gpa >= 2.7) { + return "B-"; + } else if (gpa < 3.3 && gpa >= 3.0) { + return "B"; + } else if (gpa < 3.7 && gpa >= 3.3) { + return "B+"; + } else if (gpa < 4.0 && gpa >= 3.7) { + return "A"; + } else { + return "A+"; + } } /** @@ -47,9 +81,12 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let sum = 1; + for (let i = 1; 1<=n; i++) { + sum = sum *= i; + } + return sum; } - /** * Adds all of the provided values and returns the sum. * From b3957f4d74949a13940574081d405828dc7fe8e0 Mon Sep 17 00:00:00 2001 From: Dasiame Date: Sun, 13 Oct 2024 20:59:06 +0000 Subject: [PATCH 2/4] feat: adding Dasia's homework answers --- lesson_07/conditionals/src/lesson7.ts | 44 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 2dfee04e7..a4ddea21e 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -24,13 +24,11 @@ export function canVote(age: number): boolean { export function compareStrings(a: string, b: string): number { const distance = computeLexicographicDistance(a, b); - if (a > b) { + if (distance < 0) { return -1; - } else if (b > a) { - return 1; - } else { - return 0; } + + return distance; // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 // if it is greater, and 0 if the strings are equal. @@ -49,13 +47,13 @@ export function compareStrings(a: string, b: string): number { export function convertGpaToLetterGrade(gpa: number): string { if (gpa < 0 || gpa > 4.0) { return "Invaild GPA"; - } else if (gpa <= 0.9) { + } else if (gpa < 1) { return "F"; - } else if (gpa < 1.25 && gpa >= 1.0) { + } else if (gpa < 1.3 && gpa >= 1.0) { return "D"; - } else if (gpa < 1.75 && gpa >= 1.25) { + } else if (gpa < 1.7 && gpa >= 1.3) { return "D+"; - } else if (gpa < 2.0 && gpa >= 1.75) { + } else if (gpa < 2.0 && gpa >= 1.7) { return "C-"; } else if (gpa < 2.3 && gpa >= 2.0) { return "C"; @@ -68,10 +66,9 @@ export function convertGpaToLetterGrade(gpa: number): string { } else if (gpa < 3.7 && gpa >= 3.3) { return "B+"; } else if (gpa < 4.0 && gpa >= 3.7) { - return "A"; - } else { - return "A+"; + return "A-"; } + return "A"; } /** @@ -82,7 +79,7 @@ export function convertGpaToLetterGrade(gpa: number): string { */ export function computeFactorial(n: number): number { let sum = 1; - for (let i = 1; 1<=n; i++) { + for (let i = 1; i <= n; i++) { sum = sum *= i; } return sum; @@ -94,7 +91,11 @@ export function computeFactorial(n: number): number { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let sum = 0; + for (let i = 0; i < values.length; i++) { + sum = sum + values[i]; + } + return sum; } /** @@ -104,7 +105,20 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + if (n === 0) { + return []; + } else if (n === 1) { + return [1]; + } else if (n === 2) { + return [1, 1]; + } + const fibArray = [1, 1]; + + for (let i = 2; i < n; i++) { + const nextFibNumber = fibArray[i - 1] + fibArray[i - 2]; + fibArray.push(nextFibNumber); + } + return fibArray; } /** From 0b31f2679344ddb258569d22f1c93d1425974452 Mon Sep 17 00:00:00 2001 From: Dasiame Date: Mon, 14 Oct 2024 12:45:11 +0000 Subject: [PATCH 3/4] feat: adding Dasias homework answers --- lesson_07/conditionals/src/lesson7.ts | 29 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index a4ddea21e..314c195fd 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -105,20 +105,18 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - if (n === 0) { - return []; - } else if (n === 1) { - return [1]; - } else if (n === 2) { - return [1, 1]; - } - const fibArray = [1, 1]; + let current = 1; + let prev = 0; + + const numbers = []; - for (let i = 2; i < n; i++) { - const nextFibNumber = fibArray[i - 1] + fibArray[i - 2]; - fibArray.push(nextFibNumber); + for (let i = 1; i <= n; i++) { + numbers.push(current); + const nextNumber = current + prev; + prev = current; + current = nextNumber; } - return fibArray; + return numbers; } /** @@ -142,6 +140,13 @@ export function binarySearch( } const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, pivotIndex + 1, end, value); + } // TODO(you): Finish implementing this algorithm From e032ba52a5189d6da39db9265a2eeb131984f88f Mon Sep 17 00:00:00 2001 From: Dasiame Date: Mon, 14 Oct 2024 13:13:16 +0000 Subject: [PATCH 4/4] fix: added a for of loop --- lesson_07/conditionals/src/lesson7.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 314c195fd..747669fac 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -92,8 +92,8 @@ export function computeFactorial(n: number): number { */ export function addNumbers(values: number[]): number { let sum = 0; - for (let i = 0; i < values.length; i++) { - sum = sum + values[i]; + for (const i of values) { + sum += i; } return sum; }