Reputation: 109
I've been stuck on this problem for awhile, and after various attepts, I decided it was time to ask for some help.
Here's the question: Create a function called changeEmail that takes in a user object and a newEmail string. Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object.
Here's my code
var user = {
name: "John Doe",
email: "[email protected]"
};
function changeEmail(param1) {
param1 = param1.email.replace("johndoe", "newjohndoe");
user.email = param1;
return user;
}
changeEmail(user);
console.log(user);
Upvotes: 2
Views: 40138
Reputation: 3176
You are returning all the wrong stuff in your example. Take a look at this,
var oldUser = new User("John", "[email protected]"); // Assuming you have a User object
function changeEmail(user, newEmail) {
var newUser = new User();
newUser.name = user.name;
newUser.email = newEmail;
return newUser;
}
var updatedUser = changeEmail(oldUser, "[email protected]");
You first have the old user to use, then you have the function to change their email.
When you want to change their email, you pass in the user object and a new email. The new user is now in updatedUser
A user object can be something simple like this,
var User = function(name, email) {
this.name = name;
this.email = email;
}
Upvotes: -1
Reputation: 138527
I think youre overcomplicating it. You just need to update the property:
function changeEmail(user,email){
user.email = email;
return user;
}
So you can do:
changeEmail({email:"before"},"after");
If you wanna annoy your teacher with some ESnext:
const changeEmail = (user,email)=>({...user,email});
//(note this does shallow copy)
const user = changeEmail({email:"before"},"after");
And actually, i think this assignment isnt really useful. Why not simply:
user.email = "after";
Upvotes: 3
Reputation: 5158
What's param1 ? You just have to do a replace on object attribute.
var user = {name: "John Doe", email: "[email protected]"};
// If you want to replace a part of email by another thing:
user.email = user.email.replace("johndoe", "newjohndoe")
// If you just want to setup a new email:
user.email = "[email protected]
Or with a function:
function replaceObjectParam(obj, key, old_value, new_value) {
obj[key] = obj[key].replace(old_value, new_value)
return obj
}
var user = {name: "John Doe", email: "[email protected]"};
user = replaceObjectParam(user, 'email, "johndoe", "newjohndoe")
Upvotes: 2
Reputation: 2965
To comply with the request
Create a function called changeEmail that takes in a user object and a newEmail string
your changeEmail function signature should look like
function changeEmail(userObject, emailString) {
}
Replace the user's current email address (assigned to the email property) with the newEmail string
means your function body could then look something like:
userObject.email = emailString;
and finally
then return the updated user object
would be
return userObject;
Upvotes: 3
Reputation: 2090
Let's look at your question again.
Create a function called changeEmail that takes in a user object and a newEmail string. Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object.
When looking at your code, the very first thing to notice is that your function
is only accepting one param, not two. This is why it will not satisfy your question. Another note is that you want to usually name your params
in a more descriptive way, so it makes sense when you're using them inside your function
.
Your updated code might look like this.
var user = {name: "John Doe", email: "[email protected]"};
function changeEmail (userObj, newEmail) {
userObj.email = newEmail
return userObj;
}
changeEmail(user, "[email protected]");
Upvotes: 0
Reputation: 174
You should change the property not the parameter itself:
param1.email = param1.email.replace("johndoe", "newjohndoe");
Upvotes: 0