carneiro_squared
carneiro_squared

Reputation: 23

Question on how to work with class instances inside an array

So i am learning how to work with classes and class instances and have created a scenario where i have a class Employee that is extended by three other classes that represent employees from different departments. I already managed to create a function that allows me to create a new instance of any class and push it into an array containing all existing employees.

So now i am trying to play around and iterate over the array and create new functions that allow me to access a specific value of any instance.

for example the first one i am trying to do is a function that return true or false if an employee is working remotely:

function areTheyRemote(employee){
    if (employee.workplace === 'home'){
        return true;
    } else {
        return false;
    }
}

I have no idea and couldn't find any answers online on how to do it, hoping you could shed a light on me. Cheers

Upvotes: 0

Views: 94

Answers (2)

carneiro_squared
carneiro_squared

Reputation: 23

After some more digging i found that my major error was not passing as argument the class. I capitalized "employee" as class syntax and it now runs as intended.

Also simplified the previous function to create new class instances, so thank you for the comments.

Upvotes: 0

David
David

Reputation: 1074

Without seeing your class code for the employee it should be something like this:

function Employee(name, age, workplace) {
  this.name = name
  this.age = age
  this.workplace = workplace
}

const bob = new Employee('bob', 22, 'remote')

function areTheyRemote(employee) {
  if (employee.workplace === 'remote') {
    return true;
  } else {
    return false;
  }
}

console.log(areTheyRemote(bob))

Upvotes: 1

Related Questions