Skip to content

Commit 75fedc8

Browse files
committed
feat:adds-nilejack/hw
1 parent d5ecf4f commit 75fedc8

File tree

1 file changed

+47
-64
lines changed

1 file changed

+47
-64
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
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+
*/
99

1010
export function canVote(age: number): boolean {
11-
return false;
12-
if(age >= 18) {
13-
return true;
14-
} else {
15-
return false;
16-
}
17-
console.log(age >= 18);
18-
11+
if (age >= 18) {
12+
return true;
13+
} else {
14+
return false;
15+
}
1916
}
2017
/**
2118
* Compares two strings lexicographically.
@@ -27,21 +24,17 @@ console.log(age >= 18);
2724
export function compareStrings(a: string, b: string): number {
2825
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2926
// if it is greater, and 0 if the strings are equal.
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-
}
27+
const distance = computeLexicographicDistance(a, b);
4028

41-
}
4229
//This is just an idea(That's probably wrong)/
4330
// TODO(you): Finish this method.
44-
31+
let stringLengthCompare = 0;
32+
if (distance > 0) {
33+
stringLengthCompare = 1; //equal distances
34+
} else if (distance < 0) {
35+
stringLengthCompare = -1; //unequal distances
36+
}
37+
return stringLengthCompare;
4538
}
4639

4740
/**
@@ -54,31 +47,29 @@ export function compareStrings(a: string, b: string): number {
5447
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
5548
*/
5649
export function convertGpaToLetterGrade(gpa: number): string {
57-
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-
50+
if (gpa == 4.0) {
51+
return "A";
52+
} else if (gpa <= 3.99 && gpa >= 3.7) {
53+
return "A-";
54+
} else if (gpa <= 3.69 && gpa >= 3.3) {
55+
return "B+";
56+
} else if (gpa <= 3.29 && gpa >= 3.0) {
57+
return "B";
58+
} else if (gpa <= 2.99 && gpa >= 2.7) {
59+
return "B-";
60+
} else if (gpa <= 2.69 && gpa >= 2.3) {
61+
return "C+";
62+
} else if (gpa <= 2.29 && gpa >= 2.0) {
63+
return "C";
64+
} else if (gpa <= 1.99 && gpa >= 1.7) {
65+
return "C-";
66+
} else if (gpa <= 1.69 && gpa >= 1.3) {
67+
return "D+";
68+
} else if (gpa <= 1.29 && gpa >= 1.0) {
69+
return "D";
70+
} else {
71+
return "F";
72+
}
8273
}
8374

8475
/**
@@ -88,7 +79,6 @@ if (gpa == 4.0) {
8879
* @return The factorial of n.
8980
*/
9081
export function computeFactorial(n: number): number {
91-
return 0;
9282
let product = 1;
9383
for (let i = 1; 1 <= n; i++) {
9484
product *= i;
@@ -104,9 +94,9 @@ export function computeFactorial(n: number): number {
10494
* @return The sum of all the values.
10595
*/
10696
export function addNumbers(values: number[]): number {
107-
return 0;
108-
let sum = 0; for(const value of values) {
109-
sum + value;
97+
let sum = 0;
98+
for (const value of values) {
99+
sum += value;
110100
}
111101
return sum;
112102
}
@@ -125,7 +115,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
125115
const fibonacci: number[] = [1, 1]; // The function starts with the first two Fibonacci numbers
126116

127117
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
118+
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; // Here the sum of the preceeding expression(?) is iterated with the number directly preceeding it
129119
}
130120

131121
return fibonacci.slice(0, n); // Return only the first n Fibonacci numbers
@@ -147,26 +137,19 @@ export function binarySearch(
147137
value: number,
148138
): number {
149139
if (end < start) {
150-
151140
return -1;
152141
}
153142

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

160145
if (values[pivotIndex] === value) {
161146
return pivotIndex;
162-
163147
} 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-
}
148+
return binarySearch(values, start, pivotIndex - 1, value);
149+
return value;
150+
} else {
151+
return binarySearch(values, pivotIndex + 1, end, value);
152+
}
170153
// TODO(you): Finish implementing this algorithm
171154

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

0 commit comments

Comments
 (0)