HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Bad cats for sale

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
salecatsbadfor

Problem

I am getting list of badCatsNames from webservices, now I have two asp.net bulleted lists here,

  • Bulleted List 1 - Available Cats



  • Bulleted List 2 - Purchasing Cats



Now I need to check each bad cat name if bad cat is in available cats list, if bad cat name is then take her out of Available Cats and put in Purchasing Cats ;), but if Cat is already in purchasing Cats list then put them in Available Cats list.

If bad cat name is not in any list then ignore it.

if(badCatsNames.length > 0)
{
    for (var i = 0; i < badCatsNames.length; i++) {
        var badCatName = badCatsNames[i];

        $("[id$=listAvailableCats] li").each(function (index) {
            if ($(this).text() == badCatName) {
                $(this).appendTo($("[id$=listPurchasingCats]"));
            }
            else {
                $("[id$=listPurchasingCats] li").each(function (index) {
                    if ($(this).text() == badCatName) {
                        $(this).appendTo($("[id$=listAvailableCats]"));
                    }
                });
            }
        });
    } 
}}

Solution

From a once over:

-
Naming: badCatNames not badCatsNames, listPurchasingCats -> I have no clue what this means, is this a list of cats for sale ?

-
Searching on a text : please consider the contains selector: https://api.jquery.com/contains-selector/ This way you do not have to loop over every "[id$=listAvailableCats] li"

-
$("[id$=listPurchasingCats] li").each(function (index) { should not be part of the other .each() loop, are you sure this code works ?

Context

StackExchange Code Review Q#44759, answer score: 3

Revisions (0)

No revisions yet.