Skip to content

Commit e73585d

Browse files
committed
feat: adds Kimberlee's lesson_07 conditionals and
loops exercise
1 parent 83d16b5 commit e73585d

File tree

4 files changed

+172
-22
lines changed

4 files changed

+172
-22
lines changed

lesson_07/conditionals/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lesson_07/conditionals/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"prettier": "3.3.3",
2929
"ts-jest": "^29.2.5",
3030
"ts-node": "^10.9.2",
31-
"typescript": "^5.6.2",
31+
"typescript": "^5.6.3",
3232
"typescript-eslint": "^8.7.0"
3333
}
34-
}
34+
}

lesson_07/conditionals/src/lesson7.ts

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

3-
/**
3+
/** (Q1)
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.
88
*/
99
export function canVote(age: number): boolean {
10-
return false;
10+
return age >= 18;
1111
}
1212

13-
/**
13+
const age = 12;
14+
let checkAge;
15+
16+
if (canVote(age)) {
17+
checkAge = true;
18+
} else {
19+
checkAge = false;
20+
}
21+
22+
console.log(checkAge);
23+
24+
/** (Q2)
1425
* Compares two strings lexicographically.
1526
*
1627
* @param a The first `string` to compare.
1728
* @param b The second `string` to compare.
1829
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
1930
*/
2031
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
22-
// if it is greater, and 0 if the strings are equal.
2332
const distance = computeLexicographicDistance(a, b);
33+
return distance;
34+
}
2435

25-
// TODO(you): Finish this method.
26-
27-
return 0;
36+
export function computeLexicographicDistance(a: string, b: string): number {
37+
//I literally used export function again because typescript kept telling me
38+
// that my return statements needed to be inside the body of a function.
39+
// I'm a bit skeptical because we are supposed to be DRY! and the only thing I
40+
// changed was the function name. But it worked. I'm just leaving this note
41+
// to remind myself to ask for clarification about this.
42+
if (a < b) {
43+
return -1;
44+
} else if (a > b) {
45+
return 1;
46+
} else {
47+
return 0;
48+
}
2849
}
2950

30-
/**
51+
const result = compareStrings("Kimberlee", "haldane");
52+
console.log(result);
53+
54+
//If I am understanding question 2 correctly, because the first function
55+
// 'compareStrings' calls on a second function 'computeLexicographicDistance',
56+
// I needed to set up both functions. The first function calls the second
57+
// and the second function does the actual calculation and determines if each string
58+
// is one of this set (-1, 1, 0). Testing is when the first function comes
59+
//back into play, comparing two strings and returning a numerical result. Sorry
60+
// for the long note, it took me a VERY long time to get to this understanding.
61+
// I just hope I am right.
62+
63+
/** (Q3)
3164
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
3265
* scale. See
3366
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
@@ -36,31 +69,86 @@ export function compareStrings(a: string, b: string): number {
3669
* @param gpa The GPA value.
3770
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3871
*/
72+
export function myGrade(grade: number): string {
73+
const gpa = convertGpaToLetterGrade(grade);
74+
return gpa;
75+
}
76+
77+
//I followed the same logic as question 2. I can already tell that
78+
// there may be many simpler ways to build this function. I am just
79+
// happy it came out with no errors and will make a note to play around
80+
// with this and see if I can't get it to look better (simpler/cleaner).
81+
3982
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
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) {
106+
return "A+";
107+
} else {
108+
return "unable to assess";
109+
}
41110
}
42111

43-
/**
112+
const grade = convertGpaToLetterGrade(32);
113+
console.log(grade);
114+
115+
/** (Q4)
44116
* Computes the factorial of the given value of `n`.
45117
*
46118
* @param n The value for which to compute the factorial.
47119
* @return The factorial of n.
48120
*/
49121
export function computeFactorial(n: number): number {
50-
return 0;
122+
let result = 1;
123+
for (let i = 1; i <= n; i++) { // I used i+1 and fell into an infinite loop. My bad!
124+
result *= i;
125+
}
126+
return result;
51127
}
128+
// Steps I followed: Describe the function, Declare the function, crawl inside the function
129+
const n = 7;
130+
console.log(computeFactorial(n));
131+
52132

53-
/**
133+
/** (Q5)
54134
* Adds all of the provided values and returns the sum.
55135
*
56136
* @param values The values to sum.
57137
* @return The sum of all the values.
58138
*/
59139
export function addNumbers(values: number[]): number {
60-
return 0;
140+
let total = 0;
141+
for (const value of values) {
142+
total += value;
143+
}
144+
return total;
61145
}
62146

63-
/**
147+
const numbers = [8, 4, 6, 2, 7];
148+
const sum = addNumbers(numbers);
149+
console.log(sum);
150+
151+
/** (Q6)
64152
* Returns an array of the first `n` Fibonacci numbers starting from 1.
65153
*
66154
* @param n The first `n` of Fibonacci values to compute.
@@ -70,7 +158,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
70158
return [];
71159
}
72160

73-
/**
161+
/** (Q7)
74162
* Finds a value in an array of values.
75163
*
76164
* @param values The values to search.

lesson_07/conditionals/src/test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { computeLexicographicDistance } from "./util.js";
2+
3+
/**
4+
* Returns true if the provided age meets the minimum US voting age and false otherwise.
5+
*
6+
* @param age The age to check.
7+
* @return True if the age corresponds to a voting age and false otherwise.
8+
*/
9+
export function canVote(age: number): boolean {
10+
return age >= 18;
11+
}
12+
13+
const age = 8;
14+
let voTing;
15+
16+
if (canVote(age)) {
17+
voTing = true;
18+
} else {
19+
voTing = false;
20+
}
21+
22+
console.log(voTing);
23+
24+
/**
25+
* Compares two strings lexicographically.
26+
*
27+
* @param a The first `string` to compare.
28+
* @param b The second `string` to compare.
29+
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
30+
*/
31+
export function compareStrings(a: string, b: string): number {
32+
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
33+
// if it is greater, and 0 if the strings are equal.
34+
const distance = computeLexicographicDistance(a, b);
35+
const distance: number
36+
return distance;
37+
}
38+
if (distance < 0) {
39+
console.log(-1);
40+
} else if (distance > 0) {
41+
console.log(1);
42+
} else {
43+
console.log(0);
44+
}
45+
46+
const result= compareStrings("Kimberlee", "haldane");
47+
console.log(result);
48+
49+
50+
// TODO(you): Finish this method.
51+
52+
**
53+
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
54+
* scale. See
55+
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
56+
* for details.
57+
*
58+
* @param gpa The GPA value.
59+
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
60+
*/
61+
export function convertGpaToLetterGrade(gpa: number): string {
62+
return "F";

0 commit comments

Comments
 (0)