Skip to content

Commit 7f5583d

Browse files
committed
working on lesson_07
1 parent 70dccf2 commit 7f5583d

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ 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) {
11+
return true;
12+
} else {
13+
return false;
14+
}
1115
}
1216

1317
/**
@@ -18,13 +22,19 @@ export function canVote(age: number): boolean {
1822
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
1923
*/
2024
export function compareStrings(a: string, b: string): number {
25+
const distance = computeLexicographicDistance(a, b);
26+
27+
if (a > b) {
28+
return -1;
29+
} else if (b > a) {
30+
return 1;
31+
} else {
32+
return 0;
33+
}
2134
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2235
// if it is greater, and 0 if the strings are equal.
23-
const distance = computeLexicographicDistance(a, b);
2436

2537
// TODO(you): Finish this method.
26-
27-
return 0;
2838
}
2939

3040
/**
@@ -37,7 +47,31 @@ export function compareStrings(a: string, b: string): number {
3747
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3848
*/
3949
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
50+
if (gpa < 0 || gpa > 4.0) {
51+
return "Invaild GPA";
52+
} else if (gpa <= 0.9) {
53+
return "F";
54+
} else if (gpa < 1.25 && gpa >= 1.0) {
55+
return "D";
56+
} else if (gpa < 1.75 && gpa >= 1.25) {
57+
return "D+";
58+
} else if (gpa < 2.0 && gpa >= 1.75) {
59+
return "C-";
60+
} else if (gpa < 2.3 && gpa >= 2.0) {
61+
return "C";
62+
} else if (gpa < 2.7 && gpa >= 2.3) {
63+
return "C+";
64+
} else if (gpa < 3.0 && gpa >= 2.7) {
65+
return "B-";
66+
} else if (gpa < 3.3 && gpa >= 3.0) {
67+
return "B";
68+
} else if (gpa < 3.7 && gpa >= 3.3) {
69+
return "B+";
70+
} else if (gpa < 4.0 && gpa >= 3.7) {
71+
return "A";
72+
} else {
73+
return "A+";
74+
}
4175
}
4276

4377
/**
@@ -47,9 +81,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
4781
* @return The factorial of n.
4882
*/
4983
export function computeFactorial(n: number): number {
50-
return 0;
84+
let sum = 1;
85+
for (let i = 1; 1<=n; i++) {
86+
sum = sum *= i;
87+
}
88+
return sum;
5189
}
52-
5390
/**
5491
* Adds all of the provided values and returns the sum.
5592
*

0 commit comments

Comments
 (0)