/*
* Text is the localized language pack, which is provided based on the viewers language settings
*/
var Text = {
Error: {
InvalidArray: 'You must supply a valid array',
NoMatch: 'No items matched the criteria'
}
};
/*
* Array.remove rips out the first occurance of the given value from an array
* @param s the string or number to remove
*/
Array.prototype.remove = function(s){
var i = this.indexOf(s);
if(i != -1) this.splice(i, 1);
};
// add array.indexOf to IE
if(!Array.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++){
if(this[i]==obj){
return i;
}
}
return -1;
}
}
// used for array.sort to handle the problem of numbers sorting by 10,2,310,41 instead of 2,10,41,310
function sortNumber(a,b)
{
return a - b;
}
/*
* findOdd sifts through an intArray and returns any values that are found
* to repeat an odd number of times
*
* @param {array} intArr is the array of integers
* @return {object} {
success: {bool} // true if at least one odd occurance is found, if false, 'error' will have a message
items: {array} // a list of valid odd occuring values as strings (to prevent massive array indexing)
error: {string} // if an error occurs, it will be reported here
* }
*/
var findOdd = function(intArr) { // unsorted
// setup the default return object
// TODO: this should be cloned from a default, defined in a master class or property
var output = {
success: false,
items: [],
error: ''
};
if(!intArr || !intArr.length) { // function is called without a value or supplied array is empty
output.error = Text.Error.InvalidArray;
return output;
}
// loop through the given array and build the item list
for(var i=0; i < intArr.length; i++) {
if(output.items.indexOf(intArr[i])!=-1)
output.items.remove(intArr[i]); // remove it (happens every even occurance)
else // not yet defined
output.items.push(intArr[i]); // add it (happens every odd occurance)
}
output.items.sort(sortNumber); // sort ASC numeric
// see if we found at least one item to return
if(output.items.length)
output.success = true;
else
output.error = Text.Error.NoMatch
return output;
};