Well, there are a couple of ways to approach this, and hacking together a function that adds blank values into an array is certainly one of them, but a more object-oriented approach is going to help you a lot more.
I'm not sure how "advanced" you are with javascript, so I'm sorry if it sounds like I'm babying you.
The first thing we want to do is set up a Currency class. There are a few ways to do this, but I prefer the one below, most likely because I'm from a C++ background. Feel free to use another method if you wish.
function Currency(type, quantity){
this.type = type;
this.quantity = quantity;
}
If you want, you could add some validation, such as making sure that quantity is greater than 0 or that type is a valid currency. You could also make those properties private and implement accessors for them, but I'm just trying to keep it simple for now. If you'd like, you could extend it for each currency, so you could say new USD(280)
to create a USD object with a value of 280.
So, now that we have our currencies settled, we can think about how we want to store them. We want to make sure that if we haven't added a currency, its quantity is equal to 0. To do this, we'll instantiate an object for each type of currency, then set its value as necessary.
Here's a really simple implementation:
function CurrencyList(){
var valid_currencies = ["EUR", "RON", "USD"];
this.currencies = [];
for(i in valid_currencies){
this.currencies[i] = new Currency(valid_currencies[i], 0);
}
}
I just created an array of valid currencies, iterate through them, and create a "public" property with the valid currency object. I store it in an array like in you have already, it's just in an object.
We're not done yet, though. What if some nefarious person deleted an element in our array? We'd be back to your original problem. We can solve this by making currencies
invisible outside the object.
function CurrencyList(){
var valid_currencies = ["EUR", "RON", "USD"];
currencies = [];
for(i in valid_currencies){
currencies[i] = new Currency(valid_currencies[i], 0);
}
}
Now if we try something like this:
var currency_list = new CurrencyList();
alert(currency_list.currencies[0]);
We get an error, which means that our array cannot be modified from outside.
This introduces another problem: what if we want to view an element in our array? The answer is to implement an accessor (a "get") method.
All we have to is create a "public" property and set it equal to a function, like this:
this.at = function(index){
return currencies[index];
}
Now we can access a currency like this:
var currency_list = new CurrencyList();
alert(currency_list.at(0));
This is it all put together:
function CurrencyList(){
var valid_currencies = ["EUR", "RON", "USD"];
currencies = [];
for(i in valid_currencies){
currencies[i] = new Currency(valid_currencies[i], 0);
}
this.at = function(index){
return currencies[index];
}
}
function Currency(type, quantity){
this.type = type;
this.quantity = quantity;
}
This will ensure that you can't have mismatched arrays, so we don't have to bother with a hacky, post hoc solution. In other words, it doesn't solve your problem, but rather prevents the problem from ever existing, which is an even better solution. It also ensures that your array is sorted by currency, because the elements will always be in the order that you specify in your constructor.