Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Class method won't give the right output
class CoffeeShop {  
  constructor(name, menu) {
      this.name = name;
      this.menu = menu;
      this.orders = [];
  }

  addOrder(itemName) {
    this.menu.reduce((acc, meal, i, arr) => {
      meal[itemName] ? (this.orders.push(itemName), console.log("Order Added!"), arr.splice(1)) : (console.log("This item is currently unavailable!"), arr.splice(1))
    }, [])
  }

  fulfillOrder() {
    this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
  }
}


let Shop = new CoffeeShop("Shop", [
  { item: "cinnamon roll", type: "food", price: 4.99},
  { item: "hot chocolate", type: "drink", price: 2.99},
  { item: "lemon tea", type: "drink", price: 2.50}
]);

Shop.addOrder("hot chocolate");

console.log(Shop.orders);

Shop.fulfillOrder();

console.log(Shop.orders);

The output is:

This item is currently unavailable! [] The undefined is ready! []

Why isn't the order list changing? Why does the fullfillOrder function return the opposite of what it should return even when the condition is true?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would rather used filter - to filter out added item

class CoffeeShop {  
  constructor(name, menu) {
      this.name = name;
      this.menu = menu;
      this.orders = [];
  }

  addOrder(itemName) {
    this.menu = this.menu.filter(i => {
    if(i.item === itemName) {
        this.orders.push(itemName),
        console.log("Order Added!")
        return false;
    }
    console.log("This item is currently unavailable!")
    return true;
    })
  }

  fulfillOrder() {
    this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
  }
}


let Shop = new CoffeeShop("Shop", [
  { item: "cinnamon roll", type: "food", price: 4.99},
  { item: "hot chocolate", type: "drink", price: 2.99},
  { item: "lemon tea", type: "drink", price: 2.50}
]);

Shop.addOrder("hot chocolate");

console.log(Shop.orders);

Shop.fulfillOrder();

console.log(Shop.orders);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!