Skip to content

Commit b024553

Browse files
committed
feat: adds Kimberlee's lesson_07 conditionals and
loops
1 parent 018ad1b commit b024553

File tree

2 files changed

+55
-95
lines changed

2 files changed

+55
-95
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,30 @@ export function myGrade(grade: number): string {
8080
// with this and see if I can't get it to look better (simpler/cleaner).
8181

8282
export function convertGpaToLetterGrade(gpa: number): string {
83-
if (gpa < 65) {
84-
return "F";
85-
} else if (gpa < 67 && gpa >= 65) {
86-
return "D";
87-
} else if (gpa < 70 && gpa >= 67) {
88-
return "D+";
89-
} else if (gpa < 73 && gpa >= 70) {
90-
return "C-";
91-
} else if (gpa < 77 && gpa >= 73) {
92-
return "C";
93-
} else if (gpa < 80 && gpa >= 77) {
94-
return "C+";
95-
} else if (gpa < 83 && gpa >= 80) {
96-
return "B-";
97-
} else if (gpa < 87 && gpa >= 83) {
98-
return "B";
99-
} else if (gpa < 90 && gpa >= 87) {
100-
return "B+";
101-
} else if (gpa < 93 && gpa >= 90) {
102-
return "A-";
103-
} else if (gpa < 97 && gpa >= 93) {
104-
return "A";
105-
} else if (gpa <= 100 && gpa >= 97) {
83+
if (gpa >= 4.0) {
10684
return "A+";
85+
} else if (gpa > 3.7 && gpa < 4.0) {
86+
return "A";
87+
} else if (gpa > 3.3 && gpa <= 3.7) {
88+
return "A-";
89+
} else if (gpa > 3.0 && gpa <= 3.3) {
90+
return "B+";
91+
} else if (gpa > 2.7 && gpa <= 3.0) {
92+
return "B";
93+
} else if (gpa > 2.3 && gpa <= 2.7) {
94+
return "B-";
95+
} else if (gpa > 2.0 && gpa <= 2.3) {
96+
return "C+";
97+
} else if (gpa > 1.7 && gpa <= 2.0) {
98+
return "C";
99+
} else if (gpa > 1.3 && gpa <= 1.7) {
100+
return "C-";
101+
} else if (gpa > 1.0 && gpa <= 1.3) {
102+
return "D+";
103+
} else if (gpa > 0.0 && gpa <= 1.0) {
104+
return "D";
105+
} else if (gpa < 0.0) {
106+
return "F";
107107
} else {
108108
return "unable to assess";
109109
}
@@ -120,7 +120,8 @@ console.log(grade);
120120
*/
121121
export function computeFactorial(n: number): number {
122122
let result = 1;
123-
for (let i = 1; i <= n; i++) { // I used i+1 and fell into an infinite loop. My bad!
123+
for (let i = 1; i <= n; i++) {
124+
// I used i+1 and fell into an infinite loop. My bad!
124125
result *= i;
125126
}
126127
return result;
@@ -129,7 +130,6 @@ export function computeFactorial(n: number): number {
129130
const n = 7;
130131
console.log(computeFactorial(n));
131132

132-
133133
/** (Q5)
134134
* Adds all of the provided values and returns the sum.
135135
*
@@ -154,10 +154,32 @@ console.log(sum);
154154
* @param n The first `n` of Fibonacci values to compute.
155155
* @return An array containing the first `n` Fibonacci values.
156156
*/
157+
//This question nearly had me in tears and I'm sad to say that in the end
158+
// I had to seek help. However, I think if I can explain what I learned as
159+
// I go, that help would not have been in vain. Again, pardon the long notes.
157160
export function getFirstNFibonacciNumbers(n: number): number[] {
158-
return [];
161+
const myArray: number[] = []; // First: initiatialize an empty array inside the function
162+
// Next:Take care of base case since the first two Fibonacci numbers are 1, 1.
163+
// If n is 1, return an array with the first number. If n is 2, return an array with
164+
// the first two numbers. This step is crucial for ensuring the loop works properly.
165+
if (n <= 0) return [];
166+
if (n === 1) return [1];
167+
if (n === 2) return [1, 1];
168+
myArray.push(1, 1);
169+
170+
for (let i = 2; i < n; i++) {
171+
//initialization(stats at 2 because I added the first two numbers in the sequence above)
172+
// so this tells the function to start computing from the 3rd number. Condition: this tells
173+
// the function that it can only work with numbers less than the given number.
174+
// Increment: this tells the function to move on to the next number by 1 (no infinite loops).
175+
const nextNum = myArray[i - 1] + myArray[i - 2];
176+
myArray.push(nextNum); // adds the new number to the array to continue the loop
177+
}
178+
return myArray;
159179
}
160180

181+
console.log(getFirstNFibonacciNumbers(7));
182+
161183
/** (Q7)
162184
* Finds a value in an array of values.
163185
*
@@ -180,11 +202,11 @@ export function binarySearch(
180202

181203
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
182204

183-
// TODO(you): Finish implementing this algorithm
184-
185-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
186-
// Else if values[pivotIndex] is greater than the value, then
187-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
188-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
189-
return -1;
205+
if (values[pivotIndex] === value) {
206+
return pivotIndex;
207+
} else if (values[pivotIndex] > value) {
208+
return binarySearch(values, start, pivotIndex - 1, value);
209+
}
210+
return binarySearch(values, pivotIndex + 1, end, value);
190211
}
212+
// This problem was much easier to write out because of question 6.

lesson_07/conditionals/src/test.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)