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