Jump to content

string comparison problem.


Scip

Recommended Posts

here is a snippet of the code thats coursing me problems.

 

  var i=0;
     
      for(i=0;i<cartRows.length; i++){
       var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild;
       if (resourceName != clonedParent.getElementsByTagName('td')[0].firstChild){
                cartTable.appendChild(clonedParent);
       }else{
              alert("You may only pick a resource once.");
       }

 

it is suppose to go through the rows of a table (cart-table) and compare it to a new  row which is going to be entered to see if they are the same before it is entered. The problem is if the comparison is false it inserts the row and then alerts the user. If it false i want to only alert the user.

 

How can i make it do that. I tried switching statements around and tried "==" but doesn't seem to work.

 

thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/163747-string-comparison-problem/
Share on other sites

var newResourceName = clonedParent.getElementsByTagName('td')[0].firstChild;

for(var i=0;i<cartRows.length; i++)
{
    var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild;
    var duplicate = false;

    if (resourceName == newResourceName)
    {
        duplicate = true;
    }
}

if (duplicate)
{
    alert("You may only pick a resource once.");
}
else
{
    cartTable.appendChild(clonedParent);
}

I just realized I should have added a break statement if a match is found since there would be no reason to check and additional rows after the first match is found.

var newResourceName = clonedParent.getElementsByTagName('td')[0].firstChild;

for(var i=0;i<cartRows.length; i++)
{
    var resourceName = cartRows[i].getElementsByTagName('td')[0].firstChild;
    var duplicate = false;

    if (resourceName == newResourceName)
    {
        duplicate = true;
        break;
    }
}

if (duplicate)
{
    alert("You may only pick a resource once.");
}
else
{
    cartTable.appendChild(clonedParent);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.