-
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 15 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 (invertedRobot.hasOwnProperty(robot[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 use 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. The use of 'hasOwnProperty' is correct for checking if a property exists in an object. However, consider using 'Object.prototype.hasOwnProperty.call(invertedRobot, robot[key])' to avoid potential issues if the 'robot' object has its own 'hasOwnProperty' property. 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 use of 'hasOwnProperty' is correct for checking if a property exists on an object. However, since ECMAScript 5, it's recommended to use 'Object.prototype.hasOwnProperty.call(invertedRobot, robot[key])' to avoid issues when the object has a property with the name 'hasOwnProperty'. |
||
return null; | ||
} | ||
|
||
invertedRobot[String(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. The explicit conversion to 'String' is not necessary because object keys are always strings in JavaScript. If the value is not a string, it will be coerced to a string automatically. 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 explicit conversion to String is not necessary when setting the property on 'invertedRobot'. Object keys are automatically converted to strings in JavaScript, so you can directly use '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. The use 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. It's good practice to check if 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 use 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. 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. Using 'String(robot[key])' is redundant since object keys are always strings. You can directly use 'robot[key]' as the key for '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. 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 |
||
|
||
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.