Skip to content
13 changes: 12 additions & 1 deletion src/inverseRobot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
*/

function inverseRobot(robot) {

Choose a reason for hiding this comment

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

It's a good practice to add a function description using JSDoc comments. This helps other developers understand what the function does, its parameters, and its return value.

Choose a reason for hiding this comment

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

It's a good practice to add a brief comment describing what your function does. This will help other developers understand your code more easily.

Choose a reason for hiding this comment

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

Consider adding a check to ensure that the 'robot' parameter is indeed an object and not null or undefined. This can prevent potential runtime errors if the function is called with invalid arguments.

Choose a reason for hiding this comment

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

The function 'inverseRobot' is correctly named and describes its purpose well.

// write code here
const clone = {};

Choose a reason for hiding this comment

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

The variable name 'clone' could be more descriptive. Consider renaming it to 'invertedRobot' or something similar to reflect its purpose of storing the inverted key-value pairs.

Choose a reason for hiding this comment

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

The variable name 'clone' could be more descriptive. Consider renaming it to 'invertedRobot' or something similar to reflect its purpose of storing the inverted key-value pairs of the 'robot' object.

Choose a reason for hiding this comment

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

The variable name 'clone' could be more descriptive. Consider renaming it to 'invertedRobot' to better reflect its purpose as the inverse of the input 'robot'.


for (const key in robot) {

Choose a reason for hiding this comment

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

It's recommended to use Object.keys() or Object.entries() to iterate over an object. This way, you don't have to worry about properties that come from the object's prototype.

Choose a reason for hiding this comment

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

Consider using Object.keys(robot) or Object.entries(robot) to iterate over the properties of the robot object. This approach avoids iterating over properties in the prototype chain and is generally considered a best practice when you only want to deal with the object's own properties.

const newKey = robot[key];

if (clone.hasOwnProperty(newKey)) {

Choose a reason for hiding this comment

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

Instead of using 'hasOwnProperty', which is an older approach, you can use the 'in' operator or 'Object.prototype.hasOwnProperty.call' for checking property existence to avoid issues with properties that may shadow object's own properties.

Choose a reason for hiding this comment

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

Consider using 'Object.prototype.hasOwnProperty.call(clone, newKey)' instead of 'clone.hasOwnProperty(newKey)' to avoid issues that may arise if the object has a property with the name 'hasOwnProperty'. This is a more robust way to check for property existence.

Choose a reason for hiding this comment

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

The method 'hasOwnProperty' is a safe way to check for property existence, but it's recommended to use 'Object.prototype.hasOwnProperty.call(clone, newKey)' to avoid issues in cases where the object might have a property with the name 'hasOwnProperty'.

Choose a reason for hiding this comment

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

Instead of using 'hasOwnProperty', it's recommended to use 'Object.prototype.hasOwnProperty.call' to avoid issues if the 'clone' object has a property with the name 'hasOwnProperty'.

return null;
}
clone[newKey] = key;
Comment on lines +12 to +18

Choose a reason for hiding this comment

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

It's good practice to check if the property belongs to the object itself and is not inherited from the prototype chain. You can add a check using 'robot.hasOwnProperty(key)' inside the loop.

}

Choose a reason for hiding this comment

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

InversedRobot, use inversedRobot instead of robot.


return clone;
}

module.exports = inverseRobot;

Choose a reason for hiding this comment

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

It's good practice to include a newline at the end of the file. Some tools and systems expect or require it for proper processing.