1

I am trying to create a set of buttons that behave like a list, where an array is created from the values selected.

Below is the function for checking if the value already exists in the array, and if it does not it will not add the new value.

function linearSearch(arrayName, sValue) 
{               
    Array.prototype.exists = function(search){
      for (var i=0; i<this.length; i++)
        if (this[i] == search) return true;

      arrayName.push(sValue);
      return false;
    } 
}

Here is the jquery click function (of the listed items) where this function is called:

    con_array = [];
    $('.con_button').live('click', function (e) {
        e.stopPropagation()                              
        $(this).html('<div class="fun_button_center"></div>');
        con_value = $(this).attr('data-value');
        linearSearch(con_array, con_value);
        alert(con_array);
    });

The function works perfectly fine if it is inside of the click function without parameters. Yet in this circumstance, where it would be optimal because I can reuse it, no value is displayed with alert(con_array);

2 Answers 2

1

At the very least, write your `linearSearch function this way:

function linearSearch(arrayName, sValue) 
{               
    Array.prototype.exists = function(search){
      for (var i=0; i<this.length; i++)
        if (this[i] == search) return true;
    };

    if (arrayName.exists(sValue) return true;

    arrayName.push(sValue);
    return false;     
}

It's brittle to monkey-patch Array this way, though, so you can either monkey-patch it at global scope (still ugly, but at least it's both faster and available everywhere:

Array.prototype.exists = function(search){
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
};

function linearSearch(arrayName, sValue) 
{    
    if (arrayName.exists(sValue) return true;

    arrayName.push(sValue);
    return false;     
}

Or you can just inline that loop:

function linearSearch(arrayName, sValue) 
{    
    for (var i=0; i<arrayName.length; i++)
      if (arrayName[i] == sValue) return true;

    arrayName.push(sValue);
    return false;     
}
Sign up to request clarification or add additional context in comments.

3 Comments

ES5 has Array.prototype.indexOf() - no need for an exists function.
Thank you for then many options. I will go for the simplest without using a prototype, the array contains no more than 5 values. Thank you
0

You can try like this:

Array.prototype.exists = function(search){
      for (var i=0; i<this.length; i++)
        if (this[i] == search) return true;

      this.push(search);
      return true;
} 

function linearSearch(arrayName, sValue) 
{               
    arrayName.exists(sValue);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.