From ffbdf89f2313dc1ba2b143c0029c6e0b3fd8ae14 Mon Sep 17 00:00:00 2001 From: Dadenaike251 Date: Fri, 21 Mar 2025 17:04:21 +0000 Subject: [PATCH 1/3] hw incomplete --- lesson_07/conditionals/src/lesson7.ts | 38 ++++++++++++++++++++++++--- lesson_07/conditionals/src/part_a.ts | 14 ++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..b9213950c 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,10 +11,14 @@ export function compareStrings(a: string, b: string): number { // 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); - + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } // TODO(you): Finish this method. - - return 0; } /** @@ -24,7 +28,16 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + return 0; + } + let numbers = 1; + + for (let i = 1; i < n; n--) { + numbers = numbers * n; + } + + return numbers; } /** @@ -34,6 +47,16 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { + if (n === 0) { + return []; + } + if (n === 1) { + return [1]; + } + const number: number[] = [1, 1]; + for (let i = 2; i < n; i++) { + number.push(number[i - 1] + number[i - 2]); + } return []; } @@ -65,5 +88,12 @@ export function binarySearch( // Else if values[pivotIndex] is greater than the value, then // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, start, pivotIndex + 1, end); + } return -1; } diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index 7ad571136..f28888804 100644 --- a/lesson_07/conditionals/src/part_a.ts +++ b/lesson_07/conditionals/src/part_a.ts @@ -5,6 +5,9 @@ * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { + if (age >= 18) { + return true; + } return false; } @@ -15,9 +18,16 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + if (values.length === 0) { + return 0; + } else { + let sum = 0; + for (let i = 0; i < values.length; i++) { + sum += values[i]; + } + return sum; + } } - /** * Computes the factorial of the given value of `n`. * From 4812604035bc930a1cbb1438dc5e56ba283f27a6 Mon Sep 17 00:00:00 2001 From: Dadenaike251 Date: Mon, 24 Mar 2025 15:50:17 +0000 Subject: [PATCH 2/3] fix: Completed homework & Passed all Tests --- lesson_07/conditionals/.env.test | 2 +- lesson_07/conditionals/src/lesson7.ts | 89 +++++++++++++++++++-------- lesson_07/conditionals/src/part_a.ts | 19 +++++- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..8532774b0 100644 --- a/lesson_07/conditionals/.env.test +++ b/lesson_07/conditionals/.env.test @@ -1 +1 @@ -HW_VERSION=your homework version here \ No newline at end of file +HW_VERSION=A \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index b9213950c..055cc5338 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -28,16 +28,28 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - if (n < 0) { + if (n === 0) { + return 1; + } + else if (n === 1) { + return 1; + } + else if (n < 0) { return 0; } - let numbers = 1; - - for (let i = 1; i < n; n--) { - numbers = numbers * n; + else { + return n * computeFactorial(n-1); } + // if (n < 0) { + // return 0; + // } + // let numbers = 1; - return numbers; + // for (let i = 1; i < n; n--) { + // numbers = numbers * n; + // } + + // return numbers; } /** @@ -47,17 +59,39 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - if (n === 0) { - return []; - } - if (n === 1) { - return [1]; - } - const number: number[] = [1, 1]; - for (let i = 2; i < n; i++) { - number.push(number[i - 1] + number[i - 2]); + + for (let i = 0; i < n; i++) { + if (n === 0) + return[]; + else if (n === 1) { + return [1]; + } + else if (n === 2) { + return[1,1]; + } + else { + const fib = getFirstNFibonacciNumbers(n-1); + fib.push(fib[fib.length - 1] + fib [fib.length -2]); + return fib; + } } - return []; + return[]; + +// if (n === 0) { +// return []; +// } +// else if(n === 1) { +// return [1]; +// } else if (n === 2) { +// return [2] +// } else { +// const number: number[] = [1, 1]; +// for (let i = 3; i < n; i++) { +// number.push(number[i - 1] + number[i - 2]);; + +// } +// return number; +// } } /** @@ -69,6 +103,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] { * @param value The value to look for. * @return The index of the value if found in the array and -1 otherwise. */ + export function binarySearch( values: number[], start: number, @@ -80,7 +115,17 @@ export function binarySearch( return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. //this is a variable "pivotIndex" that finds the middle values of our array + if (values[pivotIndex] === value) { + //this function checks if middle value index of our array is equal to the value in our array + return pivotIndex; // returns said value if it is + } else if (values[pivotIndex] > value) { + // checks if middle value is greater than our targett value, if it is than our target is in left half + return binarySearch(values, start, pivotIndex - 1, value); //calls the binarySearch function but range is narrowed down to left side from start to pivotIndex -1 + } else { + //if middle value is less than the target value then the target must be in the right half + return binarySearch(values, pivotIndex + 1, end, value); // calls function binarySearch again and limit range to right side from pivotIndex +1 to end + } // TODO(you): Finish implementing this algorithm @@ -88,12 +133,6 @@ export function binarySearch( // Else if values[pivotIndex] is greater than the value, then // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. - if (values[pivotIndex] === value) { - return pivotIndex; - } else if (values[pivotIndex] > value) { - return binarySearch(values, start, pivotIndex - 1, value); - } else { - return binarySearch(values, start, pivotIndex + 1, end); - } - return -1; + + } diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index f28888804..9963d0bbf 100644 --- a/lesson_07/conditionals/src/part_a.ts +++ b/lesson_07/conditionals/src/part_a.ts @@ -18,12 +18,17 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { + // const array1 = ["a", "b", "c"]; + + // for (const element of array1) { + // console.log(element); + // } if (values.length === 0) { return 0; } else { let sum = 0; - for (let i = 0; i < values.length; i++) { - sum += values[i]; + for (const value of values) { + sum += value; } return sum; } @@ -35,5 +40,13 @@ export function addNumbers(values: number[]): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + return 0; + } + let numbers = 1; + + for (let i = 1; i < n; n--) { + numbers = numbers * n; + } + return numbers; } From 07dcb87d09bdefaa41fdefc72c3822bf4d599fac Mon Sep 17 00:00:00 2001 From: Dadenaike251 Date: Wed, 26 Mar 2025 14:38:02 +0000 Subject: [PATCH 3/3] feat/David.A_lesson7hw --- lesson_07/conditionals/src/lesson7.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 055cc5338..10bd87612 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -40,16 +40,6 @@ export function computeFactorial(n: number): number { else { return n * computeFactorial(n-1); } - // if (n < 0) { - // return 0; - // } - // let numbers = 1; - - // for (let i = 1; i < n; n--) { - // numbers = numbers * n; - // } - - // return numbers; } /**