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
50 changes: 43 additions & 7 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export function compareStrings(a: string, b: string): number {

// TODO(you): Finish this method.

return 0;
if (distance > 0) {
return 1;
} else if (distance < 0) {
return -1; // Or any negative number as per the comment
} else {
return 0;
}
}

/**
Expand All @@ -24,7 +30,18 @@ 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;
}
if (n === 0) {
return 1;
} else {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}

/**
Expand All @@ -34,7 +51,17 @@ 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 [];
} else if (n === 1) {
return [1];
} else {
const fib: number[] = [1, 1];
while (fib.length < n) {
fib.push(fib[fib.length - 1] + fib[fib.length - 2]);
}
return fib.slice(0, n);
}
}

/**
Expand All @@ -59,11 +86,20 @@ export function binarySearch(

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.

// TODO(you): Finish implementing this algorithm
// 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.
if (values[pivotIndex] === value) {
return pivotIndex;

// Else if values[pivotIndex] is greater than the value, then
} else if (values[pivotIndex] > value) {
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
return binarySearch(values, start, pivotIndex - 1, value);
} else {
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
return binarySearch(values, pivotIndex + 1, end, value);
}
// If the value is not found, return -1.
return -1;
}
31 changes: 29 additions & 2 deletions lesson_07/conditionals/src/part_f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
* @returns
*/
export function getLetterGrade(score: number): string {

if (score >= 90 && score <= 100) {
return "A";
} else if (score >= 80 && score < 90) {
return "B";
} else if (score >= 70 && score < 80) {
return "C";
} else if (score >= 60 && score < 70) {
return "D";
} else if (score < 60) {
return "F";
}
if (score < 0 || score > 100) {
return "Invalid score";
}
return "";
}

Expand All @@ -16,7 +31,13 @@ export function getLetterGrade(score: number): string {
* @returns
*/
export function sumEvenNumbers(numbers: number[]): number {
return 0;
let sum = 0;
for (const num of numbers) {
if (num % 2 === 0) {
sum += num;
}
}
return sum;
}

/**
Expand All @@ -27,5 +48,11 @@ 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++;
}
}
return count;
}