Netflix interview question

Write a contains function in javascript

Interview Answers

Anonymous

13 Sept 2014

HTMLElement.prototype.contains = HTMLElement.prototype.contains || function(child) { var children; var i; var node; if (this == child) { return true; } if (this.hasChildNodes) { for (i = 0, children = this.childNodes; i < children.length; i++) { node = children[i]; // skip for text nodes if (node.nodeType === 1 && (node == child || node.contains(child))) { return true; } } } return false; };

2

Anonymous

2 Aug 2015

function contains(needle, haystack) { var a = needle, b = haystack, result; return result = b.indexOf(a) !== -1 ? true : false; }

Anonymous

22 July 2012

function checkContains() { var url = "this is the answer"; if(url.indexOf("answer")!=-1) alert("contains"); else alert("not contains"); }