Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions challenge-3/submissions/AlexO-85/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import "fmt"

type Employee struct {
ID int
Name string
Age int
Salary float64
}

type Manager struct {
Employees []Employee
}

// AddEmployee adds a new employee to the manager's list.
func (m *Manager) AddEmployee(e Employee) {
m.Employees = append(m.Employees, e)
}

// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
for i, e := range m.Employees {
if(e.ID == id){
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
break;
}
}
}

// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {

countEmployees := len(m.Employees)
sumSalary := float64(0)

if(countEmployees == 0){
return sumSalary
}

for _, e := range m.Employees {
sumSalary += e.Salary
}

avgSalary := sumSalary / float64(len(m.Employees))

return avgSalary
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for _, e := range m.Employees {
if(e.ID == id){
return &e
}
}
return nil
}
Comment on lines +50 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Fix loop variable address capture bug.

Line 54 contains a critical bug: returning &e captures the address of the loop variable, not the actual employee in the slice. In Go, the loop variable e is reused for each iteration, so its address remains the same and points to a temporary copy. This will lead to incorrect or unpredictable behavior.

Additionally, Line 53 has unnecessary parentheses around the if condition.

Apply this diff to fix both issues:

 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for _, e := range m.Employees {
-	    if(e.ID == id){
-	        return &e
+	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/AlexO-85/solution-template.go around lines 50 to 58,
the function returns &e which captures the loop variable's address and also
contains unnecessary parentheses in the if; to fix, iterate with index (for i :=
range m.Employees) or use for i, e := range m.Employees and return
&m.Employees[i] instead of &e, and remove the extraneous parentheses around the
if condition so it reads if e.ID == id { ... }.


func main() {
manager := Manager{}
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
manager.RemoveEmployee(1)
averageSalary := manager.GetAverageSalary()
employee := manager.FindEmployeeByID(2)

fmt.Printf("Average Salary: %f\n", averageSalary)
if employee != nil {
fmt.Printf("Employee found: %+v\n", *employee)
}
}
Loading