I'm finally doing an advanced course for React and have some pre-work stuff to complete.
The task description doesn't say what the exact result should be and my result doesn't seem right to me.
The task:
Part 1
Create a constructor for object Client with argument name.
This object is supposed to have the following attributes:
name and orders (array to store orders), and a method addOrder(order) (to add new orders to orders)
Part 2
Create a constructor for object Order with arguments client and number.
This object is supposed to have the following attributes:
client (related to the client) and number (order number)
Part 3
Check your code with the following:
const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");
client1.addOrder(order1);
client1.addOrder(order2);
console.table(client1.orders);
My code:
class Client {
constructor(name) {
this.name = name;
this.orders = [];
}
addOrder(order) {
this.orders.push(order);
}
}
class Order extends Client {
constructor(client, number) {
super(client);
this.number = number;
}
}
const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");
client1.addOrder(order1);
client1.addOrder(order2);
console.table(client1.orders);
returns this (and I believe, for the correct code, name should be "John" and orders arrays shouldn't be there at all):
Array(2)
0: Order {name: Client, orders: Array(0), number: '1'}
1: Order {name: Client, orders: Array(0), number: '2'}