-
Notifications
You must be signed in to change notification settings - Fork 2.4k
added initial solution #2676
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
base: master
Are you sure you want to change the base?
added initial solution #2676
Changes from all commits
efa2ecd
a946d53
6831bb9
0c9c947
b67f7e2
66f7d93
412eb24
2c949bd
07d32d3
727503c
a44140d
6320c61
b589d37
408ab78
fc864ed
297f3ed
c1b5609
0d59f06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,18 @@ | |
| */ | ||
|
|
||
| function inverseRobot(robot) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = {}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's recommended to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| const newKey = robot[key]; | ||
|
|
||
| if (clone.hasOwnProperty(newKey)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InversedRobot, use |
||
|
|
||
| return clone; | ||
| } | ||
|
|
||
| module.exports = inverseRobot; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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.
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.