1

This below supposed to alert 'true', but, it is alerting 'No item'. Where i went i wrong on this code. Any clue?

Array.prototype.CheckColor = function (datain) {
    for (var i = 0, len = this.length; i < len; i++) {
        if (this[i] === datain) {
            return true;
        } else {
            return "No item";
        }
    }
}

var newstr = "red blue green".split(" ");
var oyrsval = Array.prototype.CheckColor.call(newstr, "blue");
alert(oyrsval);
5
  • 4
    because this is returning "No item" if the first item is not equal to datain Commented Jan 7, 2015 at 13:16
  • Move the return "No item"; outside the for (after) Commented Jan 7, 2015 at 13:17
  • because at the first time, when red doesn't matches to blue, it goes to else statement where 'No Item' is returned and it comes out of function. Commented Jan 7, 2015 at 13:18
  • why are you using a loop if there is already a return operation after the first element (occurs at both if and else). Maybe the else statement were meant to occur after the loop, if there is no datain match in the object. Commented Jan 7, 2015 at 13:18
  • Not related to your problem, but why aren't you just saying var oyrsval = newstr.CheckColor("blue');? Commented Jan 7, 2015 at 13:41

5 Answers 5

1

Should be

Array.prototype.CheckColor = function(datain){
    for (var i = 0, len = this.length; i < len;i++ ){
        if (this[i] === datain){
            return true; // Return true if found
        }
    } 
    return "No item"; // else return
}

Or simplier using indexOf:

Array.prototype.CheckColor = function(datain){
    return this.indexOf(datain) > -1 ? true : "No item";
}
Sign up to request clarification or add additional context in comments.

Comments

0

We it's already explained that your problem is because you return from the loop too early. I will propose another solution, maybe simpler:

Array.prototype.CheckColor = function (datain) {
    return this.some(function(el) {
        return datain === el;
    }) || 'No item';
}

Array.prototype.some method is useful in this case. Also since you are extending prototype there is no need to go hard way with Array.prototype.CheckColor.call(newstr, "blue") when you can directly use newstr.CheckColor("blue").

Check the demo.

Array.prototype.CheckColor = function (datain) {
    return this.some(function(el) {
        return datain === el;
    }) || 'No item';
}

var found = "red blue green".split(" ").CheckColor("blue");
var notfound = "red blues green".split(" ").CheckColor("blue");
alert(found);
alert(notfound);

Finally if testing if the item is within array is the only thing you need to do, you can use already available Array.prototype.indexOf method:

newstr.indexOf("blue") !== -1

Comments

0

You are returning the result for the first comparison in your for

Change your code to:

for (var i = 0, len = this.length; i < len;i++) {
    if (this[i] === datain) {
        return true;
    }
}

return "No item";

Comments

0

Change your function to:

Array.prototype.CheckColor = function(datain){

    for (var i = 0, len = this.length; i < len;i++ ){

        if (this[i] === datain) {
            return true;
        }
    }
    // if here, nothing found
    return "No item";
}

Comments

0

There's nothing wrong with looping over the array, but you could also regexp directly on the string:

String.prototype.CheckColor = function(datain) {
    return RegExp("\\b" + datain + "\\b").test(this);
}

In the real world I suppose you'd want to escape special regexp characters inside datain.

2 Comments

why rewrite the indexOf method ?
Because we only want separate words, that's what the \b is about.

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.