Object Oriented Test

Object Oriented Test
0

I get this error after running my code “The toString method should return the correct heirachy (reply)”… Help is needed.

*Composition*

No replied to:
message + " by " + author.name.

With replied to:
message + " by " + author.name + " (replied to " + repliedTo.author.name + ")"
class User {
  constructor(name) {
    this._name = name;
    this._loggedIn = false;
    this._lastLoggedInAt = null;
  }
  isLoggedIn() {
    return this._loggedIn;
  }
  getLastLoggedInAt() {
    return this._lastLoggedInAt;
  }
  logIn() {
    this._lastLoggedInAt = new Date ();
    this._loggedIn = true;
  }
  logOut() {
    this._loggedIn = false;
  }
  getName() {
    return this._name;
  }
  setName(name) {
    this._name = name;
  }
  canEdit(comment) {
    if (comment._author._name === this._name) {
      return true;
    }
    return false;
  }
  canDelete(comment) {
    return false;
  }
}

class Moderator extends User {
  constructor (name) {
    super (name);
  }
  canDelete (comment) {
    return true;
  }
}

class Admin extends Moderator {
  constructor (name) {
    super(name)
    }
  canEdit (comment) {
    return true;
  }
}
class Comment {
  constructor (author = null, message, repliedTo = null) 
 {
    this._createdAt = new Date ();
    this._message = message;
    this._repliedTo = repliedTo;
    this._author = author;
  }
  getMessage () {
    return this._message;
  }
  setMessage (message) {
    this._message = message;
  }
    getCreatedAt () {
      return this._createdAt;
    }
  getAuthor () {
    return this._author;
  }
  getRepliedTo () {
    return this._repliedTo
  }
  toString () {
    if (this._repliedTo === null) {
      return this._message + " by " + 
        this._author._name
    }
    return this._message + " by " + this._author._name +
      "(repliedTo " +
        this._repliedTo._author._name + ")"
  }
}

In javascript the data type of null is an object. Try not strict comparison:
if(this._repliedTo == null)
or initialize repliedTo as empty string in constructor instead of null and, comapre _repliedTo with empty string:
if(this._repliedTo.equals(" "))

One way of testing for null values is to use !variable. This evaluates to true if the variable is null so you can think of this as isNull(variable).

Like any boolean you can invert it, so !!variable would similar to isNotNull(varable), which I’ve used below:

  toString() {
    const repliedTo = !!this._repliedTo
      ? ` (repliedTo ${this._repliedTo._name})`
      : ''
    return `${this._message} by ${this._author}${repliedTo}`
  }

So, if repliedTo is not null the user information is filled in otherwise it’s set as blank.

-Edit-
Notice that this._repliedTo._author._name should be this._repliedTo._name.

There’s really no need to coerce anything into a boolean using !! when you’re only using it as the condition of a ternary. The ternary operator will test its first operand for truthiness anyway.