@@ -3,63 +3,80 @@ pragma solidity ^0.8.0;
3
3
4
4
contract Assignment5 {
5
5
// 1. Declare a public unsigned integer variable called `counter`
6
-
6
+ uint public counter;
7
7
// 2. Declare a public boolean variable called `isActive`
8
-
8
+ bool public isActive;
9
9
// 3. Create a public mapping from an address to a string called `userNames`
10
-
10
+ mapping ( address => string ) userNames;
11
11
// 4. Define a struct `User` with fields: `id` (uint), `name` (string)
12
-
12
+ struct User {
13
+ uint id;
14
+ string name;
15
+ }
13
16
// 5. Create a public array of `User` called `users`
17
+ User[] public users;
14
18
15
19
// Constructor
20
+
16
21
// Initialize `isActive` to true
17
22
constructor () {
23
+ isActive = true ;
18
24
// Fill in the logic
19
-
20
25
}
21
26
22
27
// Function to increment the counter
23
28
// This function should:
24
29
// - Be external
25
30
// - Use a for loop to increment `counter` by 1 for 5 iterations
26
- function incrementCounter () {
31
+ function incrementCounter () external {
32
+ for (uint i = 0 ; i < 5 ; i++ ) {
33
+ counter = i + 1 ;
34
+ }
35
+
27
36
// Fill in the logic
28
-
29
37
}
30
38
31
39
// Function to toggle `isActive`
32
40
// This function should:
33
41
// - Be public
34
42
// - Use an if statement to toggle `isActive` between true and false
35
- function toggleActive () {
43
+ function toggleActive () public {
36
44
// Fill in the logic
37
-
45
+ if (isActive) {
46
+ isActive = false ;
47
+ } else {
48
+ isActive = true ;
49
+ }
38
50
}
39
51
40
52
// Function to add a user
41
53
// This function should:
42
54
// - Be public
43
55
// - Take three parameters: `id` (uint) and `name` (string) and userAddress (address)
44
- // Hint: Define string as `string memory`
45
- // The memory keyword will be explained later
56
+ // Hint: Define string as `string memory`
57
+ // The memory keyword will be explained later
46
58
// - Add a new user to the `users` array
47
59
// - Update the `userNames` mapping with the address and name
48
- function addUser () {
49
- // Fill in the logic
50
-
60
+ function addUser ( uint id ,string memory name , address userAddress ) public {
61
+ // Fill in the logic
51
62
// Hint: you have to use the keyword `memory` to define the struct
52
- // For example: User memory user
53
- // This keyword will be explained later
63
+ // For example: User memory user
64
+ // This keyword will be explained later
65
+
66
+ User memory newUser = User (id,name);
54
67
68
+ users.push (newUser);
69
+
70
+ userNames[userAddress] = name;
71
+
55
72
}
56
73
57
74
// Function to get the total number of users
58
75
// This function should:
59
76
// - Be public and view
60
77
// - Return the length of the `users` array
61
- function getUserCount () {
78
+ function getUserCount () public view returns ( uint ) {
62
79
// Fill in the logic
63
-
80
+ return users. length ;
64
81
}
65
82
}
0 commit comments