Skip to content

Instantly share code, notes, and snippets.

@krohne
Last active September 30, 2015 21:38
Show Gist options
  • Save krohne/16168f10b74df36aa3ca to your computer and use it in GitHub Desktop.
Save krohne/16168f10b74df36aa3ca to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="blue">////////</div>
<script id="jsbin-javascript">
console.log('1. Missing element');
// Assume unique values
function generateRandomArray(howMany) {
var s = new Set(); // ES6; forces unique values
while (s.size < howMany) {
// x10 to avoid potential infinite loop
s.add(Math.floor(Math.random() * howMany * 10));
}
return Array.from(s);
}
// Assume random element removed
function cloneAndRemoveOneElement(a) {
var l = a.length;
var e = Math.floor(Math.random() * l);
var b = a.slice();
b.splice(e, 1);
return b;
}
// Assume just 1 missing element
function findMissingElement(a, b) {
var lookup = {},
missing = [];
for (var i in b) {
lookup[b[i]] = true;
}
for (var j in a) {
if (typeof lookup[a[j]] == 'undefined') { // faster than indexof()
return a[j];
}
}
return null;
}
// Assume n missing elements
function findMissingElements(a, b) {
var lookup = {},
missing = [];
for (var i in b) {
lookup[b[i]] = b[i];
}
for (var j in a) {
if (typeof lookup[a[j]] == 'undefined') { // faster than indexof()
missing[missing.length] = a[j];
}
}
return missing;
}
var a = generateRandomArray(10);
console.log(a);
var b = cloneAndRemoveOneElement(a);
console.log(b);
var c = findMissingElement(a, b);
console.log(c);
console.log('2. Find all instances');
function generateLongParagraph() {
return "BOOK I. (FOLIO), CHAPTER III. (FIN-BACK).—Under this head I reckon a monster which, by the various names of Fin-Back, Tall-Spout, and Long-John, has been seen almost in every sea and is commonly the whale whose distant jet is so often descried by passengers crossing the Atlantic, in the New York packet-tracks. In the length he attains, and in his baleen, the Fin-back resembles the right whale, but is of a less portly girth, and a lighter colour, approaching to olive. His great lips present a cable-like aspect, formed by the intertwisting, slanting folds of large wrinkles. His grand distinguishing feature, the fin, from which he derives his name, is often a conspicuous object. This fin is some three or four feet long, growing vertically from the hinder part of the back, of an angular shape, and with a very sharp pointed end. Even if not the slightest other part of the creature be visible, this isolated fin will, at times, be seen plainly projecting from the surface. When the sea is moderately calm, and slightly marked with spherical ripples, and this gnomon-like fin stands up and casts shadows upon the wrinkled surface, it may well be supposed that the watery circle surrounding it somewhat resembles a dial, with its style and wavy hour-lines graved on it. On that Ahaz-dial the shadow often goes back. The Fin-Back is not gregarious. He seems a whale-hater, as some men are man-haters. Very shy; always going solitary; unexpectedly rising to the surface in the remotest and most sullen waters; his straight and single lofty jet rising like a tall misanthropic spear upon a barren plain; gifted with such wondrous power and velocity in swimming, as to defy all present pursuit from man; this leviathan seems the banished and unconquerable Cain of his race, bearing for his mark that style upon his back. From having the baleen in his mouth, the Fin-Back is sometimes included with the right whale, among a theoretic species denominated WHALEBONE WHALES, that is, whales with baleen. Of these so called Whalebone whales, there would seem to be several varieties, most of which, however, are little known. Broad-nosed whales and beaked whales; pike-headed whales; bunched whales; under-jawed whales and rostrated whales, are the fishermen's names for a few sorts.";
}
function findInstances(a) {
return a.match(/\w+/gi);
}
function most(words) {
var count = {},
word = '',
maxWord = '',
maxCount = 0,
mostWord = {};
for (var i in words) {
word = words[i].toLowerCase();
if (typeof count[word] == 'undefined') {
count[word] = 1;
} else {
count[word]++;
}
if (count[word] > maxCount) {
maxWord = word;
maxCount = count[word];
}
}
mostWord[maxWord] = maxCount;
return mostWord;
}
var a = generateLongParagraph();
console.log(a);
var b = findInstances(a);
console.log(b);
var c = most(b);
console.log(JSON.stringify(c));
console.log('3. getElementsByClassName(allElements, element, className)');
function allElements() {
return document.querySelectorAll('*');
}
function getElementsByClassName(allElements, element, className) {
var classElements = Array.prototype.filter.call(allElements,
function(e){
return (e.tagName === element) && (e.className === className);
});
return classElements;
}
var a = allElements();
// console.log(a);
var b = getElementsByClassName(a, 'DIV', 'blue');
console.log("b:", b);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment