Skip to content

Commit 2d92940

Browse files
author
Ogbemi mene
committed
my exercise-1
1 parent d6b6037 commit 2d92940

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Sprint-3/3-dead-code/exercise-1.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,31 @@ testName = "Aman";
1515
const greetingMessage = sayHello(greeting, testName);
1616

1717
console.log(greetingMessage); // 'hello, Aman!'
18+
19+
// this is the correct code after removing unreachable and redundant code
20+
21+
const greeting = "hello";
22+
let testName = "Aman";
23+
24+
function sayHello(greeting, name) {
25+
// Directly returns the cleanly formatted string
26+
return `${greeting}, ${name}!`;
27+
}
28+
29+
const greetingMessage = sayHello(greeting, testName);
30+
31+
console.log(greetingMessage); // Output: 'hello, Aman!'
32+
33+
//reasons for the changes:
34+
35+
// 1. Unreachable Code: The console.log(greetingStr);
36+
// inside the function occurs after the return statement.
37+
// Once a function hits a return, it immediately exits,
38+
// making anything below it completely unreachable.
39+
40+
// 2. Redundant Code: The variable const greetingStr = greeting + ", " + name + "!";
41+
// was created using old string concatenation, but the function actually returns a template literal expression (${greeting}, ${name}!).
42+
// Since greetingStr is never used elsewhere, it can be deleted entirely.
43+
44+
// 3. Unused Global Variable: let testName = "Jerry"; is declared but immediately overwritten by testName = "Aman";
45+
// before ever being used, making the initial assignment redundant.

0 commit comments

Comments
 (0)