bhordupur
bhordupur

Reputation: 960

Object comparison help needed

I would like to compare the names, if they match, then say we have similar names, else that we have different names. But this code gives output undefined while comparing names.

Could someone please help me out to understand/fix this problem?

I want to get following, for example:

We have different names, Bob and I.

function addPersonMethods(obj){
    this.obj = obj;
}

addPersonMethods.prototype.greet = function(str){
    return str + ", my name is "+ this.obj.name;
}

addPersonMethods.prototype.nameshake = function(othername){
    this.othername = othername;
    if (this.obj.name == this.othername){
         console.log("We have the same name ," + this.obj.name + " and I! ");
    }
    else
         console.log("We have different names ," + this.obj.name + " and I.");
}

var bob_def =  { name: 'Bob', age: 21 };
var eve_def =  { name: 'Eve', age: 21 };

var bob = new addPersonMethods(bob_def);
var eve = new addPersonMethods(eve_def);
var another_bob = new addPersonMethods({name:'Bob', age: 40});

console.log(bob.greet("Hi all"));
console.log(eve.greet("Hello"));
console.log(another_bob.greet("Hey"));

console.log(bob.nameshake(eve));

Upvotes: 2

Views: 119

Answers (2)

Shashank
Shashank

Reputation: 13869

You are comparing a string (this.obj.name) to an object (othername). They won't be equal so you will always output that they have different names. The return value of any function by default is undefined unless you specify otherwise, so that's why your output is tailed by undefined.

Either pass in eve.obj.name to the function, or extract that value inside the function so you can compare properly.

Upvotes: 2

Paul Roub
Paul Roub

Reputation: 36438

Your nameshake() method expects a string (the name), but you're passing an object, so the comparison will never be true. You want to compare to that object's .obj.name.

Second, you're logging the result of bob.nameshake(), when the function doesn't actually return anything.

And you're printing your own name in the "We have..." statements, when you want to print the other person's.

function addPersonMethods(obj) {
  this.obj = obj;
}

addPersonMethods.prototype.greet = function(str) {
  return str + ", my name is " + this.obj.name;
}

addPersonMethods.prototype.nameshake = function(otherperson) {
  var othername = otherperson.obj.name;

  if (this.obj.name == othername) {
    console.log("We have the same name, " + othername + " and I! ");
  } 
  else
    console.log("We have different names, " + othername + " and I.");
}


var bob_def = {
  name: 'Bob',
  age: 21
};
var eve_def = {
  name: 'Eve',
  age: 21
};


var bob = new addPersonMethods(bob_def);
var eve = new addPersonMethods(eve_def);
var another_bob = new addPersonMethods({
  name: 'Bob',
  age: 40
});

console.log(bob.greet("Hi all"));
console.log(eve.greet("Hello"));
console.log(another_bob.greet("Hey"));

bob.nameshake(eve);
bob.nameshake(another_bob);

Upvotes: 2

Related Questions