Skip to content

Commit b7fccd8

Browse files
author
Yafiaha
committed
Feat: Yafiah added Lesson-07 Functions
1 parent 70dccf2 commit b7fccd8

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ 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+
if(age>=18){
11+
return true;
12+
}else{
1013
return false;
14+
}
1115
}
1216

1317
/**
@@ -21,12 +25,15 @@ export function compareStrings(a: string, b: string): number {
2125
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
2226
// if it is greater, and 0 if the strings are equal.
2327
const distance = computeLexicographicDistance(a, b);
24-
25-
// TODO(you): Finish this method.
26-
27-
return 0;
28+
if(distance<0){
29+
return -1;
30+
}else{
31+
return distance
32+
}
33+
2834
}
2935

36+
3037
/**
3138
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
3239
* scale. See
@@ -37,17 +44,57 @@ export function compareStrings(a: string, b: string): number {
3744
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3845
*/
3946
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
47+
if(gpa >= 4.0){
48+
return "A";
49+
}
50+
else if (gpa >= 3.7 && gpa <= 3.99){
51+
return "A-";
52+
}
53+
else if(gpa >= 3.3 && gpa <= 3.69){
54+
return "B+";
55+
}
56+
else if(gpa >= 3.0 && gpa <= 3.29){
57+
return "B";
58+
}
59+
else if(gpa >= 2.7 && gpa <= 2.99){
60+
return "B-";
61+
}
62+
else if(gpa>=2.3 && gpa <= 2.69){
63+
return "C+";
64+
}
65+
else if(gpa >= 2.0 && gpa <= 2.29){
66+
return "C";
67+
}
68+
else if(gpa >= 1.7 && gpa <= 1.99){
69+
return "C-";
70+
}
71+
else if(gpa >= 1.3 && gpa <= 1.69){
72+
return "D+";
73+
}
74+
else if(gpa >= 1 && gpa <= 1.29){
75+
return "D";
76+
}
77+
else {
78+
return "F";
79+
}
4180
}
4281

82+
4383
/**
4484
* Computes the factorial of the given value of `n`.
4585
*
4686
* @param n The value for which to compute the factorial.
4787
* @return The factorial of n.
4888
*/
4989
export function computeFactorial(n: number): number {
50-
return 0;
90+
if (n === 0) {
91+
return 1;
92+
}
93+
let sum=1;
94+
for(let i = 1; i <=n; i++){
95+
sum *= i;
96+
}
97+
return sum;
5198
}
5299

53100
/**
@@ -57,7 +104,11 @@ export function computeFactorial(n: number): number {
57104
* @return The sum of all the values.
58105
*/
59106
export function addNumbers(values: number[]): number {
60-
return 0;
107+
let sum = 0;
108+
for (const value of values) {
109+
sum += value;
110+
}
111+
return sum;
61112
}
62113

63114
/**
@@ -67,9 +118,21 @@ export function addNumbers(values: number[]): number {
67118
* @return An array containing the first `n` Fibonacci values.
68119
*/
69120
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
121+
const num: number[] = [];
122+
for (let i = 0; i < n; i++) {
123+
if (i === 0){
124+
num[i] = 1;
125+
}else if (i === 1){
126+
num[i] = 1;
127+
}else {
128+
num[i] = num[i - 1] + num[i - 2];
129+
}
130+
}
131+
return num;
71132
}
72133

134+
135+
73136
/**
74137
* Finds a value in an array of values.
75138
*
@@ -93,6 +156,14 @@ export function binarySearch(
93156
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94157

95158
// TODO(you): Finish implementing this algorithm
159+
if (values[pivotIndex] == value) {
160+
return pivotIndex;
161+
}
162+
else if (values[pivotIndex] > value) {
163+
return binarySearch(values, start, pivotIndex - 1, value);
164+
} else {
165+
return binarySearch(values, pivotIndex + 1, end, value);
166+
}
96167

97168
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98169
// Else if values[pivotIndex] is greater than the value, then

0 commit comments

Comments
 (0)