Reputation: 147
Here is my dataset:
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
]
I'm trying to find and return the largest number in this object using a for loop. I know there are probably easier, more efficient routes but I'm specifically working on for loops.
Instead of simply returning the largest price, I however want to return the itemName of the largest price item. My code currently is only returning the largest price: 299.99 and not the itemName "Creation 3005". How would I return this information using for loops? This is what I have so far.
function mostExpensiveItemName(items) {
let expensive = items[0].price || null;
let number = null;
let name = null;
for (let i = 0; i < items.length; i++) {
number = items[i].price;
expensive = Math.max(expensive, number);
}
return expensive;
}
Upvotes: 1
Views: 949
Reputation: 16
function mostExpensiveItemName(items) {
let expensive = items[0];
let number = null;
let name = null;
for (let i = 0; i < items.length; i++) {
number = items[i];
expensive = expensive.price < number.price ? number:;
}
return expensive;
}
Upvotes: 0
Reputation: 74315
I would do something like this. It's cleaner, more expressive and you're not conflating two different concerns:
function mostExpensiveItemName(items) {
let item = mostExpensiveItem(items);
return item ? item.Name : undefined;
}
function mostExpensiveItem(items) {
let max;
for ( const item of items ) {
max = !item ? item // 1st item in list
: item.price > max.price ? item // replace existing w/new most-expensive item
: max ; // keep existing
}
return max ;
}
Upvotes: 0
Reputation: 3706
If you decide to look at alternatives to for
loops, these sort of questions are incredibly easy using reduce
.
const selectHigherPrice = (state, item) => state.price > item.price ? state : item
const initialState = items[0]
items.reduce(selectHigherPrice, initialState)
// One line
items.reduce((state, item) => state.price > item.price ? state : item)
state
is initialised as the first item in the items
listselectHigherPrice
function is called for each item in the arraystate.price
property is greater than the item.price
property:
state
is returneditem
is returned and becomes the state
variable when selectHigherPrice
is called for the next item in the list.If you are sure your list will have items you can omit the second argument
Upvotes: 1
Reputation: 10617
Here's what I would do:
function mostExpensive(objArray){
const m = objArray.map(o=>o.price);
return objArray[m.indexOf(Math.max(...m))];
}
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
];
const me = mostExpensive(items);
console.log(me); console.log(me.price);
Upvotes: 0
Reputation: 6269
you could use forEach
like this
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
]
let maxPrice = 0, product;
items.forEach(el => {
if(el.price > maxPrice){
maxPrice = el.price;
product = Object.assign({}, el); // for make sure you made a new copy not a reference
}
});
console.log(product, maxPrice)
Upvotes: 2
Reputation: 371019
Reassign the outer variable only if the item you're iterating over has a higher price:
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
];
function mostExpensiveItemName(items) {
let highestPriceSoFar = 0;
let nameOfHighestPriceSoFar;
for (const { itemName, price } of items) {
if (price > highestPriceSoFar) {
highestPriceSoFar = price;
nameOfHighestPriceSoFar = itemName;
}
}
console.log(nameOfHighestPriceSoFar);
}
mostExpensiveItemName(items);
Upvotes: 3