Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidying things up. #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.swp
.idea
.idea
.vscode/
File renamed without changes.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
Update
Javascript Koans
======

## Overview

JavaScript Koans is an interactive learning environment that uses failing tests to introduce students to aspects of JavaScript in a logical sequence.

The inspiration for this project comes from the Edgecase Ruby Koans and the book 'Javascript: The Good Parts'.
The inspiration for this project comes from the [Edgecase Ruby Koans](http://www.rubykoans.com/) and the book '[Javascript: The Good Parts'](https://www.oreilly.com/library/view/javascript-the-good/9780596517748/).

## Getting Started

Open the file jskoans.htm in your favourite browser and make the tests pass.
Open the file [jskoans.htm](jskoans.htm) in your favourite browser and make the tests pass by editing the files located in the `topics` directory

## Further Information

The koans use the [Qunit](http://qunitjs.com/) test syntax and test runner.

Get started with Ryan Anklam's [Learn JavaScript completely On the Cloud With the JavaScript Koans and Cloud9 IDE](http://blog.bittersweetryan.com/2011/08/learn-some-javascript-completely-on.html)

## License

This project is under the MIT License:

* [MIT License](LICENSE)

78 changes: 41 additions & 37 deletions jskoans.htm
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="support/jquery-1.4.1.js"></script>
<link rel="stylesheet" href="support/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="support/qunit.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- This will silently fail when not running in a testem environment. Safe to ignore. -->
<script type="text/javascript" src="testem.js"></script>
<title>Javascript Koans</title>

<script type="text/javascript" src="support/koans.js"></script>
<script type="text/javascript" src="topics/about_asserts.js"></script>
<script type="text/javascript" src="topics/about_operators.js"></script>
<script type="text/javascript" src="topics/about_equality.js"></script>
<script type="text/javascript" src="topics/about_truthyness.js"></script>
<script type="text/javascript" src="topics/about_assignment.js"></script>
<script type="text/javascript" src="topics/about_control_structures.js"></script>
<script type="text/javascript" src="topics/about_strings.js"></script>
<script type="text/javascript" src="topics/about_numbers.js"></script>
<script type="text/javascript" src="topics/about_objects.js"></script>
<script type="text/javascript" src="topics/about_arrays.js"></script>
<script type="text/javascript" src="topics/about_reflection.js"></script>
<script type="text/javascript" src="topics/about_prototype_chain.js"></script>
<script type="text/javascript" src="topics/about_prototypal_inheritance.js"></script>
<script type="text/javascript" src="topics/about_functions_and_closure.js"></script>
<script type="text/javascript" src="topics/about_this.js"></script>
<script type="text/javascript" src="topics/about_scope.js"></script>
<script type="text/javascript" src="topics/about_regular_expressions.js"></script>
</head>
<body>
<h1 id="qunit-header">JavaScript Koans</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<h3 class="welcome_message">To begin, find the file 'topics/about_asserts.js', and complete the tests.</h3>
<h3 id="zen-help" style="display: none;"></h3>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
<script src="support/jquery-1.4.1.js"></script>
<link rel="stylesheet" href="support/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="support/qunit.js"></script>

<!-- This will silently fail when not running in a testem environment. Safe to ignore. -->
<script type="text/javascript" src="testem.js"></script>

<script type="text/javascript" src="support/koans.js"></script>
<script type="text/javascript" src="topics/about_asserts.js"></script>
<script type="text/javascript" src="topics/about_operators.js"></script>
<script type="text/javascript" src="topics/about_equality.js"></script>
<script type="text/javascript" src="topics/about_truthyness.js"></script>
<script type="text/javascript" src="topics/about_assignment.js"></script>
<script type="text/javascript" src="topics/about_control_structures.js"></script>
<script type="text/javascript" src="topics/about_strings.js"></script>
<script type="text/javascript" src="topics/about_numbers.js"></script>
<script type="text/javascript" src="topics/about_objects.js"></script>
<script type="text/javascript" src="topics/about_arrays.js"></script>
<script type="text/javascript" src="topics/about_reflection.js"></script>
<script type="text/javascript" src="topics/about_prototype_chain.js"></script>
<script type="text/javascript" src="topics/about_prototypal_inheritance.js"></script>
<script type="text/javascript" src="topics/about_functions_and_closure.js"></script>
<script type="text/javascript" src="topics/about_this.js"></script>
<script type="text/javascript" src="topics/about_scope.js"></script>
<script type="text/javascript" src="topics/about_regular_expressions.js"></script>
</head>
<body>
<h1 id="qunit-header">JavaScript Koans</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<h3 class="welcome_message">To begin, find the file 'topics/about_asserts.js', and complete the tests.</h3>
<h3 id="zen-help" style="display: none"></h3>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
144 changes: 69 additions & 75 deletions support/koans.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,77 @@

var __ = "incomplete";

// ignore this. It simplifies determining array equality
Array.prototype.equalTo = function(compareTo) {
if (this.length !== compareTo.length) {
return false;
}
for(var i = 0; i < compareTo.length; i++) {
if (this[i] !== compareTo[i]) {
return false;
}
}
return true;
};

(function() {

var lastAssertLogReason, ignoreFurtherFailures = false;
var zenMessages = [
"The path to enlightenment has many stones",
"Do not stray from your path, for enlightenment comes with perseverance",
"The only Zen you find on tops of mountains is the Zen you bring there",
"Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
"Be master of mind rather than mastered by mind",
"Zen is not some kind of excitement, but concentration on our usual everyday routine",
"I think self-awareness is probably the most important thing towards being a champion",
"The reward of all action is to be found in enlightenment",
"lasting enlightenment can be achieved only through persistent exercise of real love",
"The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
"Do not think you will necessarily be aware of your own enlightenment",
"Enlightenment must come little by little - otherwise it would overwhelm",
"The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
"In the beginner's mind there are many possibilities, but in the expert's mind there are few",
"Only the hand that erases can write the true thing",
"Enlightenment is ego's ultimate disappointment",
"Man suffers only because he takes seriously what the gods made for fun",
"It is easy to believe we are each waves and forget we are also the ocean",
"Working out is my biggest hobby. It's my Zen hour. I just zone out",
"A self-motivation is an enlightenment of mind, empowerment of heart and enrichment of soul to arise, awake and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone"
];

QUnit.config.reorder = false;
(function () {
var lastAssertLogReason,
ignoreFurtherFailures = false;
var zenMessages = [
"The path to enlightenment has many stones",
"Do not stray from your path, for enlightenment comes with perseverance",
"The only Zen you find on tops of mountains is the Zen you bring there",
"Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
"Be master of mind rather than mastered by mind",
"Zen is not some kind of excitement, but concentration on our usual everyday routine",
"I think self-awareness is probably the most important thing towards being a champion",
"The reward of all action is to be found in enlightenment",
"lasting enlightenment can be achieved only through persistent exercise of real love",
"The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
"Do not think you will necessarily be aware of your own enlightenment",
"Enlightenment must come little by little - otherwise it would overwhelm",
"The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
"In the beginner's mind there are many possibilities, but in the expert's mind there are few",
"Only the hand that erases can write the true thing",
"Enlightenment is ego's ultimate disappointment",
"Man suffers only because he takes seriously what the gods made for fun",
"It is easy to believe we are each waves and forget we are also the ocean",
"Working out is my biggest hobby. It's my Zen hour. I just zone out",
"A self-motivation is an enlightenment of mind, empowerment of heart, and enrichment of soul to arise, awake, and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone",
];

QUnit.done(function(results) {
var failures = results.failed;
var total = results.total;
if (failures > 0) {
var failed = $('ol#qunit-tests > li.fail');
failed.hide();
$(failed[0]).show();
}
if (failures < total) {
$('h3.welcome_message').hide();
}
if (failures > 0) {
$("#zen-help").show();
}
$("body").scrollTop($(document).height());
});
QUnit.config.reorder = false;

QUnit.log(function(result) {
lastAssertLogReason = result.message;
});
QUnit.done(function (results) {
var failures = results.failed;
var total = results.total;
if (failures > 0) {
var failed = $("ol#qunit-tests > li.fail");
failed.hide();
$(failed[0]).show();
}
if (failures < total) {
$("h3.welcome_message").hide();
}
if (failures > 0) {
$("#zen-help").show();
}
$("body").scrollTop($(document).height());
});

QUnit.testDone(function(result) {
var message;
if (!ignoreFurtherFailures && result.failed > 0) {
ignoreFurtherFailures = true;
message = "" + randomZenMessage() + "\nTry meditating on this: " + result.module + ": " + result.name + " (" + lastAssertLogReason + ")";
$("#zen-help").html(message.replace(/\n/g, "<br /><br />"));
console.log(message);
}
});
QUnit.log(function (result) {
lastAssertLogReason = result.message;
});

function randomZenMessage() {
var randomIndex = Math.floor(Math.random() * zenMessages.length);
var zenMessage = zenMessages[randomIndex];
zenMessage = zenMessage.charAt(0).toUpperCase() + zenMessage.substr(1);
return "" + zenMessage + ".";
}
QUnit.testDone(function (result) {
var message;
if (!ignoreFurtherFailures && result.failed > 0) {
ignoreFurtherFailures = true;
message =
"" +
randomZenMessage() +
"\nTry meditating on this: " +
result.module +
": " +
result.name +
" (" +
lastAssertLogReason +
")";
$("#zen-help").html(message.replace(/\n/g, "<br /><br />"));
console.log(message);
}
});

function randomZenMessage() {
var randomIndex = Math.floor(Math.random() * zenMessages.length);
var zenMessage = zenMessages[randomIndex];
zenMessage = zenMessage.charAt(0).toUpperCase() + zenMessage.substr(1);
return "" + zenMessage + ".";
}
})();
40 changes: 20 additions & 20 deletions topics/about_arrays.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
module("About Arrays (topics/about_arrays.js)");

test("array literal syntax and indexing", function() {
var favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type
equal(__, favouriteThings[0], 'what is in the first position of the array?');
equal(__, favouriteThings[1], 'what is in the second position of the array?');
equal(__, favouriteThings[2], 'what is in the third position of the array?');
test("array literal syntax and indexing", function () {
var favouriteThings = ["cellar door", 42, true]; // Note that array elements do not have to be of the same type
equal(__, favouriteThings[0], "what is in the first position of the array?");
equal(__, favouriteThings[1], "what is in the second position of the array?");
equal(__, favouriteThings[2], "what is in the third position of the array?");
});

test("array type", function() {
equal(__, typeof([]), 'what is the type of an array?');
test("array type", function () {
equal(__, typeof [], "what is the type of an array?");
});

test("length", function() {
var collection = ['a','b','c'];
equal(__, collection.length, 'what is the length of the collection array?');
test("length", function () {
var collection = ["a", "b", "c"];
equal(__, collection.length, "what is the length of the collection array?");
});

test("splice", function() {
var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
test("splice", function () {
var daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var workingWeek = daysOfWeek.splice(__, __);
var weekend = daysOfWeek;

deepEqual(workingWeek, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], 'what is the value of workingWeek?');
deepEqual(weekend, ['Saturday', 'Sunday'], 'what is the value of weekend?');
deepEqual(workingWeek, ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], "what is the value of workingWeek?");
deepEqual(weekend, ["Saturday", "Sunday"], "what is the value of weekend?");
});

test("stack methods", function() {
test("stack methods", function () {
var stack = [];
stack.push("first");
stack.push("second");

equal(__, stack.pop(), 'what will be the first value popped off the stack?');
equal(__, stack.pop(), 'what will be the second value popped off the stack?');
equal(__, stack.pop(), "what will be the first value popped off the stack?");
equal(__, stack.pop(), "what will be the second value popped off the stack?");
});

test("queue methods", function() {
test("queue methods", function () {
var queue = [];
queue.push("first");
queue.push("second");
queue.unshift("third");

equal(__, queue.shift(), 'what will be shifted out first?');
equal(__, queue.shift(), 'what will be shifted out second?');
equal(__, queue.shift(), "what will be shifted out first?");
equal(__, queue.shift(), "what will be shifted out second?");
});
13 changes: 6 additions & 7 deletions topics/about_asserts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

module("About Asserts (topics/about_asserts.js)");

test("ok", function() {
ok(__ === true, 'what will satisfy the ok assertion?');
test("ok", function () {
ok(__ === true, "what will satisfy the `ok` assertion?");
});

test("not ok", function() {
ok(__ === false, 'what is a false value?');
test("not ok", function () {
ok(__ === false, "what is a false value?");
});

test("equal", function() {
equal(__, 1 + 1, 'what will satisfy the equal assertion?');
test("equal", function () {
equal(__, 1 + 1, "what will satisfy the `equal` assertion?");
});
11 changes: 5 additions & 6 deletions topics/about_assignment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

module("About Assignment (topics/about_assignment.js)");

test("local variables", function() {
test("local variables", function () {
var temp = __;
equal(temp, 1, "Assign a value to the variable temp");
equal(temp, 1, "what value needs to be assigned to `temp`?");
});

test("global variables", function() {
temp = 1; // Not using var is an example. Always use var in practise.
equal(window.__, temp, 'global variables are assigned to the window object');
test("global variables", function () {
temp = 1; // Not using var is an example. Always use var in practice.
equal(window.__, temp, "what is added to the `window` object when you declare a global variable?");
});
Loading