Skip to content

Commit 51e3d5f

Browse files
author
jjcapparell
committed
Editing lesson7.ts with finished functions
1 parent 70dccf2 commit 51e3d5f

File tree

1 file changed

+114
-11
lines changed

1 file changed

+114
-11
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { computeLexicographicDistance } from "./util.js";
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+
if (age>=18){
11+
return true;
12+
}
13+
else{
14+
return false;
15+
}
1116
}
1217

1318
/**
@@ -21,23 +26,86 @@ export function compareStrings(a: string, b: string): number {
2126
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2227
// if it is greater, and 0 if the strings are equal.
2328
const distance = computeLexicographicDistance(a, b);
24-
25-
// TODO(you): Finish this method.
26-
27-
return 0;
29+
if (distance>0){
30+
return 1;
31+
}
32+
if (distance<0){
33+
return -1
34+
}
35+
else{
36+
return 0;
37+
}
2838
}
2939

3040
/**
3141
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
3242
* scale. See
3343
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
3444
* for details.
35-
*
45+
* A+ 97-100 4.0
46+
A 93-96 4.0
47+
A- 90-92 3.7
48+
B+ 87-89 3.3
49+
B 83-86 3.0
50+
B- 80-82 2.7
51+
C+ 77-79 2.3
52+
C 73-76 2.0
53+
C- 70-72 1.7
54+
D+ 67-69 1.3
55+
D 65-66 1.0
56+
E/F Below 65 0.0
3657
* @param gpa The GPA value.
3758
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3859
*/
3960
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
61+
let letterGrade = "N/A";
62+
if (gpa===4.0){
63+
letterGrade = "A";
64+
return letterGrade;
65+
}
66+
else if (gpa>=3.7 && gpa<4.0){
67+
letterGrade = "A-";
68+
return letterGrade;
69+
}
70+
else if (gpa>=3.3 && gpa<3.7){
71+
letterGrade = "B+";
72+
return letterGrade;
73+
}
74+
else if (gpa>=3.0 && gpa<3.3){
75+
letterGrade = "B";
76+
return letterGrade;
77+
}
78+
else if (gpa>=2.7 && gpa<3.0){
79+
letterGrade = "B-";
80+
return letterGrade;
81+
}
82+
else if (gpa>=2.3 && gpa<2.7){
83+
letterGrade = "C+";
84+
return letterGrade;
85+
}
86+
else if (gpa>=2.0 && gpa<2.3){
87+
letterGrade = "C";
88+
return letterGrade;
89+
}
90+
else if (gpa>=1.7 && gpa<2.0){
91+
letterGrade = "C-";
92+
return letterGrade;
93+
}
94+
else if (gpa>=1.3 && gpa<1.7){
95+
letterGrade = "D+";
96+
return letterGrade;
97+
}
98+
else if (gpa>=1.0 && gpa<1.3){
99+
letterGrade = "D";
100+
return letterGrade;
101+
}
102+
else if (gpa>=0 && gpa<1.0){
103+
letterGrade = "F";
104+
return letterGrade;
105+
}
106+
else{
107+
return letterGrade;
108+
}
41109
}
42110

43111
/**
@@ -47,7 +115,15 @@ export function convertGpaToLetterGrade(gpa: number): string {
47115
* @return The factorial of n.
48116
*/
49117
export function computeFactorial(n: number): number {
50-
return 0;
118+
let fact = 0
119+
if (n>0){
120+
fact = 1
121+
for (let i=n; i>=0; i--){
122+
fact *= i;
123+
}
124+
return fact;
125+
}
126+
return fact;
51127
}
52128

53129
/**
@@ -57,7 +133,14 @@ export function computeFactorial(n: number): number {
57133
* @return The sum of all the values.
58134
*/
59135
export function addNumbers(values: number[]): number {
60-
return 0;
136+
let sum=0;
137+
if (values.length>0){
138+
for (let i = 0; i<values.length; i++){
139+
sum+=values[i];
140+
}
141+
return sum;
142+
}
143+
return sum;
61144
}
62145

63146
/**
@@ -67,7 +150,19 @@ export function addNumbers(values: number[]): number {
67150
* @return An array containing the first `n` Fibonacci values.
68151
*/
69152
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
153+
let list: number[] = [];
154+
if (n>=1){
155+
list.push(0);
156+
}
157+
if (n>=2){
158+
list.push(1);
159+
}
160+
if (n>=3){
161+
for (let i=2; i<=n; i++){
162+
list[i] = list[i-1] + list[i-2];
163+
}
164+
}
165+
return list;
71166
}
72167

73168
/**
@@ -98,5 +193,13 @@ export function binarySearch(
98193
// Else if values[pivotIndex] is greater than the value, then
99194
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100195
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
196+
if (values[pivotIndex]===value){
197+
return pivotIndex;
198+
}
199+
else if(values[pivotIndex]>value){
200+
return binarySearch(values, start, pivotIndex-1, value);
201+
}
202+
else{
203+
return binarySearch(values, pivotIndex + 1, end, value);
204+
}
102205
}

0 commit comments

Comments
 (0)