Birmingham | 26-ITP-Jan | Arun Kumar Akilan | Sprint 2 | Structuring-and-Testing-Data Coursework/Sprint2#1117
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5cecbee to
ab3d116
Compare
| @@ -1,5 +1,9 @@ | |||
| // Predict and explain first... | |||
| // Predict and explain first... | |||
| //Syntax error | |||
There was a problem hiding this comment.
Is this just a generic syntax issue, or is JavaScript specifically telling you something about variable declarations?
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; |
There was a problem hiding this comment.
You reassigned the parameter instead of redeclaring it.
In small examples this work, but do you think modifying function parameters is a good practice in larger codebases? Why or Why not?
Could you rewrite this function in a way that avoids mutating the parameter entirely?
| //Syntax error | ||
| // =============> write your prediction here | ||
| // We pass str as a parameter to the function. | ||
| //The parameter becomes a local variable inside the function scope. |
There was a problem hiding this comment.
If parameters behave like local variables, why does let cause an error here but var behaves differently? What does that tell you about scope rules?
| // =============> write your explanation here | ||
| //The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error. | ||
|
|
||
| //To fix this, I removed the variable keyword and modified the existing parameter instead. |
There was a problem hiding this comment.
In your explanation here, you said you modified the parameter instead of redeclaring it.
But in your final code (which is good), you didn’t modify the parameter, you removed the redeclaration.
Can you clarify what actually changed?
| return percentage; | ||
| } | ||
|
|
||
| const decimalNumber = convertToPercentage(0.5) ; |
There was a problem hiding this comment.
You name your variable decimalNumber which now holds a percentage string.
Does that variable name still accurately describe the value it contains?
| bmi = bmi.toFixed(1); | ||
| return Number(bmi); |
There was a problem hiding this comment.
You've successfully rounded the number using a string method (.toFixed). If we wanted to avoid converting the number to a string entirely, can you think of a purely mathematical way to round a decimal to one place using Math.round()? (Hint: Think about multiplying and dividing by 10).
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } No newline at end of file | ||
| let bmi = weight / (height * height); |
There was a problem hiding this comment.
Using height * height works perfectly for squaring! Out of curiosity, are you familiar with any JavaScript operators or Math methods that are specifically designed for calculating exponents?
| str = str.toUpperCase() | ||
|
|
||
| return str.replaceAll(" " , "_"); |
There was a problem hiding this comment.
Great job finding the toUpperCase and replaceAll methods! Notice how toUpperCase() returns a new string, and then you apply replaceAll() to that result on the next line. Since both of these are string methods, can you think of a way to connect or 'chain' them together into a single line of code?
| // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
|
|
||
| function upperSnakeCase(str){ | ||
| str = str.toUpperCase() |
There was a problem hiding this comment.
Your code works perfectly, but here you are overwriting the original str variable that was passed into the function. In professional development, we try to avoid changing our input parameters. If you use method chaining (commented about that below), how could you return the final result without ever reassigning the str variable?
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| time = hours - 12; |
There was a problem hiding this comment.
In your new logic, you assigned the calculated number to time. While JavaScript allows this, time was originally our input string (e.g., '19:00'). Reassigning parameters to completely different data types can sometimes lead to confusing bugs later on. Can you think of a better way to store your calculated hours - 12 without overwriting the time parameter? Perhaps a new variable name?
Learners, PR Template
Self checklist
Changelist
This PR contains my completed Sprint 2 coursework for the Structuring and Testing Data module.
Summary of changes:
padStartAll tasks have been completed and verified to work correctly.
Questions
No questions at this time.