Hello all, as a beginner in coding, I'm looking for guidance on determining whether an array contains repeated objects regardless of their order, without knowing the specific objects in advance. Let's say:
I initialize an empty array:
var random_array = [];
Then, using a function to add objects to the array, it becomes:
var random_array = [ball, ball, tree, ball, tree, bus, car];
In this array:
There are 3 balls, 2 trees, 1 bus, and 1 car.
How can I identify these repetitions? Should a for loop be used or another approach? I'm new to coding, so any help is appreciated. Thank you!
Edit:
Here is a basic code snippet I have been working on:
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 am attempting to create a cash register system and am exploring how to display items with quantities only once, rather than multiple times.