Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
You found all the real dead code: greetingStr and the line after return in exercise 1, and capitalisedPets and logPets in exercise 2. The trouble is that more than the dead code has gone, so neither file prints what it did before.
Before I can mark this Complete:
exercise-1.jsnow prints nothing. The starter printedhello, Aman!, and that final log is what the dead code was being removed around. See my comment on line 6.exercise-2.jsshould still print{ HAMSTER: 3, HORSE: 1 }from thepetsarray, andcountAndCapitalisePetsshould still count whatever array it is given. See my comments on lines 4, 10 and 25.- PR tidy-up: add
CYF-1066under a "Task code" heading in the description, update the title toLondon | 26-ITP-Sep | Russom Gebremeskel | Sprint 3 | Dead Code(you're in the September cohort now, and this task is Sprint 3), and remove theNotCourseworklabel. That label tells volunteers the PR isn't coursework and to skip it; the bot's message was asking for the task code, not that label.
Add the Needs Review label again once you've pushed.
| const greetingStr = greeting + ", " + name + "!"; | ||
| return `${greeting}, ${name}!`; | ||
| console.log(greetingStr); | ||
| } |
There was a problem hiding this comment.
Run node dead-code/exercise-1.js on main and on your branch. What does each print? Dead code is code that never affects the output, so was console.log(greetingMessage) dead?
There was a problem hiding this comment.
I made the sayHello function to continue to work for any reasonable input it's given.
I added a console.log() now
There was a problem hiding this comment.
The log is back, thanks. Run node dead-code/exercise-1.js now: it prints Hi, Aman!, but main prints hello, Aman!. Where does the Hi on line 8 come from, and what did the starter pass in as the greeting?
| @@ -2,27 +2,24 @@ | |||
| // The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable. | |||
|
|
|||
| const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; | |||
There was a problem hiding this comment.
Where is pets used now?
There was a problem hiding this comment.
I misunderstood the instruction. It is fixed now.
There was a problem hiding this comment.
Fixed, pets is used again and unchanged.
| } | ||
| }); | ||
| petsArr | ||
| .filter((pet) => pet[0] === "h") |
There was a problem hiding this comment.
The comment on line 2 says this function should keep working for any reasonable input. What does countAndCapitalisePets(["dog", "cat", "dog"]) return on main, and what does it return now?
There was a problem hiding this comment.
Try it: countAndCapitalisePets(["dog", "cat", "dog"]) returns {} with your version. On main it returns { DOG: 2, CAT: 1 }.
The .filter((pet) => pet[0] === "h") on line 10 is the reason. In the starter, the filtering happened outside the function, on its own line: const petsStartingWithH = pets.filter(...). That line wasn't dead code, because its result was what got passed into the function.
Put that line back under pets, take the filter out of the function so it goes back to a plain petsArr.forEach(...), and pass petsStartingWithH into the function on the last line. Your counting code on lines 12 to 18 doesn't need to change.
|
|
||
| console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log | ||
| const animals = ["lion", "hyena", "elephant", "hippo", "tiger", "hippo"]; | ||
| console.log(countAndCapitalisePets(animals)); |
There was a problem hiding this comment.
The starter's final log printed { HAMSTER: 3, HORSE: 1 }. What does yours print, and which array is it counting?
There was a problem hiding this comment.
The output is right again. Once the filter moves back out of the function (see line 10), make sure it still prints { HAMSTER: 3, HORSE: 1 }.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Thanks for replying to each comment. The PR tidy-up is done too: title, task code and label are all sorted, and exercise 2 prints { HAMSTER: 3, HORSE: 1 } again.
Three things left before I can mark this Complete:
exercise-2.js:countAndCapitalisePetsonly counts pets that start with "h". The filter is now inside the function (line 10), so any other array gives the wrong answer. My reply on line 10 explains how to move it back out.exercise-1.jsprintsHi, Aman!. The starter printedhello, Aman!, and removing dead code shouldn't change the output. See my reply on line 6.- Prettier on
exercise-1.js. The file is missing the empty line at the end. Right click in the editor, choose Format Document, and save. To make this happen every time you save, follow the format on save steps here: https://github.com/CodeYourFuture/Module-JavaScript-Fundamentals/blob/main/practical_guide.md
Run node dead-code/exercise-1.js and node dead-code/exercise-2.js before you push and check they print exactly what main prints. Add the Needs Review label again once you've pushed.
Learners, PR Template
Self checklist
Task code
CYF-1066
Changelist
Unused code remove. Function refactored to continue to work for any input it's given.