AI Task - Software Engineering
1. I chose ChatGPT for this task because it consistently outperforms
other AI tools in code quality, reasoning depth, and developer
productivity. Among alternatives like Gemini, Claude, and Grok, ChatGPT
was the most reliable and effective for:
● Doesn't just write code it explains why and how it works,
great for learning and debugging.
● Quickly generate working code (while tools gemini can take
a few seconds and time is important during an
assessment).
● Also ChatGPT has better context awareness in my opinion
2.
a. Prompt used for refactoring code: Refactor the following
JavaScript function to improve readability and maintainability. Use
modern ES6+ language features like arrow functions, reduce, and
default parameters. Also, include proper JSDoc comments with
type annotations for the input and output types.
b. Prompt used for generating unit tests: Write a suite of Jest unit
tests for the refactored countByRole function. Include tests for an
empty input array, users with and without the role property,
multiple users with the same and different roles, and unusual or
malformed data like undefined items in the array.
3.
Refactored code
/**
* @typedef {Object} User
* @property {number} id - Unique user ID
* @property {string} name - User's name
* @property {string} [role] - Optional role of the user
*/
/**
* Counts the number of users for each role.
* If a user has no role defined, they are grouped under
'unknown'.
*
* @param {User[]} users - Array of user objects
* @returns {Record<string, number>} A map of role names
to their counts
*/
const countByRole = (users) => {
return users.reduce((acc, { role = 'unknown' }) => {
acc[role] = (acc[role] || 0) + 1;
return acc;
}, {});
};
module.exports = { countByRole };
Generated test suite
const { countByRole } = require('./countByRole');
describe('countByRole', () => {
test('returns empty object for empty array', () =>
{
expect(countByRole([])).toEqual({});
});
test('counts roles correctly for users with defined
roles', () => {
const users = [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' },
{ id: 3, name: 'Charlie', role: 'admin' },
];
expect(countByRole(users)).toEqual({ admin: 2,
user: 1 });
});
test('treats missing role as unknown', () => {
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob', role: 'user' },
];
expect(countByRole(users)).toEqual({ unknown: 1,
user: 1 });
});
test('handles all users with unknown roles', () =>
{
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
];
expect(countByRole(users)).toEqual({ unknown: 2
});
});
test('throws if array includes undefined', () => {
const users = [
{ id: 1, name: 'Alice', role: 'admin' },
undefined,
{ id: 2, name: 'Bob' },
];
expect(() => countByRole(users)).toThrow();
});
});
5.
6. Using ChatGPT for both refactoring and test generation was smooth and
efficient. The AI produced clean, modern JavaScript code using reduce, arrow
functions, and default parameters. It also generated well-structured Jest tests
covering common and edge cases. The conversational format allowed me to
iterate quickly and adjust the output to match my coding style. Initially, I asked
for a simple refactor. To improve results, I added instructions to use ES6+
features and include full JSDoc annotations. For testing, I first requested
general unit tests, then clarified I wanted edge cases like empty arrays,
missing roles, and malformed input. This helped the AI produce more
comprehensive coverage. The AI didn’t initially handle undefined values in the
user array. I had to manually add a test case to confirm that such input throws
an error and ensure the behavior matched expectations. Other than that, the
refactored code and tests required only light validation, not major corrections.
Additional github repo of all source
https://github.com/shehanxdev/AI-assessment-for-eight25