1

So, I started with this. I think, Worker is my class

    function Worker(firstName, lastName, role, action) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
        this.action = action;
        Worker.random = function (name) {
            name = this.name
            let actions = this.action
            console.log(Math.floor((Math.random() * name.action)))
        }


    }

Now I'm trying to make a 'new' object out of it here,

let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [
    'Did I Stutter?? You and Michael drink 3.',
    'You just had a heart attack and every time Michael opens his mouth you\'re stress levels to up. Drink 4 to calm your nerves',
    'Your cheating has caught up with you. Bad Stanley. Drink 2'
])

How do I make it so that I can Stanley.random() and get back a random action from the action array? I want to be able to use the random() function on any of the characters. I just don't really know how to go about it? This is the point right? to be able to use the methods over and over again?

I know I'm dumb, if you'd like to tell me that, feel free, just know that I know. Any help, is very appreciated

mowgmowg
  • 11
  • 4
  • 3
    `Worker.random = function (name) {` should just be `this.random = function () {` – epascarello Dec 11 '19 at 18:51
  • Take a look at the simple `Dog` example at this link: https://medium.com/@kevincennis/prototypal-inheritance-781bccc97edb. In your case it would be `Worker.prototype.random = function() {...` – Matt Browne Dec 11 '19 at 18:54

3 Answers3

2

You can assign functions to class props as well as regular data.. therefore, you'll need to assign this.random to a function, instead of Worker.random.. Something like this should do the trick..

function Worker(firstName, lastName, role, action) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.role = role;
  this.action = action;
  this.random = function() {
    if (this.action.length > 0) {
      return this.action[Math.floor(Math.random() * this.action.length)];
    }
  };
}

let Stanley = new Worker("Stanley", "Hudson", "Sales", [
  "Did I Stutter?? You and Michael drink 3.",
  "You just had a heart attack and every time Michael opens his mouth you're stress levels to up. Drink 4 to calm your nerves",
  "Your cheating has caught up with you. Bad Stanley. Drink 2"
]);

let randomAction = Stanley.random();
console.log("Random Action From Stanley:", randomAction);
Matt Oestreich
  • 8,219
  • 3
  • 16
  • 41
2
  1. Your function declaration is incorrect. It should be this.random = ... instead of Worker.random = ....

  2. Your random function is incorrect. It should pick a random index based on the length of the actions array.

function Worker(firstName, lastName, role, actions) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.role = role;
  this.actions = actions;
  
  this.random = function() {
    console.log( this.actions[Math.floor(Math.random()*this.actions.length)] );
  }
}


let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [
  'Did I Stutter?? You and Michael drink 3.',
  'You just had a heart attack and every time Michael opens his mouth you\'re stress levels to up. Drink 4 to calm your nerves',
  'Your cheating has caught up with you. Bad Stanley. Drink 2'
]);

Stanley.random();

Alternatively, use a true class and define random as a prototype method:

class Worker {
  constructor(firstName, lastName, role, actions) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.role = role;
    this.actions = actions;
  }

  random() {
    console.log(this.actions[Math.floor(Math.random() * this.actions.length)]);
  }
}


let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [
  'Did I Stutter?? You and Michael drink 3.',
  'You just had a heart attack and every time Michael opens his mouth you\'re stress levels to up. Drink 4 to calm your nerves',
  'Your cheating has caught up with you. Bad Stanley. Drink 2'
]);

Stanley.random();
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
0

You have two problems:

  1. you need to write a correct random() method
  2. you need to correctly put that method on your object or class.

You have two options to put the method on the object:

put the method on the object itself

You just need to replace Worker.random = function (name) { by this.random = function (name) { and you're done. However this means you will duplicate the method for each instance.

use the prototype

You can put a method on the prototype and it will be shadowed to all instances. So do this:

function Worker(firstName, lastName, role, action) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.role = role;
    this.action = action;
}

Worker.prototype.random = function (name){
  ...
}

now about your method, you probably want this:

function () {
  const index = Math.floor((Math.random() * this.actions.length));
  return this.actions[index];
}
Lux
  • 17,835
  • 5
  • 43
  • 73