U have mentioned to check with a boolean.I don't know how it works.Can u explain it and please make sure that the syntax is correct.
(made another quick change; what is with me and the typos this week?!)
- var present = false;
-
for(i=0;(i< myDropDown.options.length) && (! present);i++)
-
present = (myDropDown.options[i].value == "NewYork");
-
-
if(! present) {
-
// I'm sure you know what goes here.
-
}
In this block, we loop through myDropDown.opti ons using a for loop, but in addition to checking to see if i is less than options.length, we also check to see if present is still false (since once we know that the item is already in the list of options, there's no point in continuing to search).
At the end of the loop's execution, if we never found "NewYork", then present will be false, and we know to add the new option. On the other hand, if present is true, then we know that the option already exists, so we can skip it.
For faster execution, I also provide this idea:
-
var myOptions = null; // Declared globally at start of execution.
-
-
.
-
.
-
.
-
-
function createOptionOrWhateverYouCalledIt(newValue, newText) {
-
.
-
.
-
.
-
// Load existing options (if we haven't done so already).
-
if(! myOptions) {
-
myOptions = {};
-
for(var i = 0; i < myDropDown.options.length; i++)
-
myOptions[myDropDown.options[i].value] = true;
-
}
-
-
if(! myOptions[newValue]) {
-
// Create the new option.
-
-
// Cache the new option.
-
myOptions[newValue] = true;
-
}
-
In this block, we create a global variable myOptions which we will use to cache all of the options in myDropDown so that we don't have to keep looping through it.
The first time we try to create an option (inside the aptly-named function createOptionOrW hateverYouCalle dIt), the function checks to see if myOptions has been defined. Since it was initialized to null, we create a generic object ('{}'), and then run through myDropDown and populate myOptions. E.g., if myDropDown had three options: (NewYork, Anchorage, Dallas), then myOptions would look like this:
-
myOptions = {
-
'NewYork': true,
-
'Anchorage': true,
-
'Dallas': true
-
};
-
Note that myOptions['NewYork'] === true, whereas myOptions['Indianapolis'] == false (undefined). This becomes important in the next step:
-
if(! myOptions[newValue]) {
-
newValue is the value of the new option. if newValue were 'NewYork', then myOptions['NewYork'] would resolve to true, which we negate to get false, so we don't create a new option.
On the other hand, if newValue were 'Ann Arbor', myOptions['Ann Arbor'] would resolve to undefined, which we negate to get true, so we *do* create the new option.
-
// Cache the new option.
-
myOptions[newValue] = true;
-
Once we create the new option, we add it to myOptions so we know about it the next time we try to create an option.
Now so far, this isn't that remarkable, since we had to run through the entire list of options. But watch what happens when we execute it a second time:
-
if(! myOptions) {
-
myOptions = {};
-
for(var i = 0; i < myDropDown.options.length; i++)
-
myOptions[myDropDown.options[i].value] = true;
-
}
Note that since myOptions now is defined (and contains all of the values in myDropDown.opti ons), we don't execute this statement at all!
Instead, we go straight to this block:
-
if(! myOptions[newValue]) {
-
// Create the new option.
-
-
// Cache the new option.
-
myOptions[newValue] = true;
-
}
-
And instead of O(n), we now have O(1) execution time!
(or something like that; I was never very good at computer science o_O)