Ho everyone, I'm new to coding and I would like to know how do I find out whether an array has repeated objects or not regardless of their order without knowing what these objects are yet? For example:
I create an empty array:
var random_array = [];
Then I use one function to push objects in that array, so I end up with:
var random_array = [ball, ball, tree, ball, tree, bus, car];
In the array above we have:
3 balls, 2 trees, 1 bus, 1 car.
So how will I be able to know these repeated numbers? Should I use a for loop or what? Anyway, I'm new to coding so please bear with me. And thanks in advance!
Edit:
Ok, so here's my simple code:
function Product(name, price) {
this.name = name;
this.price = price;
}
var register = {
total: 0,
list: [],
add: function(Object, quant) {
this.total += Object.price * quant;
for (x=0; x <quant; x++) {
this.list.push(Object);
}
},
undo: function() {
var last_item = this.list[this.list.length - 1];
this.total -= last_item.price;
this.list.pop(this.list[this.list.length - 1]);
},
print: function() {
console.log("Super Random Market");
for (x = 0; x < this.list.length; x++) {
console.log(this.list[x].name + ": " + this.list[x].price.toPrecision(3));
}
console.log("Total: " + this.total.toFixed(2));
}
}
var icecream_1 = new Product("Chocolate Icecream", 2.30);
var cake_1 = new Product("Chocolate cake", 4.00);
register.add(icecream_1, 5);
register.add(cake_1, 3);
register.print();
I'm trying to build a cash register and I'm trying to find out how to only print the items once with its quantity, instead of multiple times.