Skip to content

Commit b2a1d9f

Browse files
committed
feat:adding lesson07 homework
1 parent 87d30bf commit b2a1d9f

File tree

1 file changed

+66
-19
lines changed

1 file changed

+66
-19
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@ export function canVote(age: number): boolean {
1818
*
1919
* @param a The first `string` to compare.
2020
* @param b The second `string` to compare.
21-
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
21+
* @return -1 if a (the first 'string) is less than b (the second 'string'), 1 if a is greater than b, and 0 otherwise.
2222
*/
2323
export function compareStrings(a: string, b: string): number {
24-
if (a > b) {
24+
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`,
25+
// 1 if it is greater b, and 0 if both strings are equal.
26+
const distance = computeLexicographicDistance(a, b);
27+
28+
if (distance < 0) {
29+
return -1;
30+
} else if (distance > 0) {
2531
return 1;
26-
}
27-
if (a < b) {
28-
return -1;
29-
}
30-
if (a=b) {
32+
} else {
3133
return 0;
3234
}
33-
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
34-
// if it is greater, and 0 if the strings are equal.
35-
const distance = computeLexicographicDistance(a, b);
36-
37-
// TODO(you): Finish this method.
3835

39-
return 0;
36+
// TODO(you): Finish this method.
4037
}
4138

4239
/**
@@ -49,7 +46,31 @@ export function compareStrings(a: string, b: string): number {
4946
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
5047
*/
5148
export function convertGpaToLetterGrade(gpa: number): string {
52-
return "F";
49+
if (gpa === 4.0) {
50+
return "A";
51+
} else if (gpa > 4.0) {
52+
return "A";
53+
} else if (gpa <= 3.99 && gpa >= 3.7) {
54+
return "A-";
55+
} else if (gpa <= 3.69 && gpa >= 3.3) {
56+
return "B+";
57+
} else if (gpa <= 3.29 && gpa >= 3.0) {
58+
return "B";
59+
} else if (gpa <= 2.99 && gpa >= 2.7) {
60+
return "B-";
61+
} else if (gpa <= 2.69 && gpa >= 2.3) {
62+
return "C+";
63+
} else if (gpa <= 2.29 && gpa >= 2.0) {
64+
return "C";
65+
} else if (gpa <= 1.99 && gpa >= 1.7) {
66+
return "C-";
67+
} else if (gpa <= 1.69 && gpa >= 1.3) {
68+
return "D+";
69+
} else if (gpa <= 1.29 && gpa >= 1.0) {
70+
return "D";
71+
} else {
72+
return "F";
73+
}
5374
}
5475

5576
/**
@@ -59,9 +80,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
5980
* @return The factorial of n.
6081
*/
6182
export function computeFactorial(n: number): number {
62-
let product=1
63-
for()
64-
return 0;
83+
let product = 1;
84+
for (let i = 1; 1 <= n; i++) {
85+
product *= i;
86+
}
87+
88+
return product;
6589
}
6690

6791
/**
@@ -71,7 +95,11 @@ export function computeFactorial(n: number): number {
7195
* @return The sum of all the values.
7296
*/
7397
export function addNumbers(values: number[]): number {
74-
return 0;
98+
let sum = 0;
99+
for (const value of values) {
100+
sum += value;
101+
}
102+
return sum;
75103
}
76104

77105
/**
@@ -81,7 +109,18 @@ export function addNumbers(values: number[]): number {
81109
* @return An array containing the first `n` Fibonacci values.
82110
*/
83111
export function getFirstNFibonacciNumbers(n: number): number[] {
84-
return [];
112+
let current = 1;
113+
let prev = 0;
114+
115+
const numbers = [];
116+
for (let i = 1; i <= n; i++) {
117+
numbers.push(current);
118+
const nextNum = current + prev;
119+
prev = current;
120+
current = nextNum;
121+
}
122+
123+
return numbers;
85124
}
86125

87126
/**
@@ -105,12 +144,20 @@ export function binarySearch(
105144
}
106145

107146
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
147+
if (values[pivotIndex] === value) {
148+
return pivotIndex;
149+
} else if (values[pivotIndex] > value) {
150+
return binarySearch(values, start, pivotIndex - 1, value);
151+
} else {
152+
return binarySearch(values, pivotIndex + 1, end, value);
153+
}
108154

109155
// TODO(you): Finish implementing this algorithm
110156

111157
// If values[pivotIndex] is equal to value then return `pivotIndex`.
112158
// Else if values[pivotIndex] is greater than the value, then
113159
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
114160
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
161+
115162
return -1;
116163
}

0 commit comments

Comments
 (0)