-
-
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
WM4 | Fatima Safana |Module 2/Sprint1 Exercises | Week4 #213
base: main
Are you sure you want to change the base?
Conversation
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.
Code is good. I just left some suggestions and questions to challenge you in the code.
@@ -1,9 +1,13 @@ | |||
const cardNumber = 4533787178994213; | |||
const cardNumber = "4533787178994213"; |
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.
If you cannot modify this statement const cardNumber = 4533787178994213;
(that is, keep the variable's value unchanged),
how would you modify the code so that it can still extract the last 4 digits from its value.
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.
the type of variable needs to string for .slice() method to work.
const twelveHourClockTime = "20:53"; |
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.
Have you also noticed the variable names do not quite match the values assigned to the variable?
console.log(wholeNumberPart) | ||
|
||
let decimalPart = num - Math.floor(num); | ||
console.log(decimalPart); |
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.
If you run the program, you may notice that console.log()
outputs the value of decimalPart
with more than 4 decimal places (which is normal because of floating point arithmetic).
If you were asked to print the value of decimalPart
to exactly 4 decimal places,
how would you modify the code?
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.
let decimalPart = (num - Math.floor(num)).toFixed(4);
console.log(decimalPart);
console.log(`The base part of ${filePath} is ${base}`); | ||
|
||
// Create a variable to store the dir part of the filePath variable | ||
// Create a variable to store the ext part of the variable | ||
|
||
const dirPart = filePath.substring(45, -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.
Your code should be written to to work for any valid value of filePath
.
For example, "/path1/path2/package.json"
.
const dirPart = filePath.substring(45, -1); | ||
console.log(`The directory path of ${dirPart}`); | ||
|
||
const extPart = dirPart + "ext.txt"; |
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.
The "ext part" refers to the file extension of the file represented by the file path, which is the string after the dot in the base (including the dot).
So the file extension of "package.json" would be ".json".
|
||
console.log(num); | ||
|
||
// The program creates a random whole number between 1 and 100, max and min inclusive. |
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.
This exercise was expecting you to break down
the expression Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
, and explain
why such expression can correctly produce a random integer in the range [1, 100].
Also, to test your understanding, how would you write an expression
(without using any variable in the expression) that yields a random
integer between -5 and 3 (including -5 and 3)?
// 3. initializes a variable that removes the "p" at the end, it uses method .substring | ||
// which returns the part of the string corresponding to the index and end index | ||
//-1 excluding the p | ||
//9. uses the method padstart to add "0" to the start of string penceStringWithoutTrailingP, |
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.
After line 9, what would paddedPenceNumberString
be if penceString
was "1234p" initially?
After line 9, what would paddedPenceNumberString
be if penceString
was "4p" initially?
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.
It will return 1234
It will return 004
// this effectively multiplies any value input as penceStringWithoutTrailingP by 100 | ||
//12. uses the substring method to return the first value of the string, this is to get | ||
// the pound. it also used length -2 to make sure its in a valid range so the program | ||
// can go through the steps with any value input. |
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.
What do you mean by "first value of the string"?
length - 2
is a valid range but "making sure its in a valid range" is not quite the reason why length - 2
is used in the code.
//12. uses the substring method to return the first value of the string, this is to get | ||
// the pound. it also used length -2 to make sure its in a valid range so the program | ||
// can go through the steps with any value input. | ||
//18.the code gets the last two digits from the paddedPenceNumberString and ensures that the result |
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.
Can we expect this program to work as intended even if we deleted .padEnd(2, "0")
from the code?
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.
no, it will not work for single digit or double digits input
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.
Well, can you find a value of penceString
to show that the function won't work properly without .padEnd(2, "0")
?
Thank you @cjyuan for reviewing my code, I appreciate the feedback. |
Merge branch 'sprint-1-exercises' of https://github.com/Fatimasfn/Module-Structuring-and-Testing-Data into sprint-1-exercises
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.