Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
*package.json
*package-lock.json
**/.DS_Store
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Original file:
*
// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
Expand Down Expand Up @@ -35,3 +38,33 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
*
* End of original file
*/

// Implementation a function getAngleType

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)

function getAngleType(angle) {
// Check for invalid angles first (angles ≤ 0 or ≥ 360)
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

// Check for specific angle types
if (angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else { // angle > 180 and < 360 (already validated above)
return "Reflex angle";
}
}

module.exports = getAngleType;

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Original file:
*
// Implement a function isProperFraction,
// when given two numbers, a numerator and a denominator, it should return true if
// the given numbers form a proper fraction, and false otherwise.
Expand Down Expand Up @@ -31,3 +34,78 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
*
* End of the original file
*/

// Implementation of a function isProperFraction

// when given two numbers, a numerator and a denominator, it should return true if
// the given numbers form a proper fraction, and false otherwise.

// Assumption: The parameters are valid numbers (not NaN or Infinity).

function isProperFraction(numerator, denominator) {
// Check if denominator is zero - not a valid fraction
if (denominator === 0) {
return false;
}

// Check if both numbers are integers
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
return false;
}

// Check if absolute value of numerator is less than absolute value of denominator
return Math.abs(numerator) < Math.abs(denominator);
}

module.exports = isProperFraction;

// Here's our helper function again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases.
console.log("Running tests for isProperFraction...");

// Test 1: Proper fractions with positive numbers
assertEquals(isProperFraction(1, 2), true); // 1/2 is proper
assertEquals(isProperFraction(3, 4), true); // 3/4 is proper
assertEquals(isProperFraction(5, 8), true); // 5/8 is proper
assertEquals(isProperFraction(0, 5), true); // 0/5 is proper (0 < 5)

// Test 2: Improper fractions with positive numbers
assertEquals(isProperFraction(3, 2), false); // 3/2 is improper
assertEquals(isProperFraction(4, 3), false); // 4/3 is improper
assertEquals(isProperFraction(5, 5), false); // 5/5 is equal, so improper

// Test 3: Fractions with negative numbers
assertEquals(isProperFraction(-1, 2), true); // |-1| < 2, so proper
assertEquals(isProperFraction(1, -2), true); // 1 < |-2|, so proper
assertEquals(isProperFraction(-3, -4), true); // |-3| < |-4|, so proper
assertEquals(isProperFraction(-3, 2), false); // |-3| > 2, so improper
assertEquals(isProperFraction(3, -2), false); // 3 > |-2|, so improper
assertEquals(isProperFraction(-4, -3), false); // |-4| > |-3|, so improper

// Test 4: Edge cases with zero
assertEquals(isProperFraction(0, 1), true); // 0/1 is proper
assertEquals(isProperFraction(0, -5), true); // 0/ -5 is proper
assertEquals(isProperFraction(5, 0), false); // Denominator cannot be zero
assertEquals(isProperFraction(0, 0), false); // Denominator cannot be zero

// Test 5: Non-integer inputs
assertEquals(isProperFraction(1.5, 2), false); // Numerator not integer
assertEquals(isProperFraction(1, 2.5), false); // Denominator not integer
assertEquals(isProperFraction(1.5, 2.5), false); // Both not integers

// Test 6: Large numbers
assertEquals(isProperFraction(100, 101), true); // 100 < 101
assertEquals(isProperFraction(1000, 999), false); // 1000 > 999
assertEquals(isProperFraction(-100, 101), true); // |-100| < 101

console.log("Tests completed!");
125 changes: 125 additions & 0 deletions Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Original file:
*
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Implement a function getCardValue, when given a string representing a playing card,
Expand Down Expand Up @@ -50,3 +53,125 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
*
* End of original file
*/

// Implementation

function getCardValue(card) {
// Validate input type
if (typeof card !== 'string' || card.length < 2) {
throw new Error('Invalid card format');
}

// Define valid ranks and suits
const validRanks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
const validSuits = ['♠', '♥', '♦', '♣'];

// Extract rank and suit
let rank, suit;

// Handle '10' which is 2 characters
if (card.startsWith('10') && card.length === 3) {
rank = '10';
suit = card[2];
} else if (card.length === 2) {
rank = card[0];
suit = card[1];
} else {
throw new Error('Invalid card format');
}

// Validate rank and suit
if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error('Invalid card format');
}

// Return value based on rank
if (rank === 'A') {
return 11;
} else if (['J', 'Q', 'K'].includes(rank)) {
return 10;
} else {
return parseInt(rank, 10);
}
}

module.exports = getCardValue;

function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// Helper function to test error throwing
function assertThrowsError(fn, expectedErrorMessage) {
try {
fn();
console.error(`Error was not thrown. Expected: ${expectedErrorMessage}`);
} catch (e) {
console.assert(
e.message === expectedErrorMessage,
`Expected error message "${expectedErrorMessage}" but got "${e.message}"`
);
}
}

// Tests for valid cards
console.log('Testing valid cards:');
assertEquals(getCardValue('A♠'), 11);
assertEquals(getCardValue('A♥'), 11);
assertEquals(getCardValue('A♦'), 11);
assertEquals(getCardValue('A♣'), 11);

assertEquals(getCardValue('2♠'), 2);
assertEquals(getCardValue('3♥'), 3);
assertEquals(getCardValue('4♦'), 4);
assertEquals(getCardValue('5♣'), 5);
assertEquals(getCardValue('6♠'), 6);
assertEquals(getCardValue('7♥'), 7);
assertEquals(getCardValue('8♦'), 8);
assertEquals(getCardValue('9♣'), 9);
assertEquals(getCardValue('10♠'), 10);
assertEquals(getCardValue('10♥'), 10);
assertEquals(getCardValue('10♦'), 10);
assertEquals(getCardValue('10♣'), 10);

assertEquals(getCardValue('J♠'), 10);
assertEquals(getCardValue('Q♥'), 10);
assertEquals(getCardValue('K♦'), 10);
assertEquals(getCardValue('J♣'), 10);

// Tests for invalid cards
console.log('\nTesting invalid cards:');

// Invalid format
assertThrowsError(() => getCardValue(''), 'Invalid card format');
assertThrowsError(() => getCardValue('A'), 'Invalid card format');
assertThrowsError(() => getCardValue('10'), 'Invalid card format');
assertThrowsError(() => getCardValue('A♠♠'), 'Invalid card format');
assertThrowsError(() => getCardValue('10♠♠'), 'Invalid card format');

// Invalid rank
assertThrowsError(() => getCardValue('1♠'), 'Invalid card format');
assertThrowsError(() => getCardValue('11♠'), 'Invalid card format');
assertThrowsError(() => getCardValue('B♠'), 'Invalid card format');
assertThrowsError(() => getCardValue('X♠'), 'Invalid card format');

// Invalid suit
assertThrowsError(() => getCardValue('A♤'), 'Invalid card format'); // Using ♤ instead of ♠
assertThrowsError(() => getCardValue('A♡'), 'Invalid card format'); // Using ♡ instead of ♥
assertThrowsError(() => getCardValue('A♢'), 'Invalid card format'); // Using ♢ instead of ♦
assertThrowsError(() => getCardValue('A♧'), 'Invalid card format'); // Using ♧ instead of ♣

// Invalid type
assertThrowsError(() => getCardValue(123), 'Invalid card format');
assertThrowsError(() => getCardValue(null), 'Invalid card format');
assertThrowsError(() => getCardValue(undefined), 'Invalid card format');
assertThrowsError(() => getCardValue({}), 'Invalid card format');

console.log('\nAll tests completed!');

Loading