Skip to content

chore: added functions for Lesson07.ts and part_d #329

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 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion lesson_07/conditionals/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your homework version here
HW_VERSION=D
65 changes: 38 additions & 27 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@ import { computeLexicographicDistance } from "./util.js";
* @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);

// TODO(you): Finish this method.

return 0;
}
function compareStrings(a: string, b: string): number {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}

}

/**
* 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;
function computeFactorial(n: number): number {
if (n === 0) {
return 1;
}

let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}

return result;
}

/**
Expand All @@ -33,8 +43,17 @@ export function computeFactorial(n: number): number {
* @param n The first `n` of Fibonacci values to compute.
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
function getFirstNFibonacciNumbers(n: number): number[] {
let fib: number[] = [];

for (let i = 0; i < n; i++) {
if (i === 0 || i ===1) {
fib.push(1);
} else {
fib.push(fib[i - 1] + fib[i -2]);
}
}
return fib;
}

/**
Expand All @@ -46,24 +65,16 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
* @param value The value to look for.
* @return The index of the value if found in the array and -1 otherwise.
*/
export function binarySearch(
function findValue(
values: number[],
start: number,
end: number,
value: number,
value: number
): number {
if (end < start) {
// The range is not valid so just return -1.
return -1;
for (let i+ start; i <= end; i++) {
if (values[i] === value) {
return i;
}
}

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

// 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;
}
29 changes: 21 additions & 8 deletions lesson_07/conditionals/src/part_d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
* @param max
* @returns
*/
export function isWithinRange(num: number, min: number, max: number): boolean {
return false;
}
function isWithinRange(num: number, min: number, max: number): boolean {
return num >= min && num <= max;
}

console.log(isWithinRange(7, 5, 10));
console.log(isWithinRange(2, 5, 10));

/**
* Determine if a shape is a triangle based on the given side lengths.
Expand All @@ -18,8 +21,8 @@ export function isWithinRange(num: number, min: number, max: number): boolean {
* @param c
* @returns
*/
export function isValidTriangle(a: number, b: number, c: number): boolean {
return false;
function isValidTriangle(a: number, b: number, c: number): boolean {
return a + b > c && a + c > b && b + c > a;
}

/**
Expand All @@ -29,6 +32,16 @@ export function isValidTriangle(a: number, b: number, c: number): boolean {
* @param month
* @returns
*/
export function getSeason(month: number): string {
return "Invalid month";
}
function getSeason(month: number): string {
if (month >= 3 && month <= 5) {
return "Spring";
} else if (month >= 6 && month <= 8) {
return "Summer";
} else if (month >= 9 && month <= 11) {
return "Fall";
} else if (month === 12 || month === 1 || month === 2) {
return "Winter";
} else {
return "Invalid month";
}
}
6 changes: 6 additions & 0 deletions package-lock.json

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

Loading