-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-task.js
74 lines (55 loc) · 2.2 KB
/
project-task.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
===========================================
🔁 Function Refactoring Activity
===========================================
🎯 Objective:
Students will identify repetitive or poorly organized code in a given script
and refactor it by creating programmer-defined functions.
This activity reinforces:
- Writing functions with no, single, and multiple parameters
- Using return statements effectively
- Improving code reusability and modularity
---
🧭 Instructions:
1️⃣ Analyze the provided script, which performs a series of repetitive tasks.
2️⃣ Identify sections of code that can be improved using functions.
3️⃣ Write reusable functions with appropriate parameters and return statements.
4️⃣ Refactor the original code to use your new functions.
5️⃣ Test the refactored script to ensure it produces the same output as the original.
---
*/
// ============================================
// ❌ Original Code (Before Refactoring)
// ============================================
// Script 1 - Greeting multiple users
console.log("Welcome, Alice!");
console.log("Welcome, Bob!");
console.log("Welcome, Charlie!");
// Script 2 - Sum calculation
let num1 = 5, num2 = 10;
let sum = num1 + num2;
console.log("The sum of 5 and 10 is " + sum);
// Script 3 - Product calculation
let product = num1 * num2;
console.log("The product of 5 and 10 is " + product);
// Script 4 - Print names from a list
let names = ["Alice", "Bob", "Charlie"];
console.log("Names in the list:");
for (let i = 0; i < names.length; i++) {
console.log(names[i]);
}
/*
===========================================
🛠️ Steps for Refactoring
===========================================
🔹 Break Down Tasks into Functions:
- Identify repetitive patterns (e.g., greetings, calculations, list printing)
- Define separate functions for each task
🔹 Write Functions with Parameters and Return Values:
- Parameterize functions for flexibility (e.g., pass in name, numbers, arrays)
- Use return statements where necessary
🔹 Refactor the Original Code:
- Replace repeated code with meaningful function calls
- Keep your code clean, readable, and easy to maintain
*/
// ✅ Your refactored code goes below this line!