0

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

  • Welcome to Stack Overflow! I'm afraid it's really not clear what precisely you're asking. Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – T.J. Crowder Mar 09 '21 at 10:27
  • 1
    Side note: Nine parameters is **way** too many parameters for a function, trying to use it you'll get lost, even with IDE help. You might consider accepting an object with named properties (perhaps with destructuring), see the answers to [this question](https://stackoverflow.com/questions/11796093/is-there-a-way-to-provide-named-parameters-in-a-function-call-in-javascript). – T.J. Crowder Mar 09 '21 at 10:29
  • 1
    i dont get it, why `employeeName = new team` (name vs team?) – Dee Mar 09 '21 at 10:30
  • u should be using new keyword `class` – Dee Mar 09 '21 at 10:31
  • Something like `let employees = []; class Employee{ constructor(args){ console.log(args) } }; employees.push( new Employee({name:"...", age : 25}))` – Jeremy Thille Mar 09 '21 at 10:34
  • Ah, you completely changed your question while I was typing my comment..... So, new comment : `I have no idea and couldn't find any answers online on how to do it`, why are you saying that, because that's exactly what `areTheyRemote()` is doing (also it could be written `const areTheyRemote = employee => !!employee.workplace==="home"`) – Jeremy Thille Mar 09 '21 at 10:38
  • By changing the function argument to Employee i managed to do exactly what i needed and. also thank you for the comments on the previous function i showed on my question, changed it to make it simpler and more readable. – carneiro_squared Mar 09 '21 at 11:28

2 Answers2

1

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))
David
  • 1,034
  • 11
  • 20
0

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.