Skip to content

Commit 382b75a

Browse files
committed
feat: Lesson 7 Homework - Partial Author:cdbluejr
1 parent 68305cb commit 382b75a

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import { computeLexicographicDistance } from "./util.js";
66
* @param age The age to check.
77
* @return True if the age corresponds to a voting age and false otherwise.
88
*/
9+
910
export function canVote(age: number): boolean {
10-
return false;
11+
if (age >= 18) {
12+
return true;
13+
} else {
14+
return false;
15+
}
1116
}
12-
1317
/**
1418
* Compares two strings lexicographically.
1519
*
@@ -21,7 +25,13 @@ export function compareStrings(a: string, b: string): number {
2125
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2226
// if it is greater, and 0 if the strings are equal.
2327
const distance = computeLexicographicDistance(a, b);
24-
28+
if (distance < 0) {
29+
return -1;
30+
} else if (distance > 0) {
31+
return 1;
32+
} else {
33+
return 0;
34+
}
2535
// TODO(you): Finish this method.
2636

2737
return 0;
@@ -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+
let letterGrade = "";
51+
if (gpa > 3.99) {
52+
letterGrade = "A";
53+
} else if (gpa > 3.69 && gpa < 4) {
54+
letterGrade = "A-";
55+
} else if (gpa > 3.29 && gpa < 3.7) {
56+
letterGrade = "B+";
57+
} else if (gpa > 2.99 && gpa < 3.3) {
58+
letterGrade = "B";
59+
} else if (gpa > 2.69 && gpa < 3.0) {
60+
letterGrade = "B-";
61+
} else if (gpa > 2.29 && gpa < 2.7) {
62+
letterGrade = "C+";
63+
} else if (gpa > 1.99 && gpa < 2.3) {
64+
letterGrade = "C";
65+
} else if (gpa > 1.69 && gpa < 2.0) {
66+
letterGrade = "C-";
67+
} else if (gpa > 1.29 && gpa < 1.7) {
68+
letterGrade = "D+";
69+
} else if (gpa > 0.99 && gpa < 1.3) {
70+
letterGrade = "D";
71+
} else if (gpa < 1) {
72+
letterGrade = "F";
73+
}
74+
return letterGrade;
4175
}
4276

4377
/**
@@ -47,7 +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 integer;
85+
let factorial = 1;
86+
for (integer = n; integer > 1; integer--) {
87+
factorial *= integer;
88+
}
89+
return factorial;
5190
}
5291

5392
/**
@@ -57,7 +96,11 @@ export function computeFactorial(n: number): number {
5796
* @return The sum of all the values.
5897
*/
5998
export function addNumbers(values: number[]): number {
60-
return 0;
99+
let sum = 0;
100+
for (let x = 0; values.length > x; x++) {
101+
sum += values[x];
102+
}
103+
return sum;
61104
}
62105

63106
/**

0 commit comments

Comments
 (0)