Skip to content

Commit d5ecf4f

Browse files
committed
feat:adds lesson_07/conditionals-nilejackson
1 parent 68305cb commit d5ecf4f

File tree

1 file changed

+83
-8
lines changed

1 file changed

+83
-8
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { computeLexicographicDistance } from "./util.js";
22

3-
/**
3+
/**
44
* Returns true if the provided age meets the minimum US voting age and false otherwise.
55
*
66
* @param age The age to check.
77
* @return True if the age corresponds to a voting age and false otherwise.
8-
*/
8+
*/
9+
910
export function canVote(age: number): boolean {
11+
return false;
12+
if(age >= 18) {
13+
return true;
14+
} else {
1015
return false;
11-
}
16+
}
17+
console.log(age >= 18);
1218

19+
}
1320
/**
1421
* Compares two strings lexicographically.
1522
*
@@ -20,11 +27,21 @@ export function canVote(age: number): boolean {
2027
export function compareStrings(a: string, b: string): number {
2128
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2229
// if it is greater, and 0 if the strings are equal.
23-
const distance = computeLexicographicDistance(a, b);
30+
const distance = computeLexicographicDistance(a, b);
31+
distance; a; 1;
32+
distance; b; 1; {
33+
if(distance > 0) {
34+
return 1; //equal distances
35+
} else if(distance <0 ) {
36+
return -1; //unequal distances
37+
} else if(distance === 0) {
38+
return 0;
39+
}
2440

41+
}
42+
//This is just an idea(That's probably wrong)/
2543
// TODO(you): Finish this method.
26-
27-
return 0;
44+
2845
}
2946

3047
/**
@@ -38,6 +55,30 @@ export function compareStrings(a: string, b: string): number {
3855
*/
3956
export function convertGpaToLetterGrade(gpa: number): string {
4057
return "F";
58+
if (gpa == 4.0) {
59+
return "A";
60+
} else if (gpa <= 3.99 && gpa >= 3.7) {
61+
return "A-";
62+
} else if (gpa <= 3.69 && gpa >= 3.3) {
63+
return "B+";
64+
} else if (gpa <= 3.29 && gpa >= 3.0) {
65+
return "B";
66+
} else if (gpa <= 2.99 && gpa >= 2.7) {
67+
return "B-";
68+
} else if (gpa <= 2.69 && gpa >= 2.3) {
69+
return "C+";
70+
} else if (gpa <= 2.29 && gpa >= 2.0) {
71+
return "C";
72+
} else if (gpa <= 1.99 && gpa >= 1.7) {
73+
return "C-";
74+
} else if (gpa <= 1.69 && gpa >= 1.3) {
75+
return "D+";
76+
} else if (gpa <= 1.29 && gpa >= 1.0) {
77+
return "D";
78+
} else {
79+
return "F";
80+
}
81+
4182
}
4283

4384
/**
@@ -48,6 +89,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
4889
*/
4990
export function computeFactorial(n: number): number {
5091
return 0;
92+
let product = 1;
93+
for (let i = 1; 1 <= n; i++) {
94+
product *= i;
95+
}
96+
97+
return product;
5198
}
5299

53100
/**
@@ -58,6 +105,10 @@ export function computeFactorial(n: number): number {
58105
*/
59106
export function addNumbers(values: number[]): number {
60107
return 0;
108+
let sum = 0; for(const value of values) {
109+
sum + value;
110+
}
111+
return sum;
61112
}
62113

63114
/**
@@ -67,7 +118,17 @@ export function addNumbers(values: number[]): number {
67118
* @return An array containing the first `n` Fibonacci values.
68119
*/
69120
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
121+
if (n < 1) {
122+
return [];
123+
}
124+
125+
const fibonacci: number[] = [1, 1]; // The function starts with the first two Fibonacci numbers
126+
127+
for (let i = 2; i < n; i++) {
128+
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; // Here the sum of the preceeding expression(?) is iterated with the number directly preceeding it
129+
}
130+
131+
return fibonacci.slice(0, n); // Return only the first n Fibonacci numbers
71132
}
72133

73134
/**
@@ -86,12 +147,26 @@ export function binarySearch(
86147
value: number,
87148
): number {
88149
if (end < start) {
89-
// The range is not valid so just return -1.
150+
90151
return -1;
91152
}
92153

154+
155+
156+
157+
93158
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94159

160+
if (values[pivotIndex] === value) {
161+
return pivotIndex;
162+
163+
} else if (values[pivotIndex] > value) {
164+
return binarySearch(values, start, pivotIndex -1 ,value);
165+
return value;
166+
} else {
167+
return (binarySearch(values, pivotIndex +1, end, value));
168+
169+
}
95170
// TODO(you): Finish implementing this algorithm
96171

97172
// If values[pivotIndex] is equal to value then return `pivotIndex`.

0 commit comments

Comments
 (0)