Manchester | 26-ITP-May | Yee Man Tsang | Sprint 3 | 2-practice-tdd - #1507
Manchester | 26-ITP-May | Yee Man Tsang | Sprint 3 | 2-practice-tdd#1507lintsang wants to merge 2 commits into
Conversation
|
Don't forget to add the (I've picked this up today but bear it in mind for future!) |
| if (String(num).length > 1) { | ||
| //check if the number has more than 1 digit | ||
| onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number. | ||
| const tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number. | ||
|
|
||
| if (onesDigit == 1) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } else { | ||
| return num + "st"; | ||
| } | ||
| } else if (onesDigit == 2) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } | ||
| return num + "nd"; | ||
| } else if (onesDigit == 3) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } | ||
| return num + "rd"; | ||
| } else { | ||
| ("th"); | ||
| } | ||
| } else if (String(num).length == 1) { | ||
| onesDigit = Number(String(num)[0]); //extract the figure at the tens digit, and convert it into a number. | ||
| if (onesDigit == 1) { | ||
| return num + "st"; | ||
| console.log("Yeah youre in"); | ||
| } else if (onesDigit == 2) { | ||
| return num + "nd"; | ||
| } else if (onesDigit == 3) { | ||
| return num + "rd"; | ||
| } else { | ||
| return num + "th"; | ||
| } | ||
| } |
There was a problem hiding this comment.
This is a mostly working solution, but with a lot of nested if statements it can be quite hard to follow the logic of it.
A few things to consider:
- You repeat the same
if (tensDigit == 1) { return num + "th" }line, which suggests if the tens digit of the number is 1, you're always doing the same thing. How you might you simplify this so you only need to perform that check once? - On line 25 you have an
elsestatement that doesn't contain any logic. What should that statement be doing? Why wasn't it caught by the tests? - The logic you have for numbers whose "length" is 1 is very similar to the logic you have for numbers whose "length" is more than 1. You could definitely make this less repetitive!
- In a lot of places you have used
==instead of===- I asked you in a previous submission what the difference was and what would be better - same question applies here! - You've got a leftover
console.logon line 32 - get rid!
There was a problem hiding this comment.
Thanks for guiding me on this problem. I am having some insights for how to make this kind of statement in the future.
There was a problem hiding this comment.
This is so much better Lin! I hope you are happy with this improvement 😄
|
@lintsang Really nice improvements here Lin - I have no further comments so happy to mark as Complete ✅ |
|
Closing PR because the May ITP run has finished. Feel free to re-open if you're still working on it. |
Learners, PR Template
Self checklist
Changelist
Finished all exercises in 2-practice-tdd folder