Skip to content
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

add solutions to Objects and Classes chapter #164

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,58 @@ const annotateData = (array) => array.reduce(reducer, [])
const newData = annotateData(data)
```

## Objects and Classes

### Delays

```{js}
class Delay {
constructor(initialValue){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use a space between the method name and the opening parenthesis, and between the closing parenthesis and curly brace:

constructor (initialValue) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review and have acknowledged the required changes to be made 👍

this.nextValue = initialValue
}

call(nextValue) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Spacing as above please.

let previousValue = this.nextValue
this.nextValue = nextValue
return previousValue
}
}
```

### Filtering

```{js}
class Filter {
constructor(...values){
Copy link
Collaborator

Choose a reason for hiding this comment

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

spacing as above

this.filterValues = values
}

call(inputValue){
Copy link
Collaborator

Choose a reason for hiding this comment

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

spacing as above

return this.filterValues.some((value) => value === inputValue) ? null : inputValue
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is probably more efficient to use indexOf:

return this.filterValues.indexOf(value) === -1 ? inputValue : null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @gvwilson,
I performed some tests out of curiosity to learn more about your suggestion.
I did run a performance test. The results showed me that both are comparatively same.
Please advise.

}
}
```

### Pipelines

```{js}
class Pipeline {
constructor(...pipes){
Copy link
Collaborator

Choose a reason for hiding this comment

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

spacing as above

this.pipes = pipes
}

call(inputValue){
Copy link
Collaborator

Choose a reason for hiding this comment

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

spacing as above

let returnValue
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no need to introduce another variable: you can update inputValue repeatedly with inputValue = pipe.call(inputValue). (Although if you're going to do this, it should probably just be called value.)

for (let pipe of this.pipes){
returnValue = (() => pipe.call(inputValue))()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why introduce the anonymous function call? Why not simply value = pipe.call(value)? (Assuming you unify the two variable inputValue and returnValue as described above.)

if (!returnValue) break
inputValue = returnValue
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have we introduced break before this point in the text? If not, can you find a solution that does not use it?

return returnValue
}
}
```

## Data-Forge

### Revisting Data Manipulation
Expand Down