|
1 | 1 | // Find the instances of unreachable and redundant code - remove them! |
2 | 2 | // The sayHello function should continue to work for any reasonable input it's given. |
3 | 3 |
|
4 | | -let testName = "Jerry"; |
| 4 | + |
| 5 | +// DEAD CODE |
| 6 | +// let testName = "Jerry"; |
| 7 | +// const greeting = "hello"; |
| 8 | + |
| 9 | +// function sayHello(greeting, name) { |
| 10 | +// // const greetingStr = greeting + ", " + name + "!"; |
| 11 | +// return `${greeting}, ${name}!`; |
| 12 | +// console.log(greetingStr); |
| 13 | +// } |
| 14 | + |
| 15 | +// // testName = "Aman"; |
| 16 | + |
| 17 | +// const greetingMessage = sayHello(greeting, testName); |
| 18 | + |
| 19 | +// console.log(greetingMessage); // 'hello, Aman!' |
| 20 | + |
| 21 | +/** |
| 22 | +"Clean Code Refactoring: SayHello Function" |
| 23 | +
|
| 24 | +Removed Dead Data: Eliminated the unreachable variable value "Jerry" to keep memory clean. |
| 25 | +Enforced Immutability: Switched from let to const for variables that do not change, preventing accidental reassignments. |
| 26 | +Optimized Variable Scope: Reorganized the declaration order to ensure variables are defined before they are called. |
| 27 | +Simplified Function Logic: Removed redundant intermediate variables (greetingStr) inside the function to return the result directly. |
| 28 | +Streamlined Output: Removed internal console.log statements after testing to keep the console clean and focused on the final output. |
| 29 | +Execution: Properly invoked the function by passing the correct parameters for the final print. |
| 30 | +*/ |
| 31 | + |
5 | 32 | const greeting = "hello"; |
| 33 | +const testName = "Aman"; |
6 | 34 |
|
7 | 35 | function sayHello(greeting, name) { |
8 | | - const greetingStr = greeting + ", " + name + "!"; |
9 | 36 | return `${greeting}, ${name}!`; |
10 | | - console.log(greetingStr); |
| 37 | + |
11 | 38 | } |
12 | 39 |
|
13 | | -testName = "Aman"; |
14 | | - |
15 | | -const greetingMessage = sayHello(greeting, testName); |
16 | | - |
17 | | -console.log(greetingMessage); // 'hello, Aman!' |
| 40 | +console.log(sayHello(greeting, testName)); // 'hello, Aman!' |
0 commit comments