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
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);
}

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.