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
return "No item";outside the for (after)returnoperation after the first element (occurs at both if and else). Maybe the else statement were meant to occur after the loop, if there is nodatainmatch in the object.var oyrsval = newstr.CheckColor("blue');?