I have an array of stocks with each stock having 3 properties. I want to get the price property from the array and find out the highest/lowest price and return the object with highest/lowest price.
'use strict';
const stocks = [
{ company: 'Splunk', symbol: 'SPLK', price: 137.55 },
{ company: 'Microsoft', symbol: 'MSFT', price: 232.04 },
{ company: 'Oracle', symbol: 'ORCL', price: 67.08 },
{ company: 'Snowflake', symbol: 'SNOW', price: 235.8 },
{ company: 'Teradata', symbol: 'TDC', price: 44.98 }
];
I want to get price for each object in stocks and return the object with the highest price as max and return the object with lowest price as min.
// Function for highest/lowest price
function findStockByPrice(stocks) {
const max = Math.max.apply(null, stocks.price);
const min = Math.min.apply(null, stocks.price);
if (max === true) {
return stocks
} else if (min === true) {
return stocks
}
}
I am trying to use Math.max() but I am getting ReferenceError: max is not defined.
CodePudding user response:
There are a few problems with your code. First, you are trying to call Math.max() on the stocks.price property, but stocks is an array of objects, not a single object. Therefore, stocks.price is undefined and you will get a ReferenceError.
Second, Math.max() expects an array of numbers as its argument, but you are passing it an object. Therefore, even if you fix the ReferenceError, Math.max() will not work as expected because it cannot operate on an object.
To fix these problems, you can first use the map() method to extract the price property from each object in the stocks array, and then pass the resulting array of prices to Math.max() and Math.min() to find the maximum and minimum values.
Here is how you can implement the findStockByPrice() function to find the object with the highest and lowest price:
function findStockByPrice(stocks) {
// Extract the price property from each object in the stocks array
const prices = stocks.map(stock => stock.price);
// Find the maximum and minimum price
const max = Math.max(...prices);
const min = Math.min(...prices);
// Find the object with the highest price
const maxStock = stocks.find(stock => stock.price === max);
// Find the object with the lowest price
const minStock = stocks.find(stock => stock.price === min);
return { max: maxStock, min: minStock };}
You can then use this function like this:
const stockPrices = findStockByPrice(stocks);
console.log(stockPrices.max); // { company: 'Snowflake', symbol: 'SNOW', price: 235.8 }
console.log(stockPrices.min); // { company: 'Oracle', symbol: 'ORCL', price: 67.08 }
CodePudding user response:
First off, you are missing const
infront of max
and min
inside of your function. Second, as ak.leimrey pointed out in their comment, Math.max()
expects an array.
You can use the Math.max()
and Math.min()
methods to find the highest and lowest prices in the stocks array, and then use the find()
method to return the objects with those prices:
function findStockByPrice(stocks) {
// Find the highest and lowest prices in the stocks array
const maxPrice = Math.max(...stocks.map((stock) => stock.price));
const minPrice = Math.min(...stocks.map((stock) => stock.price));
// Return the objects with the highest and lowest prices
const maxStock = stocks.find((stock) => stock.price === maxPrice);
const minStock = stocks.find((stock) => stock.price === minPrice);
return {
max: maxStock,
min: minStock,
};
}