INDAPlus21 / INDAPlus21/eliased-sockets-task-14
Pass
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
**Very well done Elias!**
But please, name your repositories according to the instructions!
Professional tip: Authorisation keys should never be included in the repository. The reason is that API services often has a free limit or are not free. API services also tend to have special user conditions for key sharing. What you could have done is to save the key in a `.env` file and add the file to `.gitignore`. Then you could have added instructions of how to get and include the key in the project.
Nice code (expect all out-commented code), could have used some documentation and cleanup, but works as intended. You must have been tired, because:
_Your code (excluding console statements)_:
```js
socket.addEventListener('message', function (event) {
var message = event.data
if (message == "is_bot == true") {
var change_to
if (guess_is_bot) change_to = "You guessed right"
else change_to = "You guessed wrong"
if (guess_is_bot) document.getElementById("is_bot").innerHTML = change_to
else {
document.getElementById("is_human").innerHTML = change_to
}
} else if (message == "is_bot == false") {
var change_to
if (!guess_is_bot) change_to = "You guessed right"
else change_to = "You guessed wrong"
if (guess_is_bot) document.getElementById("is_human").innerHTML = change_to
else document.getElementById("is_human").innerHTML = change_to
} else {
//...
}
});
```
_Refactored version 1_:
```js
const WRONG_GUESS = "You guessed wrong";
const RIGHT_GUESS = "You guessed right";
socket.addEventListener('message', ({ data }) => {
switch (data) {
case 'is_bot == true':
if (guess_is_bot)
document.getElementById("is_bot").innerHTML = RIGHT_GUESS
else
document.getElementById("is_human").innerHTML = WRONG_GUESS
break;
case 'is_bot == false':
if (guess_is_bot)
document.getElementById("is_human").innerHTML = WRONG_GUESS
else
document.getElementById("is_human").innerHTML = RIGHT_GUESS
break;
default:
//...
}
});
```
_Refactored version 2_:
```js
const WRONG_GUESS = "You guessed wrong";
const RIGHT_GUESS = "You guessed right";
socket.addEventListener('message', ({ data }) => {
switch (true) {
case (data === 'is_bot == true' && guess_is_bot):
document.getElementById("is_bot").innerHTML = RIGHT_GUESS;
break;
case (data === 'is_bot == true' && !guess_is_bot):
case (data === 'is_bot == false' && guess_is_bot):
document.getElementById("is_human").innerHTML = WRONG_GUESS;
break;
case (data === 'is_bot == false' && !guess_is_bot):
document.getElementById("is_human").innerHTML = RIGHT_GUESS;
break;
default:
//...
}
});
```
_Refactored version 3_:
```js
const WRONG_GUESS = "You guessed wrong";
const RIGHT_GUESS = "You guessed right";
socket.addEventListener('message', ({ data }) => {
if (data === 'is_bot == true' && guess_is_bot)
document.getElementById("is_bot").innerHTML = RIGHT_GUESS
else if ((data === 'is_bot == true' && !guess_is_bot) ||
(data === 'is_bot == false' && guess_is_bot))
document.getElementById("is_human").innerHTML = WRONG_GUESS
else if (data === 'is_bot == false' && !guess_is_bot)
document.getElementById("is_human").innerHTML = RIGHT_GUESS
else
//...
});
```
Version 2 & 3 both have extra boolean calculations, while version 1 has a pair of duplicated lines. The choice is personal preference.
You also seem to know legacy JS. A couple of examples:
- Anonomus functions:
- Legacy: `function() {}`
- Lambda: `() => {}
- Variable declarations:
- Legacy: `var`
- Reassignable: `let`
- Non-reassignable: `const`
Keep it up!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository files or tests are named. Start by locating the JavaScript socket-message handler and checking the repository's naming, credential, documentation, and cleanup concerns; done would require a clearly scoped decision on which of these feedback items to address.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, security, web-dev
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100