Skip to content

Commit 23367a9

Browse files
committed
feat: implemented all the lesson 07 functions
1 parent 70dccf2 commit 23367a9

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ 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+
else
13+
return false;
1114
}
1215

1316
/**
@@ -21,10 +24,15 @@ export function compareStrings(a: string, b: string): number {
2124
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2225
// if it is greater, and 0 if the strings are equal.
2326
const distance = computeLexicographicDistance(a, b);
27+
if(distance === 0 )
28+
return 0;
29+
else if (distance > 0)
30+
return 1;
31+
else
32+
return -1;
2433

2534
// TODO(you): Finish this method.
26-
27-
return 0;
35+
// return 0;
2836
}
2937

3038
/**
@@ -37,7 +45,28 @@ export function compareStrings(a: string, b: string): number {
3745
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3846
*/
3947
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
48+
if(gpa === 4.0)
49+
return "A";
50+
else if (gpa >= 3.7 && gpa <= 3.99)
51+
return "A-";
52+
else if(gpa >= 3.3 && gpa <= 3.69)
53+
return "B+";
54+
else if(gpa >= 3.0 && gpa <= 3.29)
55+
return "B";
56+
else if(gpa >= 2.7 && gpa <= 2.99)
57+
return "B-";
58+
else if(gpa>=2.3 && gpa <= 2.69)
59+
return "C+";
60+
else if(gpa >= 2.0 && gpa <= 2.29)
61+
return "C";
62+
else if(gpa >= 1.7 && gpa <= 1.99)
63+
return "C-";
64+
else if(gpa >= 1.3 && gpa <= 1.69)
65+
return "D+";
66+
else if(gpa >= 1 && gpa <= 1.29)
67+
return "D";
68+
else (gpa === 0.99)
69+
return "F";
4170
}
4271

4372
/**
@@ -47,7 +76,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
4776
* @return The factorial of n.
4877
*/
4978
export function computeFactorial(n: number): number {
50-
return 0;
79+
if(n === 0) {
80+
return 1;
81+
}
82+
else{
83+
return n * computeFactorial(n-1);
84+
}
5185
}
5286

5387
/**
@@ -57,7 +91,11 @@ export function computeFactorial(n: number): number {
5791
* @return The sum of all the values.
5892
*/
5993
export function addNumbers(values: number[]): number {
60-
return 0;
94+
let sum = 0;
95+
for(const value of values){
96+
sum = sum + value;
97+
}
98+
return sum;
6199
}
62100

63101
/**
@@ -67,7 +105,20 @@ export function addNumbers(values: number[]): number {
67105
* @return An array containing the first `n` Fibonacci values.
68106
*/
69107
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
108+
const resultArray :number[] =[];
109+
if( n == 0)
110+
return resultArray;
111+
resultArray.push(1);
112+
let firstNumber: number = 1;
113+
let secondNumber: number = 1;
114+
let nextNumber : number = 0;
115+
for(let i: number = 1; i < n; i++ ){
116+
nextNumber = firstNumber + secondNumber;
117+
resultArray.push(secondNumber);
118+
firstNumber = secondNumber;
119+
secondNumber = nextNumber;
120+
}
121+
return resultArray;
71122
}
72123

73124
/**
@@ -93,10 +144,20 @@ export function binarySearch(
93144
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94145

95146
// TODO(you): Finish implementing this algorithm
96-
147+
if(values[pivotIndex]=== value){
148+
return pivotIndex;
149+
}
150+
else if (values[pivotIndex] > value){
151+
const posGreaterThanPivotIndex = binarySearch(values, start, pivotIndex - 1, value);
152+
return posGreaterThanPivotIndex;
153+
}
154+
else{
155+
const posLessThanPivotIndex = binarySearch(values, pivotIndex + 1, end, value);
156+
return posLessThanPivotIndex;
157+
}
97158
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98159
// Else if values[pivotIndex] is greater than the value, then
99160
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100161
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
162+
//return -1;
102163
}

0 commit comments

Comments
 (0)