To create a JavaScript object that keeps track of a stock price, you can define an object using the object literal notation or the constructor function. Here's an example using object literal notation:
1 2 3 4 5 6 7 |
let stock = { symbol: 'AAPL', price: 150.50, updatePrice: function(newPrice) { this.price = newPrice; } }; |
In this example, the stock
object has two properties: symbol
and price
. The symbol
property represents the stock symbol, such as 'AAPL' for Apple Inc. The price
property holds the current stock price.
To update the stock price, there is a method called updatePrice
defined within the object. This method takes a new price as a parameter and updates the price
property of the stock object.
You can access the properties and methods of the stock object using dot notation. For example, to retrieve the current stock price, you can use stock.price
. Similarly, to update the stock price, you can call the updatePrice
method like stock.updatePrice(155.75)
.
This is a basic example, and you can expand on it by adding more properties and methods depending on your requirements.
How to delete properties from a JavaScript object?
To delete properties from a JavaScript object, you can use the delete
operator. Here's how you can do it:
1 2 3 4 5 6 7 8 9 10 |
let obj = { name: 'John', age: 25, city: 'New York' }; // Deleting a property delete obj.age; console.log(obj); // Output: { name: 'John', city: 'New York' } |
In the example above, the delete
operator is used to remove the age
property from the obj
object. After the property is deleted, the object is logged to the console, showing that the age
property is no longer present.
How to calculate the change in stock price using a JavaScript object?
To calculate the change in stock price using a JavaScript object, you need to have the previous price and the current price stored within the object. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const stock = { symbol: "AAPL", previousPrice: 200, currentPrice: 220 }; // Calculate the change percentage const changePercentage = ((stock.currentPrice - stock.previousPrice) / stock.previousPrice) * 100; // Calculate the change value const changeValue = stock.currentPrice - stock.previousPrice; console.log(`Change percentage: ${changePercentage}%`); console.log(`Change value: $${changeValue}`); |
In this example, the stock object contains the symbol, previousPrice, and currentPrice properties. By subtracting the previous price from the current price, you can calculate the change value. Dividing the change value by the previous price and multiplying it by 100 will give you the change percentage. The results are then printed using console.log()
.
How to retrieve the current stock price from a JavaScript object?
To retrieve the current stock price from a JavaScript object, you will need to access the property that stores the price value. Here's an example of how you can do it:
1 2 3 4 5 6 7 8 9 10 11 |
// Assuming you have an object named "stock" with a property "price" const stock = { symbol: 'XYZ', price: 100.50, // other properties... }; // Access the "price" property of the "stock" object const currentPrice = stock.price; console.log(currentPrice); |
In this example, the stock
object has a property named price
which represents the current stock price. By simply accessing stock.price
, you can retrieve the value stored in that property.
How to implement getter and setter methods for a stock price object in JavaScript?
In JavaScript, you can implement getter and setter methods for a stock price object by using the get
and set
keywords.
Here is an example of how you can implement getter and setter methods for a stock price object:
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 |
class StockPrice { constructor() { this._price = 0; // private property } get price() { return this._price; } set price(newPrice) { if (typeof newPrice === 'number' && newPrice >= 0) { this._price = newPrice; } else { throw new Error('Invalid price'); } } } // Usage: const stock = new StockPrice(); console.log(stock.price); // Output: 0 stock.price = 10; // calling the setter console.log(stock.price); // Output: 10 stock.price = -5; // calling the setter with invalid price // Output: Uncaught Error: Invalid price |
In the example above, we use a private property _price
to store the actual price of the stock. The get
method, price()
, returns the value of _price
, and the set
method, price(newPrice)
, sets the value of _price
if it's a positive number.
By using getter and setter methods, you can control the access to the internal properties of your object and perform additional validation or logic as needed.
How to filter stock price objects based on certain criteria in JavaScript?
To filter stock price objects based on certain criteria in JavaScript, you can use the filter()
method of arrays along with a callback function. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 |
const stockPrices = [ { symbol: 'AAPL', price: 150 }, { symbol: 'GOOG', price: 2500 }, { symbol: 'TSLA', price: 800 }, { symbol: 'AMZN', price: 3500 }, ]; // Filter stocks with prices greater than or equal to 1000 const filteredStocks = stockPrices.filter(stock => stock.price >= 1000); console.log(filteredStocks); |
In the above example, the filter()
method is called on the stockPrices
array. The stock
parameter in the callback function represents each individual stock object. The callback function evaluates whether the price
property of each stock object is greater than or equal to 1000. If the condition is true, the stock object is included in the filteredStocks
array.
The output will be:
1 2 3 4 5 |
[ { symbol: 'GOOG', price: 2500 }, { symbol: 'TSLA', price: 800 }, { symbol: 'AMZN', price: 3500 } ] |
This demonstrates how to filter stock price objects based on a specific condition. You can modify the condition in the callback function to suit your requirement.