Anyone got any advice for me?
// Exercise Two: You will be given an object called ‘userObj’
// userObject will already have a key on it called ‘name’
// Add a method to userObj, called ‘greeting’.
// Using the keyword ‘this’, the greeting method should return the following string:
// ‘Hi, my name is ’ and the users name.
// eg: If userObj has a name: ‘Dan’, greeting should return: Hi, my name is Dan’
// NOTE: DO NOT create a new object.
// NOTE: DO NOT create a key called name the key is already on the object.
function exerciseTwo(userObj){
return userObj;
}
Could you be more specific on what exactly you need help with? What have you tried so far, and what isn’t working?
thats the thing, its asking me to put in a method, I know about methods, but I don’t know how to implement it into the assignment
Think about how you add a method to an object that you are defining.
Then do the same thing to the object that you’re provided.
Firstly, welcome to the forums.
While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.
With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).
It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.
Please provide some example of what you’ve tried and I’m sure you’ll get more help.
Happy coding
sorry it wasn’t my intention to ask for the answers, I ment to ask how method is suppose to be used in this, ive done my research and most of the research ive done hasn’t brought me what I was looking for.
to use a method or a property, does there have to be a key:value in order to use methods?
If you share what you’ve tried so far, we can help you much more effectively.
function exerciseTwo(userObj){
name(greeting)
return userObj;
}
that’s all I can figure out
Let’s say I have an object called nhcarrigan.
let nhcarrigan = {
name: "Nicholas"
}
If I wanted to add a new key to that, I’d use dot notation to assign the key and the value to the object.
Are you familiar with dot notation?
yes but I don’t know what thatll do with my problem
Is this an assignment for your class?
its my pre course practice quiz
Well an object is basically variables with multiple values. It’s like for example you. You have a first name, a last name, a height, age, nickname, and such. So does objects. It’s like objects have characteristics. Everything inside an object variable is the characteristic of the object variable. So as @nhcarrigan already shown us. You can declare an object by doing:
let variable-name = {
object-name: "string",
object-name2: Boolean,
object-name3: 15,
object-name4: function () {
}
}
An object property can have a value of a string, a boolean, a number, and a method function. When you have multiple object properties, its always separated by commas (amount of object properties - 1 is how many comas you should have). Then since it has the characteristics (Property and Value), you can grab it. so:
console.log(variable-name.object-name1); //this will output "string"
1 Like
what can u tell me about the ‘this’ keyword?
This
is one of the hardest keyword for me to really explain. It is very different than this
in other programming language. Since we are talking about object variable, I’m only going to explain how this
reacts on object variable.
- If you use
this
inside an object, it will will be local to THAT specific object variable and will be followed by .propertyInsideTheObject
. Ex:
let variableName = {
objectName: "string",
objectName2: Boolean,
objectName3: 15,
objectName4: function() { return this.objectName }
}
/*When you are trying to call an object property that has
a value of a function, you have to call it like a function. */
console.log(variableName.objectName4()); //this will output "string"
- So what
this
is doing in the function above is go through THIS object which is variableName and then find objectName.
-
this
reacts differently depending on where it is used and how. I only explained 1 small part of it. If any of my explanations is unclear or you want to learn more about this
you can always go here for detailed explanation here
function exerciseTwo(userObj){
greeting: name(Dan){
console.log(‘Hi, my name is’+ this.name)
}
return userObj;
}
Can u tell me what im doing wrong in this?
console.log()
is for debugging purposes. It doesn’t return anything.
what else did I do wrong?
You need to add a method to userObj that returns “Hi, my name is” and the name.