Skip to content

Commit e05b2a4

Browse files
committed
feat: changes made to lesson7.ts file
1 parent 70dccf2 commit e05b2a4

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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 true;
11+
else return false;
1112
}
1213

1314
/**
@@ -21,10 +22,13 @@ export function compareStrings(a: string, b: string): number {
2122
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2223
// if it is greater, and 0 if the strings are equal.
2324
const distance = computeLexicographicDistance(a, b);
24-
25+
if (distance == 0) {
26+
return 0;
27+
} else if (distance < 1) {
28+
return -1;
29+
}
2530
// TODO(you): Finish this method.
26-
27-
return 0;
31+
else return 1;
2832
}
2933

3034
/**
@@ -37,7 +41,32 @@ export function compareStrings(a: string, b: string): number {
3741
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3842
*/
3943
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
44+
switch (true) {
45+
case gpa >= 4.0:
46+
return "A";
47+
case gpa <= 3.99 && gpa >= 3.7:
48+
return "A-";
49+
case gpa >= 3.3 && gpa <= 3.69:
50+
return "B+";
51+
case gpa >= 3.0 && gpa <= 3.29:
52+
return "B";
53+
case gpa >= 2.7 && gpa <= 2.99:
54+
return "B-";
55+
case gpa >= 2.3 && gpa <= 2.69:
56+
return "C+";
57+
case gpa >= 2.0 && gpa <= 2.29:
58+
return "C";
59+
case gpa >= 1.7 && gpa <= 1.99:
60+
return "C-";
61+
case gpa >= 1.3 && gpa <= 1.69:
62+
return "D+";
63+
case gpa >= 1.0 && gpa <= 1.29:
64+
return "D";
65+
case gpa < 1:
66+
return "F";
67+
default:
68+
return "unknown";
69+
}
4170
}
4271

4372
/**
@@ -47,7 +76,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
4776
* @return The factorial of n.
4877
*/
4978
export function computeFactorial(n: number): number {
50-
return 0;
79+
if (n == 0) return 1;
80+
for (let i = n - 1; i > 0; i--) {
81+
n *= i;
82+
}
83+
return n;
5184
}
5285

5386
/**
@@ -57,7 +90,8 @@ export function computeFactorial(n: number): number {
5790
* @return The sum of all the values.
5891
*/
5992
export function addNumbers(values: number[]): number {
60-
return 0;
93+
const sum: number = values.reduce((a, b) => a + b, 0);
94+
return sum;
6195
}
6296

6397
/**
@@ -67,7 +101,16 @@ export function addNumbers(values: number[]): number {
67101
* @return An array containing the first `n` Fibonacci values.
68102
*/
69103
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
104+
const arr = [1, 1];
105+
106+
if (n === 0) return [];
107+
if (n === 1) return [arr[n]];
108+
else if (n > 1) {
109+
for (let i = 3; i <= n; i++) {
110+
arr[i - 1] = arr[i - 3] + arr[i - 2];
111+
}
112+
}
113+
return arr;
71114
}
72115

73116
/**
@@ -85,18 +128,21 @@ export function binarySearch(
85128
end: number,
86129
value: number,
87130
): number {
131+
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
132+
88133
if (end < start) {
89134
// The range is not valid so just return -1.
90135
return -1;
136+
} else if (values[pivotIndex] == value) return pivotIndex;
137+
else if (values[pivotIndex] > value) {
138+
return binarySearch(values, start, pivotIndex - 1, value);
139+
} else {
140+
return binarySearch(values, pivotIndex + 1, end, value);
91141
}
92-
93-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94-
95142
// TODO(you): Finish implementing this algorithm
96143

97144
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98145
// Else if values[pivotIndex] is greater than the value, then
99146
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100147
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
102148
}

0 commit comments

Comments
 (0)