-
-
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 all 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 |
---|---|---|
|
@@ -29,3 +29,31 @@ | |
// 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."); | ||
} | ||
console.log(getCardValue("010♠")); | ||
console.log(getCardValue("02♠")); | ||
console.log(getCardValue("0x02♠")); | ||
console.log(getCardValue("2.1♠")) |
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.