How to create a javascript object that keeps track of a stock price?

by francisco , in category: Technical Analysis , 6 months ago

How to create a javascript object that keeps track of a stock price?

Facebook Twitter LinkedIn Whatsapp

2 answers

by bridie_mante , 6 months ago

@francisco 

To create a JavaScript object that keeps track of a stock price, you can follow these steps:

  1. Create the object using the constructor function or object literal notation.
  2. Define properties for the object to store the relevant information such as stock symbol, current price, and any other details.
  3. Add methods to the object to perform operations like updating the price, retrieving the price, or calculating percentage changes.
  4. Implement any additional functionality you desire, such as tracking the historical prices or providing alerts based on price changes.


Here's an example using object literal notation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const stock = {
  symbol: "AAPL", // Stock symbol
  price: 150.50, // Current stock price
  
  updatePrice: function(newPrice) {
    this.price = newPrice;
  },
  
  getPrice: function() {
    return this.price;
  },
  
  calculateChanges: function(oldPrice) {
    const change = this.price - oldPrice;
    const percentageChange = (change / oldPrice) * 100;
    return {
      change,
      percentageChange
    };
  }
};

console.log(stock.getPrice()); // Output: 150.50

stock.updatePrice(155.25);
console.log(stock.getPrice()); // Output: 155.25

const changes = stock.calculateChanges(150.50);
console.log(changes.change); // Output: 4.75
console.log(changes.percentageChange); // Output: 3.159467964279182


In this example, the stock object is created with properties such as symbol and price. It has methods to update the price using updatePrice, retrieve the current price using getPrice, and calculate changes between a previous price and the current price using calculateChanges.

by paris_marquardt , 3 months ago

@francisco 

Here is the code illustrating how you can create a JavaScript object that keeps track of a stock price:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Define a constructor function for Stock
function Stock(symbol, initialPrice) {
  this.symbol = symbol;
  this.price = initialPrice;
}

// Add a prototype method to update the stock price
Stock.prototype.updatePrice = function(newPrice) {
  this.price = newPrice;
}

// Add a prototype method to get the current stock price
Stock.prototype.getPrice = function() {
  return this.price;
}

// Create a new instance of Stock
const stock = new Stock("AAPL", 150.50);

// Test the methods
console.log(stock.getPrice()); // Output: 150.50

stock.updatePrice(155.25);
console.log(stock.getPrice()); // Output: 155.25


In this code snippet, a constructor function Stock is defined to create Stock objects with the symbol and price properties. Prototype methods updatePrice and getPrice are added to update and retrieve the stock price, respectively. Finally, a new instance of Stock is created with the initial price, and the methods are tested to update and retrieve the stock price.