-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
London 11 | Pezhman-Azizi | Structuring-and-testing-data | week3 | sprint 3 #214
base: main
Are you sure you want to change the base?
Changes from all commits
782bd64
eda8a0d
ba2e832
2f76a64
9f854dc
6023fb8
2714bd0
61e406a
e46ba79
ad3bbe8
73cbf25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
|
||
// Acceptance criteria: | ||
|
||
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), | ||
// Given a card string in the format "A♠" (representing a card in blackjack - the last character | ||
// will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), | ||
// When the function getCardValue is called with this card string as input, | ||
// Then it should return the numerical card value | ||
|
||
|
@@ -29,3 +30,19 @@ | |
// Given a card with an invalid rank (neither a number nor a recognized face card), | ||
// When the function is called with such a card, | ||
// Then it should throw an error indicating "Invalid card rank." | ||
function getCardValue(card){ | ||
|
||
const cardValue = card.slice(0, -1); // // Extracts the value of a card by slicing. | ||
// Starts at index 0 (the first character) and excludes the last character (index -1). | ||
const numericValue = Number(cardValue); | ||
|
||
if ( numericValue >= 2 && numericValue <= 10){ //insures that the numeric value is valid. For example 13♠ should return "Invalid card rank." | ||
return `your score is: ${numericValue}`; | ||
}else if(["J", "Q", "K"].includes(cardValue)){ //makes sure that the card value includes one of the letters of K, J or Q. | ||
return `your score is: ${10}`; | ||
}else if (cardValue === "A"){ | ||
return `your score is: ${11}`; | ||
}else | ||
return `Invalid card rank.` | ||
} | ||
console.log(getCardValue("K♠")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you expect from the following function calls?
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,30 @@ | |
// target output: false | ||
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. | ||
|
||
function isProperFraction(numerator, denominator){ | ||
numerator = Math.abs(numerator); | ||
denominator = Math.abs(denominator); | ||
|
||
if(denominator === 0){ | ||
throw new Error("Denominator cannot be zero"); | ||
}else if(numerator === denominator){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test, |
||
return false; | ||
}else if(numerator < denominator){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
console.log(isProperFraction(4, 0)); | ||
|
||
|
||
console.assert(isProperFraction(2, 3) === true, "2/3 should be a proper fraction"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also test negative values in numerator and/or denominator (for cases when |numerator| < |denominator|). |
||
console.assert(isProperFraction(5, 2) === false, "5/2 should return an improper fraction"); | ||
console.assert(isProperFraction(-4, 7) === true, "-7/2 should return a proper fraction"); | ||
try { | ||
isProperFraction(4, 0); // Call the function with invalid input | ||
console.assert(false, "4/0 should throw an error"); // Fail the test if no error is thrown | ||
} catch (e) { | ||
console.assert(e.message === "Denominator cannot be zero", "Error message should be 'Denominator cannot be zero'"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,17 @@ | |
// It's also true that b + c > a | ||
// It's also true that a + c > b | ||
|
||
// In our function isValidTriangle which takes as parameters the lengths of three sides, we need to invalidate any triangle where the sum of any two sides is less than or equal to the length of the third side. | ||
// In our function isValidTriangle which takes as parameters the lengths of three sides, | ||
// we need to invalidate any triangle where the sum of any two sides is less than or equal to the length of the third side. | ||
// and we need to validate any triangle where the sum of any two sides is greater than the length of the third side. | ||
|
||
// Acceptance criteria: | ||
|
||
// scenario: invalid triangle | ||
// Given the side lengths a, b, and c, | ||
// When the sum of any two side lengths is less than or equal to the length of the third side (i.e., a + b <= c, a + c <= b, b + c <= a), | ||
// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. | ||
// Then it should return false because these conditions violate the Triangle Inequality, | ||
// which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. | ||
|
||
// scenario: invalid triangle | ||
// Check for Valid Input: | ||
|
@@ -32,4 +34,19 @@ | |
// When the function is called with these values as input, | ||
// Then it should return true because the input forms a valid triangle. | ||
|
||
// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. | ||
// This specification outlines the behavior of the isValidTriangle function for different input scenarios, | ||
// ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. | ||
|
||
function isValidTriangle(a, b, c){ | ||
// Checks if the sides are positive | ||
if (a <= 0 || b <= 0 || c <= 0){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to include the if-statement at lines 42-44? I will not go into details why in some programming languages (but not JavaScript) we need also to check if a, b, c are positives. The main point I would like to make is, you should fully understand what you wrote in your code. An interviewer may ask you questions like what I am asking here, and it would reflect poorly on you if you cannot explain your code. |
||
return false; | ||
} | ||
// Checks to see the sum of two sides is greater than the third one | ||
if (a+b > c && b+c > a && a+c > b){ | ||
return true; | ||
} | ||
// Any other cases is false | ||
return false; | ||
} | ||
console.log(isValidTriangle(1,2,3)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,29 +15,69 @@ | |
// Scenario: Rotate Lowercase Letters: | ||
// Given a lowercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string. | ||
console.log(rotateCharacter("a", 3)); // Output: "d" | ||
console.log(rotateCharacter("f", 1)); // Output: "g" | ||
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, | ||
// and return the rotated lowercase letter as a string. | ||
// console.log(rotateCharacter("a", 3)); // Output: "d" | ||
// console.log(rotateCharacter("f", 1)); // Output: "g" | ||
|
||
// Scenario: Rotate Uppercase Letters: | ||
// Given an uppercase letter character and a positive integer shift, | ||
// When the function is called with these inputs, | ||
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string. | ||
console.log(rotateCharacter("A", 3)); // Output: "D" | ||
console.log(rotateCharacter("F", 1)); // Output: "G" | ||
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, | ||
// wrapping around if necessary, and return the rotated uppercase letter as a string. | ||
// console.log(rotateCharacter("A", 3)); // Output: "D" | ||
// console.log(rotateCharacter("F", 1)); // Output: "G" | ||
|
||
// Scenario: Leave Non-Letter Characters Unchanged: | ||
// Given a character that is not a letter (neither uppercase nor lowercase) and any positive or negative shift value, | ||
// When the function is called with these inputs, | ||
// Then it should return the character unchanged. | ||
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid and invalid characters, and defines the expected output or action for each case. | ||
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, | ||
// including valid and invalid characters, and defines the expected output or action for each case. | ||
// console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) | ||
|
||
// Scenario: Shifting a Character with Wraparound | ||
// Given a character char within the lowercase alphabet range (e.g., 'z') or the uppercase alphabet range (e.g., 'Z'), | ||
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., a shift of 3 for 'z' or 'Z'), | ||
// When the rotateCharacter function is called with char and shift as inputs, | ||
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound, | ||
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C'). | ||
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around) | ||
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around) | ||
// console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around) | ||
// console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around) | ||
|
||
|
||
|
||
|
||
|
||
// ------------------------------------------------------------ MY APPROACH------------------------------------------------------------------------------------------ | ||
// This approach works with ASCII (American Standard Code for Information Interchange) which assigns a unique numeric code for characters, including | ||
// letters, digits and symbols. In this case we are dealing with letters only. | ||
// Lower case letters range from 97 to 122 and uppercase letters range from 65 to 90. | ||
|
||
function rotateCharacter(letter, shift){ | ||
|
||
const asciiValue = letter.charCodeAt(0); // Gets the ASCII value for each alphabetic letter. | ||
|
||
shift = shift % 26; // normalizing shift for large and negative values. | ||
|
||
if(asciiValue >= 65 && asciiValue <= 90){ //ASCII code for uppercase | ||
let rotatedAscii = asciiValue + shift; | ||
if(rotatedAscii >= 91){ | ||
rotatedAscii -= 26; // Applies the wraparound for upper case letters. | ||
}else if(asciiValue < 65){ | ||
rotatedAscii += 26; // Sorts out the negative wraparound | ||
} | ||
}else if (asciiValue >= 97 && asciiValue <= 122){ //ASCII code for lowercase | ||
let rotatedAscii = asciiValue + shift; | ||
if(rotatedAscii>=123){ | ||
rotatedAscii -= 26; // Applies the wraparound for lower case letters. | ||
}else if(rotatedAscii < 97){ | ||
rotatedAscii += 26; // Sorts out the negative wraparound | ||
} | ||
return String.fromCharCode(rotatedAscii); | ||
}else{ | ||
return `Not a valid character`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spec says |
||
} | ||
} | ||
|
||
console.log(rotateCharacter("z", -1)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// function countChar(str, char){ | ||
// let count = 0; | ||
// str.split("").forEach(element => { | ||
// if (element === char){ | ||
// count+=1;} | ||
// }); | ||
// if (count > 0){ | ||
// return `${char} appears ${count} times in ${str}`; | ||
// }else{ | ||
// return `no occurrences of the "${char}" were found in the "${str}"`; | ||
// } | ||
// } | ||
// // exports.countChar = {countChar}; | ||
// module.exports = { countChar }; | ||
function countChar(str, char) { | ||
let count = 0; | ||
str.split("").forEach(element => { | ||
if (element === char) { | ||
count += 1; | ||
} | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the spec wasn't clear enough. Usually a function that "count something" would simply return an integer. |
||
if (count > 0) { | ||
return `${char} appears ${count} times in ${str}`; | ||
} else { | ||
return `no occurrences of the "${char}" were found in the "${str}"`; | ||
} | ||
} | ||
|
||
module.exports = { countChar }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function getOrdinalNumber(number){ | ||
|
||
let firstDigit = number%10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better practice is to use |
||
let secondDigit = number%100; | ||
|
||
let suffix = "th"; | ||
|
||
if(secondDigit >= 11 && secondDigit <= 13){ | ||
return `${number}${suffix}`; | ||
}else if (firstDigit === 1){ | ||
suffix = "st" | ||
}else if (firstDigit === 2){ | ||
suffix = "nd"; | ||
}else if (firstDigit === 3){ | ||
suffix = "rd"; | ||
} | ||
return `${number}${suffix}`; | ||
} | ||
console.log(getOrdinalNumber(45)); | ||
|
||
module.exports = getOrdinalNumber; // Export the function |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function isPrime(number){ | ||
|
||
console.log(number); | ||
|
||
if(number<=1) return false; | ||
|
||
for (let i = 2; i <= number-1; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To check if Doing so can possibly improve the performance of the function. You can try Google to find out "how to determine if a number is prime efficiently in JS". |
||
if (number % i === 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
console.log(isPrime(5)); | ||
|
||
module.exports = isPrime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one test case?
Without using Jest, you can prepare your test as
console.assert(getAngleType(180) === "Right angle", "Right angle");
so that if the function does not return the expected value, you will see the error message "Right angle".
Can you include more test cases to thoroughly test your function? They can help you detect bugs in your code.