Skip to content

Instantly share code, notes, and snippets.

@colemanfoley
Last active December 16, 2015 13:19
Show Gist options
  • Save colemanfoley/5440482 to your computer and use it in GitHub Desktop.
Save colemanfoley/5440482 to your computer and use it in GitHub Desktop.
Solutions to a couple of interview questions.
/*
The main function for finding elements by CSS selector. It checks which type
of selector is being passed in, then either finds the elements right there or
calls a helper function to find the elements.
*/
var findElements = function (selector) {
if (selector[0] === ".") {
return document.getElementsByClassName(selector.slice(1));
}
else if (selector[0] === "#") {
return document.getElementById(selector.slice(1));
}
else if (selector === "*") {
return document.body.childNodes;
}
else if (selector.indexOf(" ") > -1) {
console.log("Hit space case");
var separator = " ";
return isInside(selector, separator);
}
else if (selector.indexOf(">") > -1) {
var separator = ">";
return hasParent(selector, separator);
}
else if (selector.indexOf("+") > -1){
var separator = "+";
return immediatelyFollows(selector, separator);
}
else {
var nodeList = document.getElementsByTagName(selector);
return Array.prototype.slice.call(nodeList, 0);
}
};
//This function is used if the selector is of the form element>element
var hasParent = function (selector, separator) {
var neededInformation = getElementsToCheck(selector, separator);
var resultArray = [];
neededInformation.setOfElements.forEach(function(element){
if (element.parentNode.tagName === neededInformation.firstElement.toUpperCase()) {
resultArray.push(element);
}
});
if (resultArray.length > 0) {
return resultArray;
} else {
return "No elements found matching that selector.";
}
};
//This function is used if the selector is of the form element element.
var isInside = function (selector, separator) {
var neededInformation = getElementsToCheck(selector, separator);
var resultArray = [];
neededInformation.setOfElements.forEach(function(element){
var baseElement = element;
while(element !== document.body){
if (element.parentNode.tagName === neededInformation.firstElement.toUpperCase()) {
resultArray.push(baseElement);
break;
} else {
element = element.parentNode;
}
}
});
if (resultArray.length > 0) {
return resultArray;
} else {
return "No elements found matching that selector.";
}
};
//This function is used if the selector is of the form element+element.
var immediatelyFollows = function (selector, separator) {
var neededInformation = getElementsToCheck(selector, separator);
var resultArray = [];
neededInformation.setOfElements.forEach(function(element){
debugger;
if (element.previousSibling.tagName === neededInformation.firstElement.toUpperCase()) {
resultArray.push(element);
};
});
return resultArray;
};
//Helper function for hasParent, immediatelyFollows and isInside functions.
var getElementsToCheck = function (selector, separator){
var indexOfSeparator = selector.indexOf(separator);
var neededInformation = {};
neededInformation.firstElement = selector.slice(0, indexOfSeparator);
neededInformation.secondElement = selector.slice(indexOfSeparator + 1);
neededInformation.setOfElements = document.getElementsByTagName(neededInformation.secondElement);
neededInformation.setOfElements = Array.prototype.slice.call(neededInformation.setOfElements, 0);
return neededInformation;
};
//Calling the function with a test selector.
window.onload = function(){
console.log(findElements("div+div"));
};
var fizzBuzz = function () {
for (var i = 1; i < 101; i++) {
if ((i % 3 === 0) && (i % 5 === 0)){
console.log("FlashTalking");
}
else if (i % 3 === 0) {
console.log("Flash");
}
else if (i % 5 === 0) {
console.log("Talking");
}
else {
console.log(i);
}
};
}
<html>
<head>
<script type="text/javascript" src="find-elements.js"></script>
<title></title>
</head>
<body>
<div id="div1">div1</div>
<div id="div2">div2</div>
<section id="someSection">
someSection
<div class="class1">
class1
<p id="someP">
#someP
</p>
</div>
<div class="class2">class2</div>
</section>
<ul id="ul1">ul1</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment