Skip to content

Commit 20c7b3e

Browse files
committed
fix: lesson_07 added solutions
1 parent d12e7a3 commit 20c7b3e

File tree

1 file changed

+92
-20
lines changed

1 file changed

+92
-20
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
import { computeLexicographicDistance } from "./util.js";
2-
31
/**
42
* Returns true if the provided age meets the minimum US voting age and false otherwise.
53
*
64
* @param age The age to check.
75
* @return True if the age corresponds to a voting age and false otherwise.
86
*/
97
export function canVote(age: number): boolean {
8+
if (age >= 18) {
9+
console.log("You can Vote");
10+
return true;
11+
} else console.log("you cannot vote");
1012
return false;
1113
}
1214

1315
/**
1416
* Compares two strings lexicographically.
1517
*
16-
* @param a The first `string` to compare.
17-
* @param b The second `string` to compare.
18+
* @param a The first string to compare.
19+
* @param b The second string to compare.
1820
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
1921
*/
2022
export function compareStrings(a: string, b: string): number {
21-
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
23+
// The distance will be a number less than 0 if string a is lexicographically less than b, 1
2224
// if it is greater, and 0 if the strings are equal.
23-
const distance = computeLexicographicDistance(a, b);
24-
2525
// TODO(you): Finish this method.
2626

27-
return 0;
27+
if (a === b) {
28+
return 0;
29+
} else if (a > b) {
30+
return 1;
31+
} else return -1;
2832
}
2933

3034
/**
@@ -37,17 +41,52 @@ 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 >= 97:
46+
return "A+";
47+
case gpa >= 93 && gpa <= 96:
48+
return "A";
49+
case gpa >= 90 && gpa <= 92:
50+
return "A-";
51+
case gpa >= 87 && gpa <= 89:
52+
return "B+";
53+
case gpa >= 83 && gpa <= 86:
54+
return "B";
55+
case gpa >= 80 && gpa <= 82:
56+
return "B-";
57+
case gpa >= 77 && gpa <= 79:
58+
return "C+";
59+
case gpa >= 73 && gpa <= 76:
60+
return "C";
61+
case gpa >= 70 && gpa <= 72:
62+
return "C-";
63+
case gpa >= 67 && gpa <= 69:
64+
return "D+";
65+
case gpa >= 65 && gpa <= 66:
66+
return "D";
67+
default:
68+
return "F";
69+
}
4170
}
42-
4371
/**
44-
* Computes the factorial of the given value of `n`.
72+
* Computes the factorial of the given value of n.
4573
*
4674
* @param n The value for which to compute the factorial.
4775
* @return The factorial of n.
4876
*/
4977
export function computeFactorial(n: number): number {
50-
return 0;
78+
if (n === 1 || n === 0) {
79+
return 1;
80+
} else {
81+
let factorial = 1; //initialize variable
82+
for (
83+
let i = 2;
84+
i <= n;
85+
i++ // i starts at 2 and increments until it is equal to n
86+
)
87+
factorial = factorial * i; // factorial loops by multiplying with i until it = n
88+
return factorial;
89+
}
5190
}
5291

5392
/**
@@ -57,17 +96,45 @@ export function computeFactorial(n: number): number {
5796
* @return The sum of all the values.
5897
*/
5998
export function addNumbers(values: number[]): number {
60-
return 0;
99+
let sum = 0; // initialize variable
100+
101+
// eslint-disable-next-line @typescript-eslint/prefer-for-of
102+
for (let i = 0; i < values.length; i++) {
103+
// loop starting 0 as long as i is less than the length of values array increment
104+
sum = sum + values[i]; // add the values in the array;
105+
}
106+
return sum;
61107
}
62108

63109
/**
64-
* Returns an array of the first `n` Fibonacci numbers starting from 1.
110+
* Returns an array of the first n Fibonacci numbers starting from 1.
65111
*
66-
* @param n The first `n` of Fibonacci values to compute.
67-
* @return An array containing the first `n` Fibonacci values.
112+
* @param n The first n of Fibonacci values to compute.
113+
* @return An array containing the first n Fibonacci values.
68114
*/
69115
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
116+
const array: number[] = []; // takes number and converts to array
117+
if (n === 0) {
118+
// base case for array 0
119+
array.push(0);
120+
return array;
121+
}
122+
if (n === 1) {
123+
// base case for array 1
124+
array.push(0);
125+
array.push(1);
126+
return array;
127+
}
128+
array.push(0);
129+
array.push(1);
130+
131+
// starting at 2 increment i until it is = n
132+
for (let i = 2; i <= n; i++) {
133+
const Fibonacci = array[i - 1] + array[i - 2]; //add the numbers 2 positions behind to get current position
134+
array.push(Fibonacci);
135+
}
136+
137+
return array;
71138
}
72139

73140
/**
@@ -94,9 +161,14 @@ export function binarySearch(
94161

95162
// TODO(you): Finish implementing this algorithm
96163

97-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
164+
// If values[pivotIndex] is equal to value then return pivotIndex.
98165
// 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.
166+
// call binarySearch(values, start, pivotIndex - 1, value) and return its value;
167+
// Else call binarySearch(values, pivotIndex + 1, end, value) and return its value.
168+
if (values[pivotIndex] === pivotIndex) return pivotIndex;
169+
else if (values[pivotIndex] > pivotIndex)
170+
return binarySearch(values, start, pivotIndex - 1, value);
171+
else return binarySearch(values, pivotIndex + 1, end, value);
172+
101173
return -1;
102174
}

0 commit comments

Comments
 (0)