Skip to content

Commit 9c37bd5

Browse files
committed
Feat: Adds implementation on conditionals and loops
1 parent ff75e98 commit 9c37bd5

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 65 additions & 18 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
/**
@@ -21,10 +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-
25-
// TODO(you): Finish this method.
26-
27-
return 0;
28+
if (a < b) {
29+
return -1;
30+
} else if (a > b) {
31+
return 1;
32+
} else {
33+
return 0;
34+
}
2835
}
2936

3037
/**
@@ -37,7 +44,29 @@ export function compareStrings(a: string, b: string): number {
3744
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3845
*/
3946
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
47+
if (gpa === 4.0) {
48+
return "A";
49+
} else if (gpa >= 3.7) {
50+
return "A-";
51+
} else if (gpa >= 3.3) {
52+
return "B+";
53+
} else if (gpa >= 3.0) {
54+
return "B";
55+
} else if (gpa >= 2.7) {
56+
return "B-";
57+
} else if (gpa >= 2.3) {
58+
return "C+";
59+
} else if (gpa >= 2.0) {
60+
return "C";
61+
} else if (gpa >= 1.7) {
62+
return "C-";
63+
} else if (gpa >= 1.3) {
64+
return "D+";
65+
} else if (gpa >= 1.0) {
66+
return "D";
67+
} else {
68+
return "F";
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+
let result = 1;
80+
for (let i = 1; i <= n; i++) {
81+
result *= i;
82+
}
83+
return result;
5184
}
5285

5386
/**
@@ -57,7 +90,11 @@ 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+
let total = 0;
94+
for (const value of values) {
95+
total += value;
96+
}
97+
return total;
6198
}
6299

63100
/**
@@ -67,7 +104,18 @@ export function addNumbers(values: number[]): number {
67104
* @return An array containing the first `n` Fibonacci values.
68105
*/
69106
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
107+
if (n === 0) {
108+
return [];
109+
}
110+
if (n === 1) {
111+
return [1];
112+
}
113+
const fibonacciSequence: number[] = [1, 1];
114+
for (let i = 2; i < n; i++) {
115+
const newNumber = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
116+
fibonacciSequence.push(newNumber);
117+
}
118+
return fibonacciSequence.slice(0, n);
71119
}
72120

73121
/**
@@ -90,13 +138,12 @@ export function binarySearch(
90138
return -1;
91139
}
92140

93-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94-
95-
// TODO(you): Finish implementing this algorithm
96-
97-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98-
// Else if values[pivotIndex] is greater than the value, then
99-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
141+
const pivotIndex = Math.floor((start + end) / 2);
142+
if (values[pivotIndex] === value) {
143+
return pivotIndex;
144+
} else if (values[pivotIndex] > value) {
145+
return binarySearch(values, start, pivotIndex - 1, value);
146+
} else {
147+
return binarySearch(values, pivotIndex + 1, end, value);
148+
}
102149
}

0 commit comments

Comments
 (0)