-
-
Notifications
You must be signed in to change notification settings - Fork 634
Add solution for Challenge 3 by Be1chenok #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add solution for Challenge 3 by Be1chenok #581
Conversation
WalkthroughAdds a new Go program defining Employee and Manager types with methods to add/remove employees, compute average salary, and find employees by ID. Includes a main function demonstrating these operations by creating a Manager, manipulating employees, and printing results. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant main as main()
participant mgr as Manager
participant emp as Employee
User->>main: Run program
note over main: Initialize Manager
main->>mgr: AddEmployee(emp1)
main->>mgr: AddEmployee(emp2)
main->>mgr: RemoveEmployee(id=...)
main->>mgr: GetAverageSalary()
mgr-->>main: avgSalary
main->>mgr: FindEmployeeByID(id=...)
alt Found
mgr-->>main: *Employee
else Not found
mgr-->>main: nil
end
main-->>User: Print results
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
for _, e := range m.Employees { | ||
if e.ID == id { | ||
return &e | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix pointer-to-range-variable bug in FindEmployeeByID
.
return &e
hands back a pointer to the loop’s iteration variable, which is a copy and becomes invalid once the loop advances—callers will either read stale data or the last element inspected. Return the address of the slice element instead.
-func (m *Manager) FindEmployeeByID(id int) *Employee {
- for _, e := range m.Employees {
- if e.ID == id {
- return &e
- }
+func (m *Manager) FindEmployeeByID(id int) *Employee {
+ for i := range m.Employees {
+ if m.Employees[i].ID == id {
+ return &m.Employees[i]
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
for _, e := range m.Employees { | |
if e.ID == id { | |
return &e | |
} | |
func (m *Manager) FindEmployeeByID(id int) *Employee { | |
for i := range m.Employees { | |
if m.Employees[i].ID == id { | |
return &m.Employees[i] | |
} | |
} | |
return nil | |
} |
🤖 Prompt for AI Agents
In challenge-3/submissions/Be1chenok/solution-template.go around lines 57 to 60,
the function currently returns a pointer to the range loop variable (return &e),
which yields a pointer to a copy and is invalid after the iteration; change the
loop to use the slice index (for i := range m.Employees or for i, e := range
m.Employees) and return the address of the actual slice element (e.g.
&m.Employees[i]) so callers receive a valid pointer; keep the function’s
not-found behavior (return nil) unchanged.
Challenge 3 Solution
Submitted by: @Be1chenok
Challenge: Challenge 3
Description
This PR contains my solution for Challenge 3.
Changes
challenge-3/submissions/Be1chenok/solution-template.go
Testing
Thank you for reviewing my submission! 🚀