Home > Mobile >  Getting max and min value from array of objects and returning another value associated with the same
Getting max and min value from array of objects and returning another value associated with the same

Time:01-03

I am having trouble with a task from an education course I am taking, both the highestValShoe and lowestValShoe functions return the same.

I have tried my best with this but I am not understanding where I am going wrong here. I would really appreciate some pointers. Thank you!

//First I will establish an empty array to push() the shoes in later.
shoeArray = [];

//Now I will create a class for the shoes.
class Shoes {
  constructor(name, productCode, quantity, valuePerItem) {
    this.name = name;
    this.productCode = productCode;
    this.quantity = quantity;
    this.valuePerItem = valuePerItem;
  }

  //This code will enable us to update the quantity.
  updateQuantity(newQuantity) {
    this.quantity = newQuantity;
  }
}

//Now I will create 5 instances for the class.
converse = new Shoes("Converse", 010405, 3, 55.5);
adidas = new Shoes("Adidas", 030602, 5, 85.0);
nike = new Shoes("Nike", 052656, 2, 165.0);
vans = new Shoes("Vans", 745023, 6, 95.5);
fila = new Shoes("Fila", 034567, 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

//This function will enable us to search for any shoe within the array.
function searchShoes(shoeName, shoeArray) {
  for (i = 0; i < shoeArray.length; i  ) {
    if (shoeArray[i].name === shoeName) {
      return shoeArray[i];
    }
  }
}

//This function will enable us to search for the lowest value shoe. 
function lowestValShoe (shoeArray) {
  for (i=0; i<shoeArray.length; i  ){
    lowestVal = Math.min(shoeArray[i].valuePerItem)
    shoe = shoeArray[i].name
}
  return shoe
}

//This function will enable us to search for the highest value shoe.
function highestValShoe (shoeArray){
  for (i=0; i<shoeArray.length; i  ){
    highestVal = Math.max(shoeArray[i].valuePerItem)
    shoe1 = shoeArray[i].name
  }
  return shoe1
}

I tried to return the maximum value 'Shoe' and the minimum value 'Shoe', I thought it was working when I tested the lowestValShoe function, but then when I converted that to maximum, it was returning the same.

I altered the values on one of the shoes to make another one lowest instead, and realised that my lowestValShoe function was not working as intended either.

Both of these functions return 'Fila'

CodePudding user response:

You need to store the object instead only the name for fiinding the min or max value shoe.

Take the first item and iterate from the second to end. Then check if value is greater or smaller and take the item. Later return only the name.

function highestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i  ) {
        if (shoeArray[i].valuePerItem > shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

BTW, please declare all variables. If not, you get global ones and this may lead to funny results. And use semicolons. Always.

//Now I will create a class for the shoes.
class Shoes {
    constructor(name, productCode, quantity, valuePerItem) {
        this.name = name;
        this.productCode = productCode;
        this.quantity = quantity;
        this.valuePerItem = valuePerItem;
      }

    //This code will enable us to update the quantity.
    updateQuantity(newQuantity) {
        this.quantity = newQuantity;
    }
}

//This function will enable us to search for any shoe within the array.
function searchShoes(shoeName, shoeArray) {
    for (let i = 0; i < shoeArray.length; i  ) {
        if (shoeArray[i].name === shoeName) return shoeArray[i];
    }
}

//This function will enable us to search for the lowest value shoe. 
function lowestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i  ) {
        if (shoeArray[i].valuePerItem < shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

function highestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i  ) {
        if (shoeArray[i].valuePerItem > shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

//Now I will create 5 instances for the class.
const
    shoeArray = [],
    converse = new Shoes("Converse", "010405", 3, 55.5),
    adidas = new Shoes("Adidas", "030602", 5, 85.0),
    nike = new Shoes("Nike", "052656", 2, 165.0),
    vans = new Shoes("Vans", "745023", 6, 95.5),
    fila = new Shoes("Fila", "034567", 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

console.log(lowestValShoe(shoeArray));
console.log(highestValShoe(shoeArray));

CodePudding user response:

Since you need to rewrite your min and max functions so they won't just use Math.min and Math.max you could use Array.reduce to make them a one liner:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

function lowestValShoe (shoeArray) {
  return shoeArray.reduce((prev, curr) => (prev.valuePerItem < curr.valuePerItem ? prev : curr));
}

function highestValShoe (shoeArray){
   return shoeArray.reduce((prev, curr) => (prev.valuePerItem > curr.valuePerItem ? prev : curr));   
}

Here you can see it live returning Fila and Nike for respectively the min and the max:

//First I will establish an empty array to push() the shoes in later.
shoeArray = [];

//Now I will create a class for the shoes.
class Shoes {
  constructor(name, productCode, quantity, valuePerItem) {
    this.name = name;
    this.productCode = productCode;
    this.quantity = quantity;
    this.valuePerItem = valuePerItem;
  }

  //This code will enable us to update the quantity.
  updateQuantity(newQuantity) {
    this.quantity = newQuantity;
  }
}

//Now I will create 5 instances for the class.
converse = new Shoes("Converse", 010405, 3, 55.5);
adidas = new Shoes("Adidas", 030602, 5, 85.0);
nike = new Shoes("Nike", 052656, 2, 165.0);
vans = new Shoes("Vans", 745023, 6, 95.5);
fila = new Shoes("Fila", 034567, 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

console.log(lowestValShoe(shoeArray));
console.log(highestValShoe(shoeArray));

function lowestValShoe(shoeArray) {
  return shoeArray.reduce((prev, curr) => (prev.valuePerItem < curr.valuePerItem ? prev : curr));
}

function highestValShoe(shoeArray){
   return shoeArray.reduce((prev, curr) => (prev.valuePerItem > curr.valuePerItem ? prev : curr));   
}

  • Related