Skip to content

chore: jasonwatson_lesson_07_homework #322

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 9 commits into from
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=B
45 changes: 33 additions & 12 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { error } from "console";
import { computeLexicographicDistance } from "./util.js";

/**
Expand All @@ -11,9 +12,8 @@ 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.

if (distance < 0) return -1;
if (distance > 0) return 1;
return 0;
}

Expand All @@ -24,7 +24,17 @@ export function compareStrings(a: string, b: string): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n < 0) {
return 0;
}
if (n === 0 || n === 1) {
return 1;
}
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}

/**
Expand All @@ -34,7 +44,14 @@ export function computeFactorial(n: number): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
if (n <= 0) return [];
if (n === 1) return [0];

const fibSeries: number[] = [1, 1];
for (let i = 2; i < n; i++) {
fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]);
}
return fibSeries;
}

/**
Expand All @@ -58,12 +75,16 @@ 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] 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.
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);
}
return -1;
}
// 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.
19 changes: 19 additions & 0 deletions lesson_07/conditionals/src/part_b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
*/
export function isLeapYear(year: number): boolean {
return false;
switch (true) {
case year % 4 === 0:
return true;
case year % 100 === 0:
return false;
case year % 400 === 0:
return true;
default:
return false;
}
}

/**
Expand All @@ -15,6 +25,12 @@ export function isLeapYear(year: number): boolean {
* @returns
*/
export function isEvenOrOdd(num: number): string {
if (num % 2 === 0) {
return "even";
}
if (num % 2 !== 0) {
return "odd";
}
return "";
}

Expand All @@ -25,5 +41,8 @@ export function isEvenOrOdd(num: number): string {
* @returns
*/
export function hasVowel(word: string): boolean {
if (word.match(/[aeiou]/i)) {
return true;
}
return false;
}
Loading