Skip to content
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

feat: adds Kimberlee's Lesson07 conditionals #293

Closed
wants to merge 11 commits into from
Closed
8 changes: 4 additions & 4 deletions lesson_07/conditionals/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lesson_07/conditionals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"prettier": "3.3.3",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"typescript-eslint": "^8.7.0"
}
}
}
142 changes: 115 additions & 27 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
import { computeLexicographicDistance } from "./util.js";
//import { computeLexicographicDistance } from "./util.js";

/**
/** (Q1)
* Returns true if the provided age meets the minimum US voting age and false otherwise.
*
* @param age The age to check.
* @return True if the age corresponds to a voting age and false otherwise.
*/
export function canVote(age: number): boolean {
return false;
return age >= 18;
}

/**
const age = 12;
let checkAge;

if (canVote(age)) {
checkAge = true;
} else {
checkAge = false;
}

console.log(checkAge);

/** (Q2)
* Compares two strings lexicographically.
*
* @param a The first `string` to compare.
* @param b The second `string` to compare.
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
*/
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);
return distance;
}

// TODO(you): Finish this method.

return 0;
export function computeLexicographicDistance(a: string, b: string): number {

if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}

/**
const result = compareStrings("Kimberlee", "haldane");
console.log(result);



/** (Q3)
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
* scale. See
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
Expand All @@ -36,41 +58,104 @@ export function compareStrings(a: string, b: string): number {
* @param gpa The GPA value.
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
*/
export function myGrade(grade: number): string {
const gpa = convertGpaToLetterGrade(grade);
return gpa;
}


export function convertGpaToLetterGrade(gpa: number): string {
return "F";
if (gpa >= 4.0) {
return "A";
} else if (gpa >= 3.7) {
return "A-";
} else if (gpa >= 3.3) {
return "B+";
} else if (gpa >= 3.0) {
return "B";
} else if (gpa >= 2.7) {
return "B-";
} else if (gpa >= 2.3) {
return "C+";
} else if (gpa >= 2.0) {
return "C";
} else if (gpa >= 1.7) {
return "C-";
} else if (gpa >= 1.3) {
return "D+";
} else if (gpa >= 1.0) {
return "D";
} else if (gpa < 1.0 && gpa >= 0) {
return "F";
} else {
return "Invalid";
}
}

/**
const grade = convertGpaToLetterGrade(3.2);
console.log(grade);

/** (Q4)
* 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;
let result = 1;
for (let i = 1; i <= n; i++) {

result *= i;
}
return result;
}

/**
const n = 7;
console.log(computeFactorial(n));

/** (Q5)
* Adds all of the provided values and returns the sum.
*
* @param values The values to sum.
* @return The sum of all the values.
*/
export function addNumbers(values: number[]): number {
return 0;
let total = 0;
for (const value of values) {
total += value;
}
return total;
}

/**
const numbers = [8, 4, 6, 2, 7];
const sum = addNumbers(numbers);
console.log(sum);

/** (Q6)
* Returns an array of the first `n` Fibonacci numbers starting from 1.
*
* @param n The first `n` of Fibonacci values to compute.
* @return An array containing the first `n` Fibonacci values.
*/
//** I had to look up how to complete this problem on google
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
const myArray: number[] = [];
if (n <= 0) return [];
if (n === 1) return [1];
if (n === 2) return [1, 1];
myArray.push(1, 1);

for (let i = 2; i < n; i++) {

const nextNum = myArray[i - 1] + myArray[i - 2];
myArray.push(nextNum);
}
return myArray;
}

/**
console.log(getFirstNFibonacciNumbers(7));

/** (Q7)
* Finds a value in an array of values.
*
* @param values The values to search.
Expand All @@ -86,17 +171,20 @@ export function binarySearch(
value: number,
): number {
if (end < start) {
// The range is not valid so just return -1.

return -1;
}

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
const pivotIndex = Math.floor((start + end) / 2);

// TODO(you): Finish implementing this algorithm

// If values[pivotIndex] is equal to value then return `pivotIndex`.
// Else if values[pivotIndex] is greater than the value, then
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
return -1;
if (values[pivotIndex] === value) {
return pivotIndex;
} else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value);
}
return binarySearch(values, pivotIndex + 1, end, value);
}

const values = [ 2, 4, 6, 8, 10, 12, 14, 16];
const index = binarySearch(values, 4, 8, 14);
console.log(index);