Skip to content

Feat: Yafiah added Lesson-07 Functions #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 79 additions & 8 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { computeLexicographicDistance } from "./util.js";
* @return True if the age corresponds to a voting age and false otherwise.
*/
export function canVote(age: number): boolean {
if(age>=18){
return true;
}else{
return false;
}
}

/**
Expand All @@ -21,12 +25,15 @@ export function compareStrings(a: string, b: string): number {
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
// if it is greater, and 0 if the strings are equal.
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

return 0;
if(distance<0){
return -1;
}else{
return distance
}

}


/**
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
* scale. See
Expand All @@ -37,17 +44,57 @@ export function compareStrings(a: string, b: string): number {
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
*/
export function convertGpaToLetterGrade(gpa: number): string {
return "F";
if(gpa >= 4.0){
return "A";
}
else if (gpa >= 3.7 && gpa <= 3.99){
return "A-";
}
else if(gpa >= 3.3 && gpa <= 3.69){
return "B+";
}
else if(gpa >= 3.0 && gpa <= 3.29){
return "B";
}
else if(gpa >= 2.7 && gpa <= 2.99){
return "B-";
}
else if(gpa>=2.3 && gpa <= 2.69){
return "C+";
}
else if(gpa >= 2.0 && gpa <= 2.29){
return "C";
}
else if(gpa >= 1.7 && gpa <= 1.99){
return "C-";
}
else if(gpa >= 1.3 && gpa <= 1.69){
return "D+";
}
else if(gpa >= 1 && gpa <= 1.29){
return "D";
}
else {
return "F";
}
}


/**
* Computes the factorial of the given value of `n`.
*
* @param n The value for which to compute the factorial.
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n === 0) {
return 1;
}
let sum=1;
for(let i = 1; i <=n; i++){
sum *= i;
}
return sum;
}

/**
Expand All @@ -57,7 +104,11 @@ export function computeFactorial(n: number): number {
* @return The sum of all the values.
*/
export function addNumbers(values: number[]): number {
return 0;
let sum = 0;
for (const value of values) {
sum += value;
}
return sum;
}

/**
Expand All @@ -67,9 +118,21 @@ export function addNumbers(values: number[]): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
const num: number[] = [];
for (let i = 0; i < n; i++) {
if (i === 0){
num[i] = 1;
}else if (i === 1){
num[i] = 1;
}else {
num[i] = num[i - 1] + num[i - 2];
}
}
return num;
}



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

// TODO(you): Finish implementing this algorithm
if (values[pivotIndex] == value) {
return pivotIndex;
}
else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value);
} else {
return binarySearch(values, pivotIndex + 1, end, value);
}

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