1

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: "johndoe@gmail.com"
};

function changeEmail(param1) {
  param1 = param1.email.replace("johndoe", "newjohndoe");
  user.email = param1;
  return user;

}

changeEmail(user);
console.log(user);
MTK
  • 3,300
  • 2
  • 33
  • 49
JohnDoe99
  • 89
  • 2
  • 2
  • 4
  • 2
    Your assignment itself is fine. Focus on "*a function that takes in a user object*" and "*then returns the updated user object*". – Bergi Aug 21 '17 at 15:16
  • The code in question works perfectly. What's the issue? – Sagar V Aug 21 '17 at 15:23

6 Answers6

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";
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • your function will not replace a part of the user email, but the all email object.. – Arthur Aug 21 '17 at 15:17
  • 2
    @Arthur But that's what the assignment asks for? – Bergi Aug 21 '17 at 15:18
  • The replace function usage is disturbing – Arthur Aug 21 '17 at 15:19
  • @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});` – Thomas Aug 21 '17 at 15:39
  • 1
    @thomas https://stackoverflow.com/questions/32925460/spread-operator-vs-object-assign – Jonas Wilms Aug 21 '17 at 15:45
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;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
hairmot
  • 2,975
  • 1
  • 13
  • 26
  • 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. – Bergi Aug 21 '17 at 15:21
2

What's param1 ? You just have to do a replace on object attribute.

var user = {name: "John Doe", email: "johndoe@gmail.com"};

// 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 = "newjohndoe@gmail.com

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: "johndoe@gmail.com"};
user = replaceObjectParam(user, 'email, "johndoe", "newjohndoe")
Arthur
  • 4,870
  • 3
  • 32
  • 57
0

You should change the property not the parameter itself:

 param1.email = param1.email.replace("johndoe", "newjohndoe");
Manuel Sánchez
  • 174
  • 1
  • 7
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: "johndoe@gmail.com"};

function changeEmail (userObj, newEmail) {
 userObj.email = newEmail
 return userObj;
}

changeEmail(user, "newemail@gmail.com");
Christopher Messer
  • 2,040
  • 9
  • 13
-1

You are returning all the wrong stuff in your example. Take a look at this,

var oldUser = new User("John", "foo@barr.com");  // 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, "barr@foo.com");

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;
}
Jimenemex
  • 3,104
  • 3
  • 24
  • 56