Skip to content

Commit b3957f4

Browse files
committed
feat: adding Dasia's homework answers
1 parent 19ae6d3 commit b3957f4

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ export function canVote(age: number): boolean {
2424
export function compareStrings(a: string, b: string): number {
2525
const distance = computeLexicographicDistance(a, b);
2626

27-
if (a > b) {
27+
if (distance < 0) {
2828
return -1;
29-
} else if (b > a) {
30-
return 1;
31-
} else {
32-
return 0;
3329
}
30+
31+
return distance;
3432
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
3533
// if it is greater, and 0 if the strings are equal.
3634

@@ -49,13 +47,13 @@ export function compareStrings(a: string, b: string): number {
4947
export function convertGpaToLetterGrade(gpa: number): string {
5048
if (gpa < 0 || gpa > 4.0) {
5149
return "Invaild GPA";
52-
} else if (gpa <= 0.9) {
50+
} else if (gpa < 1) {
5351
return "F";
54-
} else if (gpa < 1.25 && gpa >= 1.0) {
52+
} else if (gpa < 1.3 && gpa >= 1.0) {
5553
return "D";
56-
} else if (gpa < 1.75 && gpa >= 1.25) {
54+
} else if (gpa < 1.7 && gpa >= 1.3) {
5755
return "D+";
58-
} else if (gpa < 2.0 && gpa >= 1.75) {
56+
} else if (gpa < 2.0 && gpa >= 1.7) {
5957
return "C-";
6058
} else if (gpa < 2.3 && gpa >= 2.0) {
6159
return "C";
@@ -68,10 +66,9 @@ export function convertGpaToLetterGrade(gpa: number): string {
6866
} else if (gpa < 3.7 && gpa >= 3.3) {
6967
return "B+";
7068
} else if (gpa < 4.0 && gpa >= 3.7) {
71-
return "A";
72-
} else {
73-
return "A+";
69+
return "A-";
7470
}
71+
return "A";
7572
}
7673

7774
/**
@@ -82,7 +79,7 @@ export function convertGpaToLetterGrade(gpa: number): string {
8279
*/
8380
export function computeFactorial(n: number): number {
8481
let sum = 1;
85-
for (let i = 1; 1<=n; i++) {
82+
for (let i = 1; i <= n; i++) {
8683
sum = sum *= i;
8784
}
8885
return sum;
@@ -94,7 +91,11 @@ export function computeFactorial(n: number): number {
9491
* @return The sum of all the values.
9592
*/
9693
export function addNumbers(values: number[]): number {
97-
return 0;
94+
let sum = 0;
95+
for (let i = 0; i < values.length; i++) {
96+
sum = sum + values[i];
97+
}
98+
return sum;
9899
}
99100

100101
/**
@@ -104,7 +105,20 @@ export function addNumbers(values: number[]): number {
104105
* @return An array containing the first `n` Fibonacci values.
105106
*/
106107
export function getFirstNFibonacciNumbers(n: number): number[] {
107-
return [];
108+
if (n === 0) {
109+
return [];
110+
} else if (n === 1) {
111+
return [1];
112+
} else if (n === 2) {
113+
return [1, 1];
114+
}
115+
const fibArray = [1, 1];
116+
117+
for (let i = 2; i < n; i++) {
118+
const nextFibNumber = fibArray[i - 1] + fibArray[i - 2];
119+
fibArray.push(nextFibNumber);
120+
}
121+
return fibArray;
108122
}
109123

110124
/**

0 commit comments

Comments
 (0)