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
51 changes: 44 additions & 7 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number {
// if it is greater, and 0 if the strings are equal.
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

return 0;
if (distance < 0) {
return -1;
} else if (distance > 0) {
return 1;
} else {
return 0;
}
}

/**
Expand All @@ -24,7 +28,20 @@ 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 is not defined for negative numbers.
}
if (n === 0 || n === 1) {
return 1; // The factorial of 0 and 1 is 1.
}
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i; // This is a shortcut for result = result * i
}
return result; // Return the computed factorial.
// If n is less than 0, return 0.
// If n is 0 or 1, return 1.
// Otherwise, compute the factorial iteratively from 2 to n.
}

/**
Expand All @@ -34,7 +51,20 @@ 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];
}
const fibonacciNumbers = [1, 1]; // Start with the first two numbers
while (fibonacciNumbers.length < n) {
const lastIndex = fibonacciNumbers.length - 1;
const nextFibonacciNumber =
fibonacciNumbers[lastIndex] + fibonacciNumbers[lastIndex - 1];
fibonacciNumbers.push(nextFibonacciNumber);
}
return fibonacciNumbers;
}

/**
Expand All @@ -59,11 +89,18 @@ 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] === value) {
return pivotIndex;
} else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value);
} else {
return binarySearch(values, pivotIndex + 1, end, value);
}


// 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 the value is not found, return -1.
}
31 changes: 28 additions & 3 deletions lesson_07/conditionals/src/part_f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,38 @@
* @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 if (score < 60) {
return "F";
}
return "Invalid score"; // In case of an invalid score
}


/**
* Calculates the sum of all even numbers in the given array.
*
* @param numbers
* @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;
}


/**
* Counts how many times a specific character appears in a string.
*
Expand All @@ -27,5 +46,11 @@ export function sumEvenNumbers(numbers: number[]): number {
* @returns
*/
export function countCharacter(text: string, character: string): number {
return 0;
let count = 0;
for (let i = 0; i < text.length; i++) {
if (text[i] === character) {
count++;
}
}
return count;
}