0

I am using codecademy to train, however I noticed that the expected result was different than what the completion notes suggested it would be, even though I got passing marks.

Instructions

1. This is a great way to make objects more flexible. Under the person object, write another variable named friend and set it equal to an object with one key named name. The value of name should be your friend's name.

You can create another object literal like this:

let friend = {
    name: 'name here'
};

2. Under the friend object, set friend.sayHello equal to person.sayHello.

friend.sayHello = person.sayHello;

This will add a key named sayHello to the friend object and set it equal to the method inside of person.sayHello. The person.sayHello method uses the this keyword. What will this be when the friend object calls it?

3. At the bottom of your main.js, use console.log() to print the output of calling friend.sayHello() and notice the output in the console.

It logged your friend's name instead of yours because the meaning of this changed to the friend object, for which the name key is different.

My code

let day = 'Wednesday';
let alarm;
let person = {
  name: 'William',
  age: 29,
  weekendAlarm: 'No alarms needed',
  weekAlarm: 'Alarm set to 7AM',
  sayHello: () => {
    return 'Hello, my name is ${this.William}';
  },
  sayGoodbye(){
    return 'Goodbye!'
  }
};
//Friend
let friend = {
  name: 'Preston'
};
//changing functions
friend.sayHello = person.sayHello;
if (day === 'Saturday' || day === 'Sunday') {
  alarm = 'weekendAlarm';}
else {
  alarm = 'weekAlarm';
}
console.log(person['name']);
console.log(person['age']);
console.log(person[alarm]);
person.hobbies = ['Pimping', 'Poon Slaying'];
person.hobbies = 'slayin hoes';
console.log(person['hobbies']);
console.log(person.sayHello());
//logging friend to console.log
console.log(friend.sayHello());

My question:

The instructions suggest the final "Hello, my name is..." should be "Hello, my name is Preston."

My code doesn't do that. Did I do something wrong? Or if the learning machine isn't up to date in standards, should I need to report it as a bug?

Your contribution in this post may go further than just helping me.

trincot
  • 211,288
  • 25
  • 175
  • 211

2 Answers2

0

The reason your code "works" as it does is for three reasons:

  • you use an arrow function for defining sayHello: that has a different way of working with this. In your case it will be undefined (or the global object in non strict mode). You need to use the function keyword to define it (or the ES6 shorthand syntax -- see below).

  • A template literal (with ${ }) needs backticks as delimiter, not normal quotes. As you have it now it will just take thos ${ } as literal text.

  • this.William references a property that does not exist, it should be this.name.

Relevant part of the corrections:

let person = {
    name: 'William',
    sayHello() {
        return `Hello, my name is ${this.name}`;
    }
};
// Friend
let friend = {
    name: 'Preston'
};
// Assigning functions
friend.sayHello = person.sayHello;

// Logging friend to console.log
console.log(friend.sayHello());
trincot
  • 211,288
  • 25
  • 175
  • 211
  • following your instructions I was able to achieve the desired effects here is the code now I actually only needed to change the sayHello key: property as you suggested sayHello() { return `Hello, my name is ${this.name}`; } and the code worked as intended! Thank you. I knew better than to write this.william but I guess I wasn't thinking clearly... idk. the backticks i didn't know and I guess I didn't notice the difference. you have taught me something! Thank you! – William Bailey Apr 06 '18 at 09:35
  • One additional note. I noticed you didn't actually use the keyword function. If I recall that is the difference in Strict, and not? I did as you, and the code worked perfectly. Thank you again. – William Bailey Apr 06 '18 at 09:39
  • About the `function` keyword. The syntax I used is the ES6 shorthand for the same. So within an object literal (or class descriptor) `sayHello() {` is short for `sayHello: function () {`. There is no difference in how that is interpreted. You can choose either way. – trincot Apr 06 '18 at 15:08
0

There are several issues with your code.

1, The arrow function does not has "this"

sayHello: () => {
  return 'Hello, my name is ${this.William}';
},

You must use short syntax like the way you defined sayGoodbye, or use traditional syntax function(){...}

2, Wrong template literal syntax

Not ', but '`'

3, Wrong variable name:

return 'Hello, my name is ${this.William}';

The correct is this.name

So, the correct version is:

let person = {
  name: 'William',
  age: 29,
  ...
  sayHello() {
    return `Hello, my name is ${this.name}`;
  },
  ...
};
Dong Nguyen
  • 1,185
  • 7
  • 17
  • oh man, that backtick thing was already in the predefined code codecademy produces. I am very concerned now! hope I am not learning from the wrong place! Thank you so much for your input! you're right it does need to be a backtick. i haven't checked the rest yet. I'm going over everything. – William Bailey Apr 06 '18 at 09:11
  • i think someone changed it to this.william I think it was originally this.name ... i know better than that >.> – William Bailey Apr 06 '18 at 09:16
  • Glad to help. I'm following several online courses too. That may be the trap of the instructor :) – Dong Nguyen Apr 06 '18 at 09:22
  • ha, nope i really am that much of an idiot. I think maybe I did name it this.william – William Bailey Apr 06 '18 at 09:27
  • I stand corrected. I didn't notice it was backticks so I was completing the stages with ' ' I guess it didn't catch up to me until later. – William Bailey Apr 06 '18 at 09:33