2

This is my code

function nameIsDuplicate(name){
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase())
            return true;
    });
    return false;
}

I am building an online file manager system. the name argument is a name provided by the user via a textbox, and the $(object).text() is the name of files and folders in the current directory. These names come via exec("ls") command.

I need to check if the name provided by the user already exists. So I compare the name with every files/folders name. The problem is it doesn't find duplicates. The result of the above code is given in the following image

enter image description here

10
  • one advise is that don't use exec. It make your system a playground for clients Commented Mar 3, 2017 at 12:26
  • Why the () around $(object).text(), you sure that is needed? Commented Mar 3, 2017 at 12:27
  • When you are reading text from DOM, there is a possibility of extra white spaces. Use .trim() to clear them. Also to check instead of logging value, log their lengths Commented Mar 3, 2017 at 12:27
  • 4
    You are returning from callback not from the function. Commented Mar 3, 2017 at 12:27
  • @SagarV The system is in a local network, not on the internet. I also will limit access to certain directories. Commented Mar 3, 2017 at 12:27

2 Answers 2

2

The return true returns out of the each callback. That has no effect on each (it only cares about return false) and doesn't do anything to set the return value of nameIsDuplicate.

You want to return false there (no need to keep looking) and set a flag so your nameIsDuplicate can return it:

function nameIsDuplicate(name){
    var duplicate = false;
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase()) {
            duplicate = true;
            return false; // Stop looping
        }
    });
    return duplicate;
}

However, that function can be a lot simpler using Array.prototype.some:

function nameIsDuplicate(name){
    var objects = $("#content").find('p.itemOldName');
    name = name.toLowerCase();
    return objects.get().some(function(object) {
        return $(object).text().toLowerCase() === name;
    });
}

some calls its callback for each entry in the array. If the callback returns a falsy value, some keeps going; if the callback returns a truthy value, some stops. some's return value is true if a call to the callback returned a truthy value, false if not.

Sign up to request clarification or add additional context in comments.

1 Comment

true, can be one line solution
1

Your function doesn't return true, because you are in each loop... should be something like this:

function nameIsDuplicate(name){
    var same=0;
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase()){
            same=1; 
            return false;        
        }
    });
    if(same){
        return true;
    }else{
        return false;
    }
}

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

Other solution:

function nameIsDuplicate(name){
    return $("#content").find('p.itemOldName').filter(function(){return $(this).text().toLowerCase() === name.toLowerCase();}).length;
}

1 Comment

Also, just return same or !!same. No need for if..else

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.