-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
northwest |Rahwa Zeslus |module-structure-and-Testing-Data | WEEK3 #224
base: main
Are you sure you want to change the base?
Changes from 23 commits
fe5ab9e
dc58894
89ced3f
f1d08ce
051dde0
2596718
dede04f
4d3a429
81f1dc7
968ebe1
00be24d
a31b23f
c8e6bb1
e551597
7b89028
b9c04de
b482ac3
2f1cb35
6bf3fbb
1f64f33
0842687
7a67c3b
9099e59
0e8952c
a128541
8f9ab5d
a05fdef
fe69f36
f964082
8f7c9aa
6a501ab
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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
// Predict and explain first... | ||
// The issue with this code is that the multiply function does not return a value, but it is trying to use it in a template string which expects a return value. | ||
|
||
//To fix this, I should modify the multiply function to return the product of a and b instead of just logging it. | ||
|
||
// function multiply(a, b) { | ||
// console.log(a * b); | ||
// } | ||
|
||
// console.log(The result of multiplying 10 and 32 is ${multiply(10, 32)}); | ||
|
||
|
||
|
||
function multiply(a, b) { | ||
console.log(a * b); | ||
return(a * b); | ||
} | ||
|
||
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// Predict and explain first... | ||
|
||
//syntax error :the error is the semicolon and sum is place on new line from the return ,which does not have to execute in returning the result | ||
function sum(a, b) { | ||
return; | ||
a + b; | ||
return a + b; | ||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
// Predict and explain first... | ||
|
||
//the str is occured twice in the function and declaration | ||
// | ||
// call the function capitalise with a string input | ||
|
||
// interpret the error message and figure out why an error is occurring | ||
//SyntaxError: Identifier 'str' has already been declared :this means str is a parameter in the capitalise function and redeclare by keyword let which is not allowed to redeclare by let keyword | ||
|
||
function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// Predict and explain first... | ||
//decimalNumber is a parameter in the convertToPercentage function and it is declared again by const keyword. | ||
|
||
// Why will an error occur when this program runs? | ||
// Try playing computer with the example to work out what is going on | ||
|
||
function convertToPercentage(decimalNumber) { | ||
const decimalNumber = 0.5; | ||
const percentage = `${decimalNumber * 100}%`; | ||
|
||
return percentage; | ||
} | ||
|
||
console.log(decimalNumber); | ||
//SyntaxError: Identifier 'decimalNumber' has already been declared: decimalNumber is declared as parameter in the function,and it is also assigned to the number by the keyword const. | ||
|
||
function convertToPercentage(decimalNumber) { | ||
const percentage = `${decimalNumber * 100}%`; | ||
|
||
return percentage;} | ||
|
||
console.log(0.5); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
|
||
// Predict and explain first... | ||
|
||
//the number 3 as a parametre in square function is invalid | ||
// this function should square any number but instead we're going to get an error | ||
|
||
function square(3) { | ||
//SyntaxError: Unexpected number | ||
function square(num) { | ||
return num * num; | ||
} | ||
console.log(square(3)); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,31 @@ | |
// You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
||
// You should call this function a number of times to check it works for different inputs | ||
function toPounds(penceString) { | ||
|
||
if (!penceString || !penceString.endsWith("P")||isNaN(penceString)) { | ||
return "Invalid input. Please provide a valid pence amount"; | ||
} | ||
|
||
|
||
const penceStringWithoutTrailingP = penceString.substring( | ||
0, | ||
penceString.length - 1) | ||
|
||
|
||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
const pounds = paddedPenceNumberString.substring( | ||
0, | ||
paddedPenceNumberString.length - 2) | ||
|
||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) | ||
.padEnd(2, "0"); | ||
|
||
return (`£${pounds}.${pence}`); | ||
} | ||
|
||
console.log(toPounds("")) | ||
|
||
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. It would be best if you also handled edge cases like empty strings, missing "P", or invalid input. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,26 @@ | |
// Identify Reflex Angles: | ||
// When the angle is greater than 180 degrees and less than 360 degrees, | ||
// Then the function should return "Reflex angle" | ||
function getAngleType(angleMeasure){ | ||
|
||
if (angleMeasure === 90){ | ||
return "Right_Angle"; | ||
} | ||
else if (angleMeasure < 90) { | ||
return "Acute angle"; | ||
} | ||
else if (angleMeasure > 90 && angleMeasure < 180) { | ||
return "Obtuse angle"; | ||
} | ||
else if (angleMeasure===180) { | ||
return "Straight angle"; | ||
} | ||
else if (angleMeasure > 180) { | ||
return "Reflex angle"; | ||
} | ||
} | ||
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?
If the spec is not clear about how to classify 0 or negative angles, you can lookup the definition of "Acute angle". 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. Yes I understand ,I didn't see that way. So what I have done in this new code is try to change the degrees to to an equivalent angle within the standard range of |
||
console.log(getAngleType(90)) | ||
console.log(getAngleType(110)) | ||
console.log(getAngleType(10)) | ||
console.log(getAngleType(180)) | ||
console.log(getAngleType(200)) | ||
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. Do you notice any inconsistency among the output produced by these statements? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,27 @@ | |
// 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 rank = card.slice(0, -1); | ||
// to exclude the last character | ||
|
||
// Check for Number Cards (2-10) | ||
if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) { | ||
return Number(rank); | ||
} | ||
Comment on lines
+39
to
+41
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?
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. 1,getCardValue("010♠"); expected : 10 2,getCardValue("02♠"); expected :2 !isNaN(rank) → true because "02" is a numeric string. 3,getCardValue("0x02♠"); Expected output: 2 !isNaN(rank) → true because "0x02" is interpreted as a valid hexadecimal number in JavaScript. 4,getCardValue("2.1♠") ;Expected output: 2.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. At the end, do you want the function to recognise these card values? |
||
|
||
// Check for Face Cards (J, Q, K) or '10' | ||
if (['J', 'Q', 'K'].includes(rank) || rank === '10') { | ||
return 10; | ||
} | ||
|
||
// Check for Ace (A) | ||
if (rank === 'A') { | ||
return 11; | ||
} | ||
|
||
|
||
throw new Error("Invalid card rank."); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,24 @@ | |
// 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. | ||
|
||
|
||
//here I have to use a function with three parameter a,b,c | ||
function isValidTriangle(a,b,c){ | ||
// Another way to write this is a + b > c | ||
// It's also true that b + c > a | ||
// It's also true that a + c > b | ||
if((a + b > c) && (a + c > b) && (b + c > a)){ | ||
return true; | ||
} | ||
// scenario: invalid triangle | ||
// Check for Valid Input: | ||
// Given the sides a, b, and c, | ||
// When any of the sides are less than or equal to zero, | ||
// Then it should return false because a triangle cannot have zero or negative side lengths. | ||
if(a <= 0 || b <= 0 || c <= 0){ | ||
return false; | ||
} | ||
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. If any of I will not go into details why in some programming languages (but not JavaScript) we need also to ensure a, b, c are positives. 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. Thank you for your review! You're absolutely right in your observation. Since the check for whether any side is less than or equal to zero is already handled in the first condition (a <= 0 || b <= 0 || c <= 0) at line 43, the second check at line 51 is unnecessary. This means the function can be simplified by removing that second check, making the logic more concise and clearer. |
||
// if any other condition comes it return false | ||
return false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,36 @@ | |
// 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. | ||
function rotateCharacter(char, shift) { | ||
// Check if the character is a lowercase letter | ||
if (char >= "a" && char <= "z") { | ||
// Calculate new character with wraparound | ||
const charCode = char.charCodeAt(0); | ||
const rotatedCode = ((charCode - 97 + shift) % 26) + 97; // 'a' starts at ASCII 97 | ||
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 approach is good. |
||
return String.fromCharCode(rotatedCode); | ||
} | ||
|
||
|
||
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. | ||
if (char >= "A" && char <= "Z") { | ||
// Calculate new character with wraparound | ||
const charCode = char.charCodeAt(0); | ||
const rotatedCode = ((charCode - 65 + shift) % 26) + 65; // 'A' starts at ASCII 65 | ||
return String.fromCharCode(rotatedCode); | ||
} | ||
|
||
// If it's not a letter, return the character unchanged | ||
return char; | ||
} | ||
|
||
console.log(rotateCharacter("A", 3)); // Output: "D" | ||
console.log(rotateCharacter("F", 1)); // Output: "G" | ||
|
||
|
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.
While your implementation is correct, your explanation does not clearly explain the reason for the bug. Please give your code a second look and update your explanation.