Problem to do an arrays exercise. Beginner inside

Viewed 64

I'm learning Javascript, and I have a problem with arrays in Javascript, and I have to do a class called TV Show with title, thematicand and actors (actors with array) Then, I have to do a function to grab a specific actor and classify it as "favorite" randomly.

Once done, I get an "empty" array.

This is my code.

class TVShow {
    constructor (title, thematic, principalActors){
        var arrayActors = new Array();
        this.title=title;
        this.thematic=thematic;
        principalActors=[];
    
        this.generateFavActor = function(){
            var long = principalActors.lenght;
            let calc = Math.floor(Math.random()*(long));
            arrayActors = principalActors[calc];
        }
        console.log(arrayActors);
    }
}
var show01= new TVShow("The Revolution", "Accion",["Hello","Hello2"]);
show01.generateFavActor();

var show02 = new TVShow("Peaky Blinders", "Drama",["Hello","Hello2", "Hello3"] );
show02.generateFavActor();

var show03 = new TVShow ("Stranger Things", "Accion", ["Hello","Hello2", "Hello3", "Hello4"]); 
show03.generateFavActor();

Thanks!!

3 Answers

that ? ( using Destructuring assignment )

class TVShow
  {
  constructor (title, thematic, principalActors)
    {
    this.title    = title
    this.thematic = thematic
    this.actors   = [...principalActors]
    }
  generateFavActor()
    {
    console.log( this.actors[ Math.floor(Math.random()*this.actors.length ) ])
    }
  }
var show01 = new TVShow('The Revolution',  'Accion', ['Hello','Hello2'])
  , show02 = new TVShow('Peaky Blinders',  'Drama',  ['Hello','Hello2', 'Hello3'] )
  , show03 = new TVShow('Stranger Things', 'Accion', ['Hello','Hello2', 'Hello3', 'Hello4'])
  ;
show01.generateFavActor()
show02.generateFavActor()
show03.generateFavActor()

first correct the principalActors.length not principalActors.lenght
Second don't set principalActors as an empty array
Next Just put console.log inside the function...
it will give you the right answer...right now it is showing the value which is just assigned to an empty array var arrayActors = new Array()
Hope it will be HelpFul :-)
and I think arrayActors don'nt need to be an an array

class TVShow {
    constructor (title, thematic, principalActors){
        var arrayActors = new Array();
        this.title=title;
        this.thematic=thematic;
        
    
        this.generateFavActor = function(){
            var long = principalActors.length;
            let calc = Math.floor(Math.random()*(long));
           arrayActors = principalActors[calc];
           console.log(arrayActors);
        }
        
    }
}
var show01= new TVShow("The Revolution", "Accion",["Hello","Hello2"]);
show01.generateFavActor();

var show02 = new TVShow("Peaky Blinders", "Drama",["Hello","Hello2", "Hello3"] );
show02.generateFavActor();

var show03 = new TVShow ("Stranger Things", "Accion", ["Hello","Hello2", "Hello3", "Hello4"]); 
show03.generateFavActor();

I think you want something like this:

class TVShow {
    constructor (title, thematic, principalActors){        
        this.title=title;
        this.thematic=thematic;
        this.arrayActors = principalActors;       
    
        this.generateFavActor = function(){
            var size = principalActors.length;
            let calc = Math.floor(Math.random()*(size));
            return principalActors[calc];
        }        
        this.favoriteActor = this.generateFavActor();
    }
}

var show01 = new TVShow("The Revolution", "Accion",["Hello","Hello2"]);
var fav01 = show01.favoriteActor;

var show02 = new TVShow("Peaky Blinders", "Drama",["Hello","Hello2", "Hello3"] );
var fav02 = show02.favoriteActor;

var show03 = new TVShow ("Stranger Things", "Accion", ["Hello","Hello2", "Hello3", "Hello4"]); 
var fav03 = show03.favoriteActor;

console.log(fav01);
console.log(fav02);
console.log(fav03);
Related