Skip to content

Commit e452522

Browse files
committed
fix: added lesson7 answers for Hummad
1 parent 011795f commit e452522

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { computeLexicographicDistance } from "./util.js";
77
* @return True if the age corresponds to a voting age and false otherwise.
88
*/
99
export function canVote(age: number): boolean {
10-
return false;
10+
if (age < 18) return false;
11+
else return true;
1112
}
1213

1314
/**
@@ -17,14 +18,14 @@ export function canVote(age: number): boolean {
1718
* @param b The second `string` to compare.
1819
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
1920
*/
20-
export function compareStrings(a: string, b: string): number {
21+
export function compareStrings(a: string, b: string): number{
2122
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2223
// if it is greater, and 0 if the strings are equal.
2324
const distance = computeLexicographicDistance(a, b);
24-
25+
if (distance < 0) return -1;
26+
else if (distance > 0) return 1;
27+
else return 0;
2528
// TODO(you): Finish this method.
26-
27-
return 0;
2829
}
2930

3031
/**
@@ -37,7 +38,27 @@ export function compareStrings(a: string, b: string): number {
3738
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3839
*/
3940
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
41+
if (gpa >= 4.0) {
42+
return "A";
43+
} else if (gpa >= 3.7) {
44+
return "A-";
45+
} else if (gpa >= 3.3) {
46+
return "B+";
47+
} else if (gpa >= 3.0) {
48+
return "B";
49+
} else if (gpa >= 2.7) {
50+
return "B-";
51+
} else if (gpa >= 2.3) {
52+
return "C+";
53+
} else if (gpa >= 2.0) {
54+
return "C";
55+
} else if (gpa >= 1.7) {
56+
return "C-";
57+
} else if (gpa >= 1.3) {
58+
return "D+";
59+
} else if (gpa >= 1.0) {
60+
return "D";
61+
} else return "F";
4162
}
4263

4364
/**
@@ -47,7 +68,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
4768
* @return The factorial of n.
4869
*/
4970
export function computeFactorial(n: number): number {
50-
return 0;
71+
let result = 1;
72+
for (let i = 1; i <= n; i++) {
73+
result *= i;
74+
}
75+
76+
return result;
5177
}
5278

5379
/**
@@ -56,8 +82,13 @@ export function computeFactorial(n: number): number {
5682
* @param values The values to sum.
5783
* @return The sum of all the values.
5884
*/
85+
5986
export function addNumbers(values: number[]): number {
60-
return 0;
87+
let sum = 0;
88+
for (const value of values) {
89+
sum = sum + value;
90+
}
91+
return sum;
6192
}
6293

6394
/**
@@ -66,8 +97,20 @@ export function addNumbers(values: number[]): number {
6697
* @param n The first `n` of Fibonacci values to compute.
6798
* @return An array containing the first `n` Fibonacci values.
6899
*/
100+
69101
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
102+
//return [];
103+
if (n <= 0) return [];
104+
if (n === 1) return [1];
105+
106+
const fibonacci: number[] = [1, 1];
107+
108+
for (let i = 2; i < n; i++) {
109+
const numberFibonacci = fibonacci[i - 1] + fibonacci[i - 2];
110+
fibonacci.push(numberFibonacci);
111+
}
112+
113+
return fibonacci;
71114
}
72115

73116
/**
@@ -92,11 +135,18 @@ export function binarySearch(
92135

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

138+
if (values[pivotIndex] === value) {
139+
return pivotIndex;
140+
} else if (values[pivotIndex] > value) {
141+
return binarySearch(values, start, pivotIndex - 1, value);
142+
} else {
143+
return binarySearch(values, pivotIndex + 1, end, value);
144+
}
145+
95146
// TODO(you): Finish implementing this algorithm
96147

97148
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98149
// Else if values[pivotIndex] is greater than the value, then
99150
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100151
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
102152
}

0 commit comments

Comments
 (0)