Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions dead-code/exercise-1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being far from home is tough, but every line of code you write is a step closer to building the future you deserve—keep going!

  1. What happens in JavaScript when you use let to declare testName twice in the same scope?
  2. Inside sayHello, what happens to any code placed after the return statement, and is greetingStr used anywhere else in that function?

Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// Find the instances of unreachable and redundant code - remove them!
// The sayHello function should continue to work for any reasonable input it's given.

let testName = "Jerry";
let testName = "Aman";
const greeting = "hello";

function sayHello(greeting, name) {
const greetingStr = greeting + ", " + name + "!";
return `${greeting}, ${name}!`;
console.log(greetingStr);
}

testName = "Aman";

const greetingMessage = sayHello(greeting, testName);

console.log(greetingMessage); // 'hello, Aman!'
6 changes: 1 addition & 5 deletions dead-code/exercise-2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To help refine it:

Did you notice any duplicate declarations for petsStartingWithH in the code?

Are capitalisedPets and logPets used anywhere to achieve the final countedPetsStartingWithH output?

Being far from home is tough, but your persistence and focus are building a brilliant path forward—keep pushing!

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
// 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"];
const capitalisedPets = pets.map((pet) => pet.toUpperCase());
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");

function logPets(petsArr) {
petsArr.forEach((pet) => console.log(pet));
}
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");

function countAndCapitalisePets(petsArr) {
const petCount = {};
Expand Down
9 changes: 8 additions & 1 deletion practice-tdd/count.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for sharing your code—it's wonderful to see the loop and counter logic set up so cleanly!

If we look closely at the very beginning of the function, what do you think happens when execution encounters return 5 right away? How might that affect test cases, like an empty string, that expect a count of 0?

Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

for (const character of stringOfCharacters) {
if (character === findCharacter) {
count++;
}
}
return count;
}
15 changes: 15 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count occurrences in a mixed string", () => {
const count = countChar("banana", "a");
expect(count).toEqual(3);
});

test("should return 0 for an empty string", () => {
const count = countChar("", "a");
expect(count).toEqual(0);
});

test("should return 0 when character does not occur", () => {
const count = countChar("hello", "z");
expect(count).toEqual(0);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down