-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
added initial solution #2676
base: master
Are you sure you want to change the base?
added initial solution #2676
Changes from 17 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,17 @@ | |
*/ | ||
|
||
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 invertedRobot = {}; | ||
|
||
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 |
||
if (robot[key] in invertedRobot) { | ||
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 check for property presence is correct, but it's worth noting that using 'in' will check the entire prototype chain. If you want to check only the object's own properties, use 'robot.hasOwnProperty(key)'. 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 check for property presence should be done using 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 explicitly compare the property to 'undefined' to check for its existence. This makes the intention clearer and the code more readable. 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 check 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 check 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 use 'hasOwnProperty' method to check for property existence to avoid issues with properties from the prototype chain. Consider using 'invertedRobot.hasOwnProperty(robot[key])' instead of 'robot[key] in invertedRobot'. 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 check 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 check |
||
return null; | ||
} | ||
|
||
invertedRobot[robot[key]] = key; | ||
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. When assigning the key as the value and the value as the key, it's important to ensure that the value is actually a string that can be used as a property name. If the value is not a string (e.g., a number, an object, etc.), it should be converted to a string to maintain the integrity of the object structure. 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 convert the key to a string before using it as a property name to ensure that the object keys are consistent, especially when dealing with non-string 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.hasOwnProperty()' to check if the property exists on 'invertedRobot'. Directly using 'in' might lead to unexpected results if the object has inherited 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. When assigning 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. When assigning the key to the invertedRobot, it's a good practice to ensure that the value is a valid identifier or use bracket notation. Since the value of 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. When assigning the key to the invertedRobot, it's safer to ensure that the key is a string. Since object keys are always strings, if the value is not a string, it could lead to unexpected behavior. Use 'String(key)' to convert it explicitly. 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. When assigning the key to the 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. When assigning the key to the value in |
||
} | ||
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 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. try to avoid using nested for loops 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.hasOwnProperty()' to check if a property exists on an object. While using 'in' is not incorrect, 'hasOwnProperty()' does not check down the object's prototype chain, which can be safer for property existence checks. |
||
|
||
return invertedRobot; | ||
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 correctly returns the 'invertedRobot' object if no duplicate values are found in the input '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. Returning the invertedRobot object is correct. The function will return either |
||
} | ||
|
||
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.