1

The task:

  1. declare a variable called myName that returns an array of two elements, firstname and last name.

  2. declare a function called join(), it will return the two elements as a string with a space between each element.

  3. declare a function createProfile that should expect an array "name" as input(???).

  4. createProfile creates and returns an object with two key-value pairs. key: name value: use the function "join()" and the parameter "name" variable to return a string with your full name, separated by a space

    key: email value: my email as a string.

  5. Declare a variable called myProfile and assign the result of calling createProfile with myName.

The problem is in step 4. I believe I should use an object constructor. I don't know how to properly include the function join() a value in a key-value pair in an object constructor.

Here is what I have tried so far:


const myName = ["firstname", "lastname"];

function join(){
    document.write("Hello, my name is " + myName[0] + " " + myName[1] + ".");
};

join();


function createProfile(name, email){
    this.name = function(){
        return this.join();
    };
    this.email = email;
    return this;
}

let myProfile = new createProfile(name,"me@gmail.com");
console.log(myProfile);

Expected results: calling create profile should log an object with my full name as a string, and my email address as a string.

Actual results:

createProfile {name: ƒ, email: "hotcoffeehero@gmail.com"}
email: "hotcoffeehero@gmail.com"
name: ƒ ()
__proto__: Object

The value of name is an empty function.

How do I display it as a string?

Onegai Shimasu

yqlim
  • 6,898
  • 3
  • 19
  • 43
  • First you do not have `join` on `this`. Second, you can look into `getter` properties for that – Rajesh Aug 23 '19 at 06:13
  • function createProfile(name, email){ this.name = function(){ return this.join(); }; I thought that is what this was doing. – hotcoffeehero Aug 23 '19 at 06:27
  • If I understand correct, all you have to do is `createProfile(join(), 'email')` and in function, `this.name = name` – Rajesh Aug 23 '19 at 06:59

1 Answers1

0

if you want to use your logic, you need fix the code with the next code

const myName = ["firstname", "lastname"];

function join(){
    //document.write("Hello, my name is " + myName[0] + " " + myName[1] + ".");
    return myName[0] + ' ' + myName[1] // You only return the values
};

join();


function createProfile(name, email){
    this.name = join(); // you call the function and assing to name
    this.email = email;
    return this;
}

const myProfile = new createProfile(name,"me@gmail.com");
console.log(myProfile);

but there are better ways to development this

David
  • 61
  • 4
  • Thanks David! That is really helpful. It is intentionally convoluted code. The task is actually the application for an immersive coding program so they made it like this on purpose. I was having trouble with that one part. Cheers! – hotcoffeehero Aug 24 '19 at 07:49