-3

I have written this code can anyone tell what I have written is correct or not.

var employee = {
  name: "John Smith",
  job: "Programmer",
  age: 31,
  printing: function(){
    for (just in employee){
        alert(this.emloyee(just)"is"this.employee[just])
    }

  }

} 

And tell me that the upper code can work the same.

var employee = {
  name: "John Smith",
  job: "Programmer",
  age: 31,
  showAlert: function(){
    alert("Name is " + this.name);
    alert("Job is " + this.job);
    alert("Age is " + this.age);
  }
}

employee.showAlert()

  • this.emloyee(just) looks like a typo, so it is probably not correct. But in general, it helps to start with what your expectations of the code are. It is difficult for an outsider to judge whether code makes sense unless you know what it should do. If you are asking about code reviews, stackoverflow is not a good place to ask. Those questions tend to be closed quickly. – Philipp Claßen Nov 19 '19 at 19:02
  • `alert(this.emloyee(just)"is"this.employee[just])` is a syntax error. Did you mean to concatenate the strings? Use the `+` operator. – Bergi Nov 19 '19 at 19:22

1 Answers1

0

It seems you are looking for

var employee = {
  name: "John Smith",
  job: "Programmer",
  age: 31,
  showAlert() {
    for (const just in this) {
      if (typeof this[just] != "function") {
        alert(just + " is " + this[just])
      }
    }
  },
};
employee.showAlert()

Notice the const declaration in the loop, the usage of the property name just instead of employee(just), the string concatenation with +, and the use of this instead of employee.

Also the showAlert method itself is just a property that gets enumerated by the for … in loop, so I added a condition to only alert non-function properties.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375