2

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);

2
  • 2
    Your assignment itself is fine. Focus on "a function that takes in a user object" and "then returns the updated user object". Commented Aug 21, 2017 at 15:16
  • The code in question works perfectly. What's the issue? Commented Aug 21, 2017 at 15:23

6 Answers 6

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I've taken the liberty to quote the other two assignment parts as well. If you don't like the edit, please feel free to roll it back.
3

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";

5 Comments

your function will not replace a part of the user email, but the all email object..
@Arthur But that's what the assignment asks for?
The replace function usage is disturbing
@Jonas, I've never seen destructuring a (non iterable) object with the spread operator/syntax like this ({...user,email}); and it fails when I test it. Could you add some reference for that? Is this an experimental, or browser specific feature? I'd write that as const changeEmail = (user,email) => Object.assign(user, {email});
2

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")

1 Comment

Ho sorry, my mistake
0

You should change the property not the parameter itself:

 param1.email = param1.email.replace("johndoe", "newjohndoe");

Comments

0

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]");

Comments

-1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.