0

The problem I am having is that my text is not being added into the array for some reason. The code looks fine, but I can confirm it's not being added by having it print out the array.length.

<script>
    function addradio(value)
    {
        var element = document.createElement("input");
        var label = document.createElement("label");
        var tele = document.getElementById("Telephone");
        var selected_text = tele.options[tele.selectedIndex].innerHTML;

        // Array uses to stop duplicate radio button
        var countryArray = new Array();

        // Used to check if country is already in array, defaults to false
        var contain = new Boolean();

        // Checks if text contains a "1", returns "-1" if it doesn't
        var str = selected_text.indexOf("1");

        // For loop to check if country name is already in the array
        for(var i = 0; i <= countryArray.length; i++)
        {
            if(countryArray[i] == selected_text)
                contain = true;
            else
                contain = false;
        }

        // If value is not empty and text does not contain a "1" and array doesn't contain specified country
        if(value != "" && str == "-1" && contain == false)
        {
            // Creating the attributes for a radio button
            element.setAttribute("type", "radio");
            element.setAttribute("value", value);
            element.setAttribute("name", "telephone");
            label.appendChild(element);
            label.innerHTML += selected_text + "<br>";

            // Creating the radio button
            var newradio = document.getElementById("fillme");
            newradio.appendChild(label);

            // Adds country into the array if its' not already there
            countryArray.push(selected_text);
        }
    }
</script>

Can anyone identify the issue with my Array.push() function?

solaris
  • 33
  • 3

3 Answers3

1

You create an empty array and then try to loop over it. Since it's empty it has no length, so loop will never run. At the end you push a value into array, but next time method runs the array starts empty all over again

I suspect you intent to use the same array and call the function several times, in which case you would need to declare the array outside of your function.

You could also get rid of the for loop and use indexOf() to see if value you want is in array.

       // Move this outside function
    var countryArray = new Array();
     /* shorter syntax*/
     var countryArray =[];
    function addradio(value)...
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Thanks. Once again I made that mistake without realizing. Also your tip on using indexOf() instead of for has been useful for refactoring the code. – solaris Oct 31 '13 at 02:41
0

Your for loop is incorrect. It's missing the last element in the array.

        // For loop to check if country name is already in the array
        for(var i = 0; i < countryArray.length; i++)
        {
            if(countryArray[i] == selected_text)
                contain = true;
            else
                contain = false;
        }
ricick
  • 5,694
  • 3
  • 25
  • 37
0

Instead of doing the following:

for(var i = 0; i <= countryArray.length; i++)
    {
        if(countryArray[i] == selected_text)
            contain = true;
        else
            contain = false;
    }

I find this easier:

   if(!!countryArray.indexOf(selected_text) && countryArray.indexOf(selected_text) > -1)
   {
       //then item exist in array. You do not have to use the base bool operator, but secondary check.
   }

So basically:

 if(countryArra.indexOf(selected_text) > -1)  
  {  
    //it exist! 
  }
Casey ScriptFu Pharr
  • 1,672
  • 1
  • 16
  • 36