Short URL: https://bit.ly/oop-challenge
Add your answers should follows this conventions:
- File name must use number of question, but with
.js
extension insrc
folder. For example, if the question file name is01.md
, then your answer must be01.js
. - The answer should have tests.
In this challenge you have freedom to design the class.
I provide the example of code, but you could create your own implementation.
For example I want to create User
class.
That class should be able create User
object with username
, name
, email
, and password
.
Here is my first approach.
class User {
constructor(username, name, email, password) {
this.username = username
this.name = name
this.email = email
this.password = password
}
}
// Usage
const user = new User('mul14', 'Mulia', 'email@example.com', 'S3cR3T')
My second approach.
class User {
setUsername(username) {
this.username = username
}
setName(name) {
this.name = name
}
setEmail(email) {
this.email = email
}
setPassword(password) {
this.password = password
}
}
// Usage
const user = new User()
user.setUsername('mul14')
user.setName('Mulia')
user.setEmail('email@example.com')
user.setPassword('S3cR3T')
The both approaches are acceptable. When design a class you should think it first, "Is this a good way to design a class? Is it easy enough for use?".