Skip to content

Commit 265a107

Browse files
committed
Working on questions.
1 parent 70dccf2 commit 265a107

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ 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+
if (age >= 18) return true;
12+
13+
return true;
1114
}
1215

1316
/**
@@ -23,8 +26,11 @@ export function compareStrings(a: string, b: string): number {
2326
const distance = computeLexicographicDistance(a, b);
2427

2528
// TODO(you): Finish this method.
29+
if (distance < 0) {
30+
return -1;
31+
}
2632

27-
return 0;
33+
return distance;
2834
}
2935

3036
/**
@@ -37,6 +43,18 @@ export function compareStrings(a: string, b: string): number {
3743
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3844
*/
3945
export function convertGpaToLetterGrade(gpa: number): string {
46+
if (gpa > 4.0) return "A+";
47+
if (gpa >= 3.7) return "A";
48+
if (gpa >= 3.3) return "A-";
49+
if (gpa >= 3.0) return "B+";
50+
if (gpa >= 2.7) return "B";
51+
if (gpa >= 2.3) return "B-";
52+
if (gpa >= 2.0) return "C+";
53+
if (gpa >= 1.7) return "C";
54+
if (gpa >= 1.3) return "C-";
55+
if (gpa >= 1.0) return "D+";
56+
if (gpa >= 0.0) return "D";
57+
4058
return "F";
4159
}
4260

@@ -47,7 +65,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
4765
* @return The factorial of n.
4866
*/
4967
export function computeFactorial(n: number): number {
50-
return 0;
68+
if (n === 1 || n === 0) return 1;
69+
70+
let nums = 1;
71+
for (let i = 2; i <= n; i++) nums *= i;
72+
73+
return nums;
5174
}
5275

5376
/**
@@ -57,7 +80,13 @@ export function computeFactorial(n: number): number {
5780
* @return The sum of all the values.
5881
*/
5982
export function addNumbers(values: number[]): number {
60-
return 0;
83+
let sum = 0;
84+
85+
for (const value of values) {
86+
sum += value;
87+
}
88+
89+
return sum;
6190
}
6291

6392
/**
@@ -87,6 +116,7 @@ export function binarySearch(
87116
): number {
88117
if (end < start) {
89118
// The range is not valid so just return -1.
119+
90120
return -1;
91121
}
92122

0 commit comments

Comments
 (0)