Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Anyone available to help me clean my code? Many thanks in advance!

function Shoes (shoeName, productCode, quantity, itemValue) {
   this.shoeName = shoeName; 
   this.productCode = productCode; 
   this.quantity = quantity; 
   this.itemValue = itemValue; 


     this.change = function (name, code, qty, value) {
      this.shoeName = name;
      this.productCode = code;
      this.quantity = qty;
      this.itemValue = value;

      console.log(`
      Item:
      ${shoeName} ${productCode} ${quantity} ${itemValue}

      has been edited to:
      ${name} ${code} ${qty} ${value}
      `)
   }

   this.shoeDescription = function () {

    return `
      Shoe: ${this.shoeName} 
      Product Code: ${this.productCode} 
      Qty: ${this.quantity}
      Value: ${this.itemValue}`
   }

}



function shoeSearch(shoes01, shoes02){
    let found = false;
    shoes01.forEach(x => {
        if(x.shoeName == shoes02){
            console.log(`${shoes02} found.`);
            found = true;
        }
    });

    if(!found){
        console.log(`${shoes02} not found.`)
    }
 }

function lowestItem(shoes01){
   let low = shoes01[0].itemValue;
   let shoeName = "";
   shoes01.forEach(shoes02 => {
       if (shoes02.itemValue < low){
           low = shoes02.itemValue;
           shoeName = shoes02.shoeName;
       }
   })

   console.log(`
   ${shoeName} 
   is our cheapest item, with value of £${low}`)
}




function highestItem(shoes01){
   let high = shoes01[0].itemValue;
   let shoeName = "";
   shoes01.forEach(shoes02 => {
       if (shoes02.itemValue > high){
           high = shoes02.itemValue;
           shoeName = shoes02.shoeName;
       }
   })

   console.log(`
   ${shoeName} 
   is our most expensive item has the value of £${high} `)
}



let sabat01 = new Shoes("nike", "nk0119", 2, 150);
let sabat02 = new Shoes("adidas", "ad5169", 43, 170);
let sabat03 = new Shoes("sketchers", "sk624", 10, 67);
let sabat04 = new Shoes("puma", "pu738", 20, 93);
let sabat05 = new Shoes("asics", "as823", 13, 50);

let shoeStock = [sabat01, sabat02, sabat03, sabat04, sabat05];



shoeStock.sort(
    (p1, p2) => 
    (p1.shoeName > p2.shoeName) ? 1 : (p1.shoeName < p2.shoeName) ? -1 : 0);




// find "adidas"
console.log(shoeSearch(shoeStock, "adidas"));
// try and find an item that doesnt exist
console.log(shoeSearch(shoeStock, "kicks"));
// find highest item
console.log(lowestItem(shoeStock));
// find lowest item
console.log(highestItem(shoeStock));
// get any item description
console.log(sabat03.shoeDescription());
// edit an item
console.log(sabat01.change("nike", "nk020", 88, 175));
// display product name in in ascending order
console.table (shoeStock)

2 Answers

Steven Parker
Steven Parker
231,210 Points

Here's a few suggestions. First, don't call console.log when using methods where you have it built-in:

// find "adidas"
shoeSearch(shoeStock, "adidas");                      // "console.log" is built-in
// try and find an item that doesnt exist
shoeSearch(shoeStock, "kicks");                       // "console.log" is built-in
// find highest item
highestItem(shoeStock);                               // "console.log" is built-in
// find lowest item
lowestItem(shoeStock);                                // "console.log" is built-in
// get any item description
console.log(sabat03.shoeDescription());
// edit an item
sabat01.change("nike", "nk020", 88, 175);             // "console.log" is built-in
// display product name in in ascending order
console.log(shoeStock.map(x => x.shoeName).sort());   // names only, sorted

I assume that last comment is meant to say "names" (plural). Is this from a course? You could include a link to the lesson page.

And the modern way of defining an object is with the "class" syntax:

class Shoes {                                                // "class" instead of "function"
  constructor(shoeName, productCode, quantity, itemValue) {  // constructor for initialization
    this.shoeName = shoeName;
    this.productCode = productCode;
    this.quantity = quantity;
    this.itemValue = itemValue;
  }

  change(name, code, qty, value) {                    // and methods have a simplified syntax
  // ...

Thanks alot! I did manage to find a way, but this is much simpler. Sorry i havent replied earlier, tree house emails were sent as junk mail. Thanks again!