Skip to content

Commit 87c006b

Browse files
committed
first Solidity commit
1 parent c6c6f94 commit 87c006b

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/Assignment5.sol

+35-18
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,80 @@ pragma solidity ^0.8.0;
33

44
contract Assignment5 {
55
// 1. Declare a public unsigned integer variable called `counter`
6-
6+
uint public counter;
77
// 2. Declare a public boolean variable called `isActive`
8-
8+
bool public isActive;
99
// 3. Create a public mapping from an address to a string called `userNames`
10-
10+
mapping(address => string) userNames;
1111
// 4. Define a struct `User` with fields: `id` (uint), `name` (string)
12-
12+
struct User {
13+
uint id;
14+
string name;
15+
}
1316
// 5. Create a public array of `User` called `users`
17+
User[] public users;
1418

1519
// Constructor
20+
1621
// Initialize `isActive` to true
1722
constructor() {
23+
isActive = true;
1824
// Fill in the logic
19-
2025
}
2126

2227
// Function to increment the counter
2328
// This function should:
2429
// - Be external
2530
// - 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+
2736
// Fill in the logic
28-
2937
}
3038

3139
// Function to toggle `isActive`
3240
// This function should:
3341
// - Be public
3442
// - Use an if statement to toggle `isActive` between true and false
35-
function toggleActive() {
43+
function toggleActive() public {
3644
// Fill in the logic
37-
45+
if (isActive) {
46+
isActive = false;
47+
} else {
48+
isActive = true;
49+
}
3850
}
3951

4052
// Function to add a user
4153
// This function should:
4254
// - Be public
4355
// - 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
4658
// - Add a new user to the `users` array
4759
// - 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
5162
// 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);
5467

68+
users.push(newUser);
69+
70+
userNames[userAddress] = name;
71+
5572
}
5673

5774
// Function to get the total number of users
5875
// This function should:
5976
// - Be public and view
6077
// - Return the length of the `users` array
61-
function getUserCount() {
78+
function getUserCount() public view returns(uint) {
6279
// Fill in the logic
63-
80+
return users.length;
6481
}
6582
}

0 commit comments

Comments
 (0)