Is there a way to modify this within a constructor once the item has been chosen from a randomly generated array?

If I use the following code:

card01.state = 3;
console.log(card01);

I can modify the state, but I'm interested in updating the state of the card chosen by the random function.

class Item {
  constructor(name, state) {
    this.name = name;
    this.state = state;
  }
};
class Card extends Item {
  constructor(name, damage, state, rarity) {
    super(name, state);
    this.damage = damage;
    this.rarity = rarity;
  }
};

var collectibles = [
  card01 = new Card("card of death", 5, 1, 0),
  card02 = new Card("two towers", 5, 1, 0),
  card03 = new Card("lovers", 5, 1, 0),
];

function gachaCard() {
  let random = collectibles[Math.floor(Math.random() * collectibles.length)];
  return random
}

Answer №1

Integrates an argument called state into the gachaCard() method, and then sets this argument as the state property of a randomly chosen variable.

class Item {
    constructor(name, state) {
      this.name = name;
      this.state = state;
    }
  };
  class Card extends Item {
    constructor(name, damage, state, rarity) {
      super(name, state);
      this.damage = damage;
      this.rarity = rarity;
    }
  };
  
  var collectibles = [
    card01 = new Card("card of death", 5, 1, 0),
    card02 = new Card("two towers", 5, 1, 0),
    card03 = new Card("lovers", 5, 1, 0),
  ];
  
  function gachaCard(state) {
    let random = collectibles[Math.floor(Math.random() * collectibles.length)];
    random.state = state;
    return random
  }

  console.log(gachaCard(2));

This will be shown in the console

Card {name: 'lovers', state: 2, damage: 5, rarity: 0}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discord.JS Guild Member Cache Responses that are not recognized as valid

My automated messaging bot has been running smoothly for the past 6-8 months, but recently it encountered a strange issue with a specific user. Upon checking the cache of the Discord server it operates on, I noticed that it only returned two members - myse ...

Having trouble with the JOSN.parse function not functioning properly

Hello there, I'm currently attempting to extract data from a simple JSON string but encountering an error. The object I am trying to retrieve looks like this: { "heading" : "The movies", "box5" : "Click on icon to add text.", "box1" : "At the movies, ...

Error in PromisifyAll: Callback parameter must be a function

Either my understanding of how BlueBird and its promisify function works is incorrect, or I am making a mistake in the following code. I have an "upload-handler" module that exports one function with a callback: The structure of the upload-handler functio ...

Completing a submission on a bootstrap form, using innerHTML and displaying an alert

I am currently having an issue with navigating to the home page after submitting a form in Bootstrap. Although I have successfully implemented an alert message upon submission, the page does not redirect to the home page as expected. Instead, it redirects ...

Leveraging AngularJS $promise in conjunction with NgResource

As I delve into the world of AngularJS, I have encountered promises which have proven to be quite beneficial in my learning journey so far. Now, I am eager to explore the optional library resource.js in AngularJS. However, I stumbled upon examples that lef ...

What causes the discrepancy in the expiresIn value of my JWT when it is transmitted from the server to the front-end?

After setting the token expiry date on the server, I decided to log out the value to double-check it. However, upon checking the value on my React front-end, I noticed a significant change in the value received compared to what was sent from the server. Is ...

Filtering a table with a customized set of strings and their specific order using pure JavaScript

Recently, I've been diving into APIs and managed to create a table using pure vanilla javascript along with a long list of sorting commands that can filter the table based on strings. My goal is to establish an object containing strings in a specific ...

What is the process for immediately changing the background color of an input field as soon as text is entered?

I am encountering an issue with the code snippet provided below. My goal is to change the background color of an input field as soon as I start typing something into it. The scenario involves 4 input fields where if the submit button is clicked and any f ...

Issue encountered when attempting to utilize Next-Auth alongside Credentials Provider to authenticate within a pre-existing system

I am currently utilizing the Next-Auth Credentials provider for authentication purposes through our existing API. Following the guidelines provided at https://next-auth.js.org/configuration/callbacks the code snippet used is as follows: callbacks: { ...

What is the method for retrieving the name of the 'Autocomplete' component in Material UI?

Currently, I am faced with the challenge of working on multiple Autocomplete MUI components and am in the process of creating a "generic" event handler that will utilize the useReducer hook to manage the state. The issue lies in the fact that the onChange ...

What is the process of creating a new array by grouping data from an existing array based on their respective IDs?

Here is the initial array object that I have: const data = [ { "order_id":"ORDCUTHIUJ", "branch_code":"MVPA", "total_amt":199500, "product_details":[ { ...

Run a series of functions with arguments to be executed sequentially upon the successful completion of an ajax request

I am currently working on implementing a couple of jQuery functions to assist me in testing some api endpoints that I am developing in php. While I have limited experience with Javascript and jQuery, I am struggling to figure out what additional knowledge ...

Validating user input fields to display error messages when empty

As part of my program, I need to gather information from users regarding their name and the number of pets they have. If a user enters an empty string, I want to display an error message prompting them to input something and fill the text box with their en ...

The error message "AWS Lambda Node 18: fetch is not defined, please resolve or include global fetch" indicates that

Seeking assistance with calling the Pagespeed Insights API from an AWS Lambda using Node 18. Struggling to make fetch() function properly. Cloudwatch logs indicate the message "inside the try about to fetch," but then nothing else appears. The Lambda con ...

Using PHP within Drupal, how can I show or hide a link as needed in PHP and Drupal?

Let me simplify my question - In the code snippet provided, how can I display the "Change" link only when the status value is "A"? I am currently struggling with the combination of Drupal module structure and PHP as I am still in the learning phase of mod ...

Upon reviewing the webpage using an IPAD and AngularJS

I recently completed a web application using AngularJS and PHP. It functions smoothly on Chrome and Firefox, but it encounters loading issues on IE due to the number of JS files. To solve this problem, I will need to reduce the amount of JS files for it to ...

What is the best way to display every comment and response using console.log()?

Currently, I am developing a commenting system where users can leave comments and reply to existing comments. In the MySQL database image Both images depict the same scenario. In my highlighted text in yellow, it should read comments[0] instead of comment ...

Internal server error encountered while making an AJAX call using AngularJS routing

I'm currently diving into AngularJS with a basic application focused on customers and their orders. The issue I'm encountering involves a table that showcases the list of customers along with a link to access their respective orders. However, upo ...

Linking to a Different Tab without Tab Mutation with Bootstrap 3.3.5

I am facing a similar issue to the mentioned questions. I am trying to use a link to change tabs, but the problem is that the link only changes the tab content and not the active tab. The most similar question can be found at: Bootstrap linking to a tab w ...

Using React for form validation

I'm facing a challenge while trying to develop a user registration form, especially when it comes to displaying form validation errors. Issues: 1) The input fails to post (via axios) to the database upon submission for inputs without errors. 2) The e ...