The task:
declare a variable called myName that returns an array of two elements, firstname and last name.
declare a function called join(), it will return the two elements as a string with a space between each element.
declare a function createProfile that should expect an array "name" as input(???).
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.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.