From e73585d0445f940116e2ffcc5de5513813ea0ea4 Mon Sep 17 00:00:00 2001 From: haldanek Date: Sat, 12 Oct 2024 03:42:58 +0000 Subject: [PATCH 1/6] feat: adds Kimberlee's lesson_07 conditionals and loops exercise --- lesson_07/conditionals/package-lock.json | 8 +- lesson_07/conditionals/package.json | 4 +- lesson_07/conditionals/src/lesson7.ts | 120 ++++++++++++++++++++--- lesson_07/conditionals/src/test.ts | 62 ++++++++++++ 4 files changed, 172 insertions(+), 22 deletions(-) create mode 100644 lesson_07/conditionals/src/test.ts diff --git a/lesson_07/conditionals/package-lock.json b/lesson_07/conditionals/package-lock.json index 6c2e02652..b91951020 100644 --- a/lesson_07/conditionals/package-lock.json +++ b/lesson_07/conditionals/package-lock.json @@ -21,7 +21,7 @@ "prettier": "3.3.3", "ts-jest": "^29.2.5", "ts-node": "^10.9.2", - "typescript": "^5.6.2", + "typescript": "^5.6.3", "typescript-eslint": "^8.7.0" } }, @@ -5335,9 +5335,9 @@ } }, "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", "dev": true, "license": "Apache-2.0", "bin": { diff --git a/lesson_07/conditionals/package.json b/lesson_07/conditionals/package.json index 6e09f98e0..1298e73da 100644 --- a/lesson_07/conditionals/package.json +++ b/lesson_07/conditionals/package.json @@ -28,7 +28,7 @@ "prettier": "3.3.3", "ts-jest": "^29.2.5", "ts-node": "^10.9.2", - "typescript": "^5.6.2", + "typescript": "^5.6.3", "typescript-eslint": "^8.7.0" } -} \ No newline at end of file +} diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..30ba80e73 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -1,16 +1,27 @@ import { computeLexicographicDistance } from "./util.js"; -/** +/** (Q1) * Returns true if the provided age meets the minimum US voting age and false otherwise. * * @param age The age to check. * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { - return false; + return age >= 18; } -/** +const age = 12; +let checkAge; + +if (canVote(age)) { + checkAge = true; +} else { + checkAge = false; +} + +console.log(checkAge); + +/** (Q2) * Compares two strings lexicographically. * * @param a The first `string` to compare. @@ -18,16 +29,38 @@ 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 { - // 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); + return distance; +} - // TODO(you): Finish this method. - - return 0; +export function computeLexicographicDistance(a: string, b: string): number { + //I literally used export function again because typescript kept telling me + // that my return statements needed to be inside the body of a function. + // I'm a bit skeptical because we are supposed to be DRY! and the only thing I + // changed was the function name. But it worked. I'm just leaving this note + // to remind myself to ask for clarification about this. + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } else { + return 0; + } } -/** +const result = compareStrings("Kimberlee", "haldane"); +console.log(result); + +//If I am understanding question 2 correctly, because the first function +// 'compareStrings' calls on a second function 'computeLexicographicDistance', +// I needed to set up both functions. The first function calls the second +// and the second function does the actual calculation and determines if each string +// is one of this set (-1, 1, 0). Testing is when the first function comes +//back into play, comparing two strings and returning a numerical result. Sorry +// for the long note, it took me a VERY long time to get to this understanding. +// I just hope I am right. + +/** (Q3) * Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board * scale. See * https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale @@ -36,31 +69,86 @@ export function compareStrings(a: string, b: string): number { * @param gpa The GPA value. * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ +export function myGrade(grade: number): string { + const gpa = convertGpaToLetterGrade(grade); + return gpa; +} + +//I followed the same logic as question 2. I can already tell that +// there may be many simpler ways to build this function. I am just +// happy it came out with no errors and will make a note to play around +// with this and see if I can't get it to look better (simpler/cleaner). + export function convertGpaToLetterGrade(gpa: number): string { - return "F"; + if (gpa < 65) { + return "F"; + } else if (gpa < 67 && gpa >= 65) { + return "D"; + } else if (gpa < 70 && gpa >= 67) { + return "D+"; + } else if (gpa < 73 && gpa >= 70) { + return "C-"; + } else if (gpa < 77 && gpa >= 73) { + return "C"; + } else if (gpa < 80 && gpa >= 77) { + return "C+"; + } else if (gpa < 83 && gpa >= 80) { + return "B-"; + } else if (gpa < 87 && gpa >= 83) { + return "B"; + } else if (gpa < 90 && gpa >= 87) { + return "B+"; + } else if (gpa < 93 && gpa >= 90) { + return "A-"; + } else if (gpa < 97 && gpa >= 93) { + return "A"; + } else if (gpa <= 100 && gpa >= 97) { + return "A+"; + } else { + return "unable to assess"; + } } -/** +const grade = convertGpaToLetterGrade(32); +console.log(grade); + +/** (Q4) * Computes the factorial of the given value of `n`. * * @param n The value for which to compute the factorial. * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let result = 1; + for (let i = 1; i <= n; i++) { // I used i+1 and fell into an infinite loop. My bad! + result *= i; + } + return result; } +// Steps I followed: Describe the function, Declare the function, crawl inside the function +const n = 7; +console.log(computeFactorial(n)); + -/** +/** (Q5) * Adds all of the provided values and returns the sum. * * @param values The values to sum. * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let total = 0; + for (const value of values) { + total += value; + } + return total; } -/** +const numbers = [8, 4, 6, 2, 7]; +const sum = addNumbers(numbers); +console.log(sum); + +/** (Q6) * Returns an array of the first `n` Fibonacci numbers starting from 1. * * @param n The first `n` of Fibonacci values to compute. @@ -70,7 +158,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] { return []; } -/** +/** (Q7) * Finds a value in an array of values. * * @param values The values to search. diff --git a/lesson_07/conditionals/src/test.ts b/lesson_07/conditionals/src/test.ts new file mode 100644 index 000000000..8ef8a942e --- /dev/null +++ b/lesson_07/conditionals/src/test.ts @@ -0,0 +1,62 @@ +import { computeLexicographicDistance } from "./util.js"; + +/** + * Returns true if the provided age meets the minimum US voting age and false otherwise. + * + * @param age The age to check. + * @return True if the age corresponds to a voting age and false otherwise. + */ +export function canVote(age: number): boolean { + return age >= 18; +} + +const age = 8; +let voTing; + +if (canVote(age)) { + voTing = true; +} else { + voTing = false; +} + +console.log(voTing); + +/** + * Compares two strings lexicographically. + * + * @param a The first `string` to compare. + * @param b The second `string` to compare. + * @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 { + // 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); + const distance: number + return distance; +} + if (distance < 0) { + console.log(-1); + } else if (distance > 0) { + console.log(1); + } else { + console.log(0); + } + + const result= compareStrings("Kimberlee", "haldane"); + console.log(result); + + + // TODO(you): Finish this method. + + ** + * Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board + * scale. See + * https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale + * for details. + * + * @param gpa The GPA value. + * @return The letter grade ("A+", "A", "A-", "B+", etc.). + */ +export function convertGpaToLetterGrade(gpa: number): string { + return "F"; \ No newline at end of file From b0245536f5d3ee840bfc0b0195cbe58e1c8ae524 Mon Sep 17 00:00:00 2001 From: haldanek Date: Sat, 12 Oct 2024 22:44:21 +0000 Subject: [PATCH 2/6] feat: adds Kimberlee's lesson_07 conditionals and loops --- lesson_07/conditionals/src/lesson7.ts | 88 +++++++++++++++++---------- lesson_07/conditionals/src/test.ts | 62 ------------------- 2 files changed, 55 insertions(+), 95 deletions(-) delete mode 100644 lesson_07/conditionals/src/test.ts diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 30ba80e73..7b6795bcb 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -80,30 +80,30 @@ export function myGrade(grade: number): string { // with this and see if I can't get it to look better (simpler/cleaner). export function convertGpaToLetterGrade(gpa: number): string { - if (gpa < 65) { - return "F"; - } else if (gpa < 67 && gpa >= 65) { - return "D"; - } else if (gpa < 70 && gpa >= 67) { - return "D+"; - } else if (gpa < 73 && gpa >= 70) { - return "C-"; - } else if (gpa < 77 && gpa >= 73) { - return "C"; - } else if (gpa < 80 && gpa >= 77) { - return "C+"; - } else if (gpa < 83 && gpa >= 80) { - return "B-"; - } else if (gpa < 87 && gpa >= 83) { - return "B"; - } else if (gpa < 90 && gpa >= 87) { - return "B+"; - } else if (gpa < 93 && gpa >= 90) { - return "A-"; - } else if (gpa < 97 && gpa >= 93) { - return "A"; - } else if (gpa <= 100 && gpa >= 97) { + if (gpa >= 4.0) { return "A+"; + } else if (gpa > 3.7 && gpa < 4.0) { + return "A"; + } else if (gpa > 3.3 && gpa <= 3.7) { + return "A-"; + } else if (gpa > 3.0 && gpa <= 3.3) { + return "B+"; + } else if (gpa > 2.7 && gpa <= 3.0) { + return "B"; + } else if (gpa > 2.3 && gpa <= 2.7) { + return "B-"; + } else if (gpa > 2.0 && gpa <= 2.3) { + return "C+"; + } else if (gpa > 1.7 && gpa <= 2.0) { + return "C"; + } else if (gpa > 1.3 && gpa <= 1.7) { + return "C-"; + } else if (gpa > 1.0 && gpa <= 1.3) { + return "D+"; + } else if (gpa > 0.0 && gpa <= 1.0) { + return "D"; + } else if (gpa < 0.0) { + return "F"; } else { return "unable to assess"; } @@ -120,7 +120,8 @@ console.log(grade); */ export function computeFactorial(n: number): number { let result = 1; - for (let i = 1; i <= n; i++) { // I used i+1 and fell into an infinite loop. My bad! + for (let i = 1; i <= n; i++) { + // I used i+1 and fell into an infinite loop. My bad! result *= i; } return result; @@ -129,7 +130,6 @@ export function computeFactorial(n: number): number { const n = 7; console.log(computeFactorial(n)); - /** (Q5) * Adds all of the provided values and returns the sum. * @@ -154,10 +154,32 @@ console.log(sum); * @param n The first `n` of Fibonacci values to compute. * @return An array containing the first `n` Fibonacci values. */ +//This question nearly had me in tears and I'm sad to say that in the end +// I had to seek help. However, I think if I can explain what I learned as +// I go, that help would not have been in vain. Again, pardon the long notes. export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const myArray: number[] = []; // First: initiatialize an empty array inside the function + // Next:Take care of base case since the first two Fibonacci numbers are 1, 1. + // If n is 1, return an array with the first number. If n is 2, return an array with + // the first two numbers. This step is crucial for ensuring the loop works properly. + if (n <= 0) return []; + if (n === 1) return [1]; + if (n === 2) return [1, 1]; + myArray.push(1, 1); + + for (let i = 2; i < n; i++) { + //initialization(stats at 2 because I added the first two numbers in the sequence above) + // so this tells the function to start computing from the 3rd number. Condition: this tells + // the function that it can only work with numbers less than the given number. + // Increment: this tells the function to move on to the next number by 1 (no infinite loops). + const nextNum = myArray[i - 1] + myArray[i - 2]; + myArray.push(nextNum); // adds the new number to the array to continue the loop + } + return myArray; } +console.log(getFirstNFibonacciNumbers(7)); + /** (Q7) * Finds a value in an array of values. * @@ -180,11 +202,11 @@ export function binarySearch( const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // TODO(you): Finish implementing this algorithm - - // If values[pivotIndex] is equal to value then return `pivotIndex`. - // 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. - return -1; + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } + return binarySearch(values, pivotIndex + 1, end, value); } +// This problem was much easier to write out because of question 6. diff --git a/lesson_07/conditionals/src/test.ts b/lesson_07/conditionals/src/test.ts deleted file mode 100644 index 8ef8a942e..000000000 --- a/lesson_07/conditionals/src/test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { computeLexicographicDistance } from "./util.js"; - -/** - * Returns true if the provided age meets the minimum US voting age and false otherwise. - * - * @param age The age to check. - * @return True if the age corresponds to a voting age and false otherwise. - */ -export function canVote(age: number): boolean { - return age >= 18; -} - -const age = 8; -let voTing; - -if (canVote(age)) { - voTing = true; -} else { - voTing = false; -} - -console.log(voTing); - -/** - * Compares two strings lexicographically. - * - * @param a The first `string` to compare. - * @param b The second `string` to compare. - * @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 { - // 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); - const distance: number - return distance; -} - if (distance < 0) { - console.log(-1); - } else if (distance > 0) { - console.log(1); - } else { - console.log(0); - } - - const result= compareStrings("Kimberlee", "haldane"); - console.log(result); - - - // TODO(you): Finish this method. - - ** - * Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board - * scale. See - * https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale - * for details. - * - * @param gpa The GPA value. - * @return The letter grade ("A+", "A", "A-", "B+", etc.). - */ -export function convertGpaToLetterGrade(gpa: number): string { - return "F"; \ No newline at end of file From 6e4e004453821eafde6def819acc93be3f597665 Mon Sep 17 00:00:00 2001 From: haldanek Date: Sun, 13 Oct 2024 00:00:29 +0000 Subject: [PATCH 3/6] feat: adds Kimberlee's lesson_07 conditionals & loops --- lesson_07/conditionals/src/lesson7.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 7b6795bcb..ca1875faf 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -1,4 +1,4 @@ -import { computeLexicographicDistance } from "./util.js"; +//import { computeLexicographicDistance } from "./util.js"; /** (Q1) * Returns true if the provided age meets the minimum US voting age and false otherwise. @@ -81,31 +81,29 @@ export function myGrade(grade: number): string { export function convertGpaToLetterGrade(gpa: number): string { if (gpa >= 4.0) { - return "A+"; - } else if (gpa > 3.7 && gpa < 4.0) { return "A"; - } else if (gpa > 3.3 && gpa <= 3.7) { + } else if (gpa >= 3.7) { return "A-"; - } else if (gpa > 3.0 && gpa <= 3.3) { + } else if (gpa >= 3.3) { return "B+"; - } else if (gpa > 2.7 && gpa <= 3.0) { + } else if (gpa >= 3.0) { return "B"; - } else if (gpa > 2.3 && gpa <= 2.7) { + } else if (gpa >= 2.7) { return "B-"; - } else if (gpa > 2.0 && gpa <= 2.3) { + } else if (gpa >= 2.3) { return "C+"; - } else if (gpa > 1.7 && gpa <= 2.0) { + } else if (gpa >= 2.0) { return "C"; - } else if (gpa > 1.3 && gpa <= 1.7) { + } else if (gpa >= 1.7) { return "C-"; - } else if (gpa > 1.0 && gpa <= 1.3) { + } else if (gpa >= 1.3) { return "D+"; - } else if (gpa > 0.0 && gpa <= 1.0) { + } else if (gpa >= 1.0) { return "D"; - } else if (gpa < 0.0) { + } else if (gpa < 1.0 && gpa >= 0) { return "F"; } else { - return "unable to assess"; + return "Invalid"; } } From e3bdffd69f71ce1798196ca9567162a0c8785520 Mon Sep 17 00:00:00 2001 From: haldanek Date: Mon, 14 Oct 2024 16:59:20 +0000 Subject: [PATCH 4/6] feat: adds Kimberlee's lesson_07 conditionals --- lesson_07/conditionals/src/lesson7.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index ca1875faf..031d9f9c9 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -74,10 +74,7 @@ export function myGrade(grade: number): string { return gpa; } -//I followed the same logic as question 2. I can already tell that -// there may be many simpler ways to build this function. I am just -// happy it came out with no errors and will make a note to play around -// with this and see if I can't get it to look better (simpler/cleaner). +//I followed the same logic as question 2. export function convertGpaToLetterGrade(gpa: number): string { if (gpa >= 4.0) { @@ -107,7 +104,7 @@ export function convertGpaToLetterGrade(gpa: number): string { } } -const grade = convertGpaToLetterGrade(32); +const grade = convertGpaToLetterGrade(3.2); console.log(grade); /** (Q4) @@ -166,7 +163,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] { myArray.push(1, 1); for (let i = 2; i < n; i++) { - //initialization(stats at 2 because I added the first two numbers in the sequence above) + //initialization(starts at 2 because I added the first two numbers in the sequence above) // so this tells the function to start computing from the 3rd number. Condition: this tells // the function that it can only work with numbers less than the given number. // Increment: this tells the function to move on to the next number by 1 (no infinite loops). @@ -208,3 +205,6 @@ export function binarySearch( return binarySearch(values, pivotIndex + 1, end, value); } // This problem was much easier to write out because of question 6. +const values = [ 2, 4, 6, 8, 10, 12, 14, 16]; +const index = binarySearch(values, 4, 8, 14); +console.log(index); \ No newline at end of file From e5488bb086faa53ca5150483b6acebb3599f56ef Mon Sep 17 00:00:00 2001 From: haldanek Date: Mon, 14 Oct 2024 17:27:13 +0000 Subject: [PATCH 5/6] chore: cleaned up notes for Kimberlee's lesson_07 conditionals --- lesson_07/conditionals/src/lesson7.ts | 42 +++++++-------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 031d9f9c9..079134157 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -34,11 +34,7 @@ export function compareStrings(a: string, b: string): number { } export function computeLexicographicDistance(a: string, b: string): number { - //I literally used export function again because typescript kept telling me - // that my return statements needed to be inside the body of a function. - // I'm a bit skeptical because we are supposed to be DRY! and the only thing I - // changed was the function name. But it worked. I'm just leaving this note - // to remind myself to ask for clarification about this. + if (a < b) { return -1; } else if (a > b) { @@ -51,14 +47,7 @@ export function computeLexicographicDistance(a: string, b: string): number { const result = compareStrings("Kimberlee", "haldane"); console.log(result); -//If I am understanding question 2 correctly, because the first function -// 'compareStrings' calls on a second function 'computeLexicographicDistance', -// I needed to set up both functions. The first function calls the second -// and the second function does the actual calculation and determines if each string -// is one of this set (-1, 1, 0). Testing is when the first function comes -//back into play, comparing two strings and returning a numerical result. Sorry -// for the long note, it took me a VERY long time to get to this understanding. -// I just hope I am right. + /** (Q3) * Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board @@ -74,7 +63,6 @@ export function myGrade(grade: number): string { return gpa; } -//I followed the same logic as question 2. export function convertGpaToLetterGrade(gpa: number): string { if (gpa >= 4.0) { @@ -116,12 +104,12 @@ console.log(grade); export function computeFactorial(n: number): number { let result = 1; for (let i = 1; i <= n; i++) { - // I used i+1 and fell into an infinite loop. My bad! + result *= i; } return result; } -// Steps I followed: Describe the function, Declare the function, crawl inside the function + const n = 7; console.log(computeFactorial(n)); @@ -149,26 +137,18 @@ console.log(sum); * @param n The first `n` of Fibonacci values to compute. * @return An array containing the first `n` Fibonacci values. */ -//This question nearly had me in tears and I'm sad to say that in the end -// I had to seek help. However, I think if I can explain what I learned as -// I go, that help would not have been in vain. Again, pardon the long notes. + export function getFirstNFibonacciNumbers(n: number): number[] { - const myArray: number[] = []; // First: initiatialize an empty array inside the function - // Next:Take care of base case since the first two Fibonacci numbers are 1, 1. - // If n is 1, return an array with the first number. If n is 2, return an array with - // the first two numbers. This step is crucial for ensuring the loop works properly. + const myArray: number[] = []; if (n <= 0) return []; if (n === 1) return [1]; if (n === 2) return [1, 1]; myArray.push(1, 1); for (let i = 2; i < n; i++) { - //initialization(starts at 2 because I added the first two numbers in the sequence above) - // so this tells the function to start computing from the 3rd number. Condition: this tells - // the function that it can only work with numbers less than the given number. - // Increment: this tells the function to move on to the next number by 1 (no infinite loops). + const nextNum = myArray[i - 1] + myArray[i - 2]; - myArray.push(nextNum); // adds the new number to the array to continue the loop + myArray.push(nextNum); } return myArray; } @@ -191,11 +171,11 @@ export function binarySearch( value: number, ): number { if (end < start) { - // The range is not valid so just return -1. + return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + const pivotIndex = Math.floor((start + end) / 2); if (values[pivotIndex] === value) { return pivotIndex; @@ -204,7 +184,7 @@ export function binarySearch( } return binarySearch(values, pivotIndex + 1, end, value); } -// This problem was much easier to write out because of question 6. + const values = [ 2, 4, 6, 8, 10, 12, 14, 16]; const index = binarySearch(values, 4, 8, 14); console.log(index); \ No newline at end of file From d8933618b3097752d5d930a82f29ed4edb2eea68 Mon Sep 17 00:00:00 2001 From: haldanek Date: Mon, 14 Oct 2024 18:35:51 +0000 Subject: [PATCH 6/6] chore: fixed notes for Kimberlee's lesson_07 conditionals- added source --- lesson_07/conditionals/src/lesson7.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 079134157..b8b436800 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -137,7 +137,7 @@ console.log(sum); * @param n The first `n` of Fibonacci values to compute. * @return An array containing the first `n` Fibonacci values. */ - +//** I had to look up how to complete this problem on google export function getFirstNFibonacciNumbers(n: number): number[] { const myArray: number[] = []; if (n <= 0) return [];