Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lesson_07/conditionals/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your homework version here
HW_VERSION=F
53 changes: 44 additions & 9 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ export function compareStrings(a: string, b: string): number {
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

return 0;
if (distance < 0) {
// a is less than b
return -1;
} else if (distance > 0) {
// a is greater than b
return 1;
} else {
// a is equal to b
return 0;
}
}

/**
Expand All @@ -24,7 +32,19 @@ 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; // Factorial of negative numbers is undefined, return 0
}

if (n === 0 || n === 1) {
return 1; // Factorial of 0 and 1 is 1
}

let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}

/**
Expand All @@ -34,7 +54,18 @@ export function computeFactorial(n: number): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
if (n <= 0) {
return [];
}
if (n === 1) {
return [1]; // First Fibonacci number is 1
}
const fibonacci = [1, 1]; // Start with first two Fibonacci numbers
for (let i = 2; i < n; i++) {
// Each new number is the sum of the previous two
fibonacci.push(fibonacci[i - 1] + fibonacci[i - 2]);
}
return fibonacci;
}

/**
Expand All @@ -61,9 +92,13 @@ export function binarySearch(

// 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) {
// If values[pivotIndex] is equal to value then return `pivotIndex`.
return pivotIndex;
} else if (values[pivotIndex] > value) {
// Else if values[pivotIndex] is greater than the value, then
return binarySearch(values, start, pivotIndex - 1, value); // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
} else {
return binarySearch(values, pivotIndex + 1, end, value); // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
}
}
33 changes: 30 additions & 3 deletions lesson_07/conditionals/src/part_f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
* @param score
* @returns
*/

export function getLetterGrade(score: number): string {
return "";
if (score >= 90 && score <= 100) {
return "A";
} else if (score >= 80 && score <= 89) {
return "B";
} else if (score >= 70 && score <= 79) {
return "C";
} else if (score >= 60 && score <= 69) {
return "D";
} else {
return "F";
}
}

/**
Expand All @@ -16,7 +27,14 @@ export function getLetterGrade(score: number): string {
* @returns
*/
export function sumEvenNumbers(numbers: number[]): number {
return 0;
let sum = 0;

for (const number of numbers) {
if (number % 2 === 0) {
sum += number;
}
}
return sum;
}

/**
Expand All @@ -27,5 +45,14 @@ export function sumEvenNumbers(numbers: number[]): number {
* @returns
*/
export function countCharacter(text: string, character: string): number {
return 0;
let count = 0;

for (const char of text) {
if (char === character) {
count++; // Increment count if character matches
}
}
return count;
}