Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherhill/9291710 to your computer and use it in GitHub Desktop.
Save christopherhill/9291710 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>MJ Test</title>
<style>
.passed { color: green; }
.failed { color: red; }
.sample-code {
border: 1px dashed gray;
background-color: #eeeeee;
padding: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
<h1>Build a simple set</h1>
<p>Implement a simple Set object in Javascript. For simplicity, the Set stores only Strings. The Set should support the following API:</p>
<pre class="sample-code">
var set = new Set(); // instantiates a new Set
set.add("someKey"); // stores 'someKey' in the set. add() returns true if the key was added; false otherwise.
set.count(); // returns the number of keys in the set.
set.contains("someKey"); // returns true if the key is in the set; false otherwise.
set.remove("someKey"); // deletes the key from the set. delete() returns true if the key was removed; false otherwise.
</pre>
What's included: jQuery and an assertion method called assertEqual that we use for simple tests. Refer to the code below for an
example of how to use assertEqual().
Suggestion/hint: You will need some data structure to store the items in the set. This should be a private variable.
<ul class="assertion-results">
</ul>
<script>
"use strict";
function assertEqual(actual, expected, message) {
var results = $(".assertion-results");
var css = (actual === expected) ? 'passed' : 'failed';
var fullMsg = (actual === expected) ? message : message + ' - FAILED: expected ' + expected + ' but was ' + actual;
results.append("<li class='" + css + "'>" + fullMsg + "</li>");
}
assertEqual(1, 1, 'This should pass');
assertEqual(true, false, 'This will fail');
function Set() {
// private variables
var struct = [];
// public variables and methods
var publics = {};
publics.add = function(key) {
// only add if not already present and a string
if (typeof(key) === 'string') {
if (!publics.contains(key)) {
if (struct.push(key)) {
return true;
} else {
return false;
}
}
}
}
publics.count = function() {
return struct.length;
}
publics.contains = function(key) {
// use $.inArray for x-browser
return $.inArray(key, struct) >= 0 ? true : false;
}
publics.remove = function(key) {
var index = $.inArray(key, struct);
if (index >= 0) {
if (struct.splice(index, 1)) {
return true;
} else {
return false;
}
}
}
// return closure of public methods
return publics;
}
var set = new Set();
assertEqual(set.count(), 0, 'Set starts out empty');
set.add("panda");
assertEqual(set.count(), 1, "Count should be 1");
set.add("panda");
assertEqual(set.count(), 1, "Count should still be 1");
set.add("tiger");
assertEqual(set.count(), 2, "Count should now be 2");
assertEqual(set.contains("panda"), true, "Set should contain 'panda'");
assertEqual(set.contains("tiger"), true, "Set should contain 'tiger'");
assertEqual(set.contains("lemur"), false, "Set should not contain 'lemur'");
set.remove("panda");
assertEqual(set.contains("panda"), false, "Set no longer contains 'panda'");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment