Ask coding questions

← Back to all posts
0
What am i getting wrong?
ayormeday (7)

Add a method to the Person's prototype called "getInitials" that returns the first letter of their first and last name, both capitalized.

function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
person.prototype.getInitials = function (person){
var names = person.split(' '),
initials = names[0].substring(0, 1).toUpperCase();

if (names.length > 1) {
initials += names[names.length - 1].substring(0, 1).toUpperCase();
}
console.log(initials);
}

/ Do not modify code below this line /

const johnDoe = new Person('john', 'doe');
console.log(johnDoe.getInitials(), '<-- should be "JD"');

Answered by vedprad1 (461) [earned 5 cycles]
View Answer
Commentshotnewtop
2
vedprad1 (461)

It seems like you have several errors that you need to fix. Let's start from the top going down.

  1. Line 5: You said person. However, that refers to a variable. You need to capitalize it to get the object: Person.prototype....

  2. Line 5: The function getInitials should not take any parameters. Change the initialization to function () {...

  3. Line 6 to Line 11: I do not really understand the things going on here. Mainly, you cannot use split on an object. I would replace this entire code with:
var initials = this.firstName.charAt(0).toUpperCase() + this.lastName.charAt(0).toUpperCase();
  1. Line 12: You are going to log the output later, so you don't need to log it now. Use a return statement instead.

That should be perfect!

If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

1
ayormeday (7)

@vedprad1 thank you so much. This was really helpful

2
vedprad1 (461)

@ayormeday : Great! Could you please mark my answer as correct? Thanks!

1
ayormeday (7)

@vedprad1 is there anyway I can keep in touch with you on further problems?

2
vedprad1 (461)

@ayormeday : If you have other questions, you can post them here, at repl.it's ask page. You can also post it at my ask page: https://tinyurl.com/coders-ask

1
MIAlien (0)

@vedprad1 Can you help me with a problem ?

2
vedprad1 (461)

@MIAlien : You should post it in another post, because it is a different question.