@@ -15,3 +15,31 @@ testName = "Aman";
1515const greetingMessage = sayHello ( greeting , testName ) ;
1616
1717console . 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