Skip to content

Refactor koans to use ES6/7/8/9 #1

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

Open
wants to merge 12 commits 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
54 changes: 25 additions & 29 deletions KoansRunner.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html>
<html>
<head>
<title>Javascript Koans</title>
<link rel="stylesheet" type="text/css" href="lib/jasmine/jskoans-jasmine.css">
<script type="text/javascript" src="lib/jasmine/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine/jskoans-jasmine-html.js"></script>

<script type="text/javascript" src="lib/underscore-min.js"></script>
<head>
<title>Javascript Koans</title>
<link rel="stylesheet" href="lib/jasmine/jskoans-jasmine.css" />
<script src="lib/jasmine/jasmine.js"></script>
<script src="lib/jasmine/jasmine-html.js"></script>
<script src="lib/jasmine/jskoans-jasmine-html.js"></script>

<script type="text/javascript" src="lib/FILL_ME_IN.js"></script>
<script>
const FILL_ME_IN = "???";
</script>

<script type="text/javascript" src="koans/AboutExpects.js"></script>
<script type="text/javascript" src="koans/AboutArrays.js"></script>
<script type="text/javascript" src="koans/AboutFunctions.js"></script>
<script type="text/javascript" src="koans/AboutObjects.js"></script>
<script type="text/javascript" src="koans/AboutMutability.js"></script>
<script type="text/javascript" src="koans/AboutHigherOrderFunctions.js"></script>
<script type="text/javascript" src="koans/AboutInheritance.js"></script>
<script type="text/javascript" src="koans/AboutApplyingWhatWeHaveLearnt.js"></script>

</head>
<body>

<script type="text/javascript">
jasmine.getEnv().addReporter(new JsKoansReporter());
jasmine.getEnv().execute();
</script>

</body>
<script src="koans/AboutExpects.js"></script>
<script src="koans/AboutArrays.js"></script>
<script src="koans/AboutFunctions.js"></script>
<script src="koans/AboutObjects.js"></script>
<script src="koans/AboutMutability.js"></script>
<script src="koans/AboutHigherOrderFunctions.js"></script>
<script src="koans/AboutInheritance.js"></script>
<script src="koans/AboutApplyingWhatWeHaveLearnt.js"></script>
</head>
<body>
<script>
jasmine.getEnv().addReporter(new JsKoansReporter());
jasmine.getEnv().execute();
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2010-2014 David Laing and Greg Malcolm
Copyright (c) 2010-2020 David Laing, Greg Malcolm, Yan Fan, and Alexander Ogilvie

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
37 changes: 21 additions & 16 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
# javascript-koans-esl
This is [mrdavidlaing's JavaScript Koans](https://github.com/mrdavidlaing/javascript-koans), but with simple English. The vague Buddhist language in the original has been replaced with simple English. Set-up instructions are also added for complete beginners.

Javascript koans is to teach you JavaScript programming through testing. When you first run the koans, you will be given a 'runtime error' and a 'stack trace' indicating where the error occured.
This is [mrdavidlaing's JavaScript Koans](https://github.com/mrdavidlaing/javascript-koans), but with simple English, and refactored to use a more modern version of JavaScript. The vague Buddhist language in the original has been replaced with simple English. Setup instructions are also added for complete beginners.

Javascript koans are to teach you JavaScript programming through testing. When you first run the koans, you will be given a 'runtime error' and a 'stack trace' indicating where the error occurred.

Your goal is to find and fix the error.

Through exploring the code and fixing the errors, you will learn new things about the JavaScript language and functional programming.

#### What are koans?
## Koans

> A koan is a riddle or puzzle that Zen Buddhists use during meditation to help them unravel greater truths about the world and about themselves.

[source](http://www.huffingtonpost.com/entry/zen-buddhism-koan_us_563251dce4b0631799115f3c)

## Setting Up

### Download Using Git

If you know git:

```code
```sh
git clone https://github.com/yanarchy/javascript-koans-esl.git
```

### Download Without Git

If you do not know git, please download the zip and unzip on your computer.
![download zip image](readme-images/downloadzip.png)

### Open in Text Editor
If you do not have a text editor for JavaScript, I highly recommend downloading and installing [Sublime Text](https://www.sublimetext.com/3) or [Atom](https://atom.io/).

Open this `javascript-koans-esl project` folder in your text editor.
If you do not have a code editor for JavaScript, we highly recommend downloading and installing [Visual Studio Code](https://code.visualstudio.com/).

Open this `javascript-koans-esl project` folder in your code editor.

## How to Start

1. Open the `KoansRunner.html` file in the browser (for example, Chrome or Firefox).
1. Open the `KoansRunner.html` file in your web browser.
2. The first error will be in `koans/AboutExpects.js`.
![first error in html file](readme-images/firsterror.png)
![first error in html file](readme-images/firsterror.png)

3. Fix the first test in the `koans/AboutExpects.js` file and save it.

Expand All @@ -44,20 +49,20 @@ Open this `javascript-koans-esl project` folder in your text editor.

## Tips

* Do not think too much about each problem--they are simple, especially in the beginning.
* Are you really stuck on something? You can disable a test by adding an 'x' to the beginning of 'it(...)'. If you do this, the KoansRunner will skip this particular test and go on to the next. Remember to come back to the tests that you disable!
- Do not think too much about each problem - they are simple, especially in the beginning.
- Are you really stuck on something? You can disable a test by adding an `x` to the beginning of `it(...)`. If you do this, the KoansRunner will skip this particular test and go on to the next. Remember to come back to the tests that you disable!

```code
xit('should be disabled', function () {
```js
xit("should be disabled", function () {
// This is an example
});
```
```

* Don't be afraid to Google
* Don't be afraid to make mistakes
- Don't be afraid to Google
- Don't be afraid to make mistakes

## Other Information

Is there anything that needs to be explained in simpler English? Let me know.

The test runner used is [Jasmine](http://jasmine.github.io/) with a customized report viewer. Understanding Jasmine is not important for finishing this.
The test runner used is [Jasmine](http://jasmine.github.io/) with a customized report viewer. Understanding Jasmine is not important for completing these.
124 changes: 62 additions & 62 deletions koans/AboutApplyingWhatWeHaveLearnt.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,112 @@
var _; //globals

describe("About Applying What We Have Learnt", function() {

var products;

beforeEach(function () {
products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
});
describe("About Applying What We Have Learnt", () => {
const products = [
{
name: "Sonoma",
ingredients: ["artichoke", "sundried tomatoes", "mushrooms"],
containsNuts: false,
},
{
name: "Pizza Primavera",
ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"],
containsNuts: false,
},
{
name: "South Of The Border",
ingredients: ["black beans", "jalapenos", "mushrooms"],
containsNuts: false,
},
{
name: "Blue Moon",
ingredients: ["blue cheese", "garlic", "walnuts"],
containsNuts: true,
},
{
name: "Taste Of Athens",
ingredients: ["spinach", "kalamata olives", "sesame seeds"],
containsNuts: true,
},
];

/*********************************************************************************/

it("should find a pizza I can eat (imperative) because I am allergic to nuts and hate mushrooms", function () {

var i, j, hasMushrooms, productsICanEat = [];
it("should find a pizza I can eat (imperative) because I am allergic to nuts and hate mushrooms", () => {
let productsICanEat = [];

for (i = 0; i < products.length; i+=1) {
if (products[i].containsNuts === false) {
hasMushrooms = false;
for (j = 0; j < products[i].ingredients.length; j+=1) {
if (products[i].ingredients[j] === "mushrooms") {
hasMushrooms = true;
}
}
if (!hasMushrooms) productsICanEat.push(products[i]);
for (const product of products) {
if (product.containsNuts === false) {
let hasMushrooms = false;
for (const ingredient of product.ingredients) {
if (ingredient === "mushrooms") {
hasMushrooms = true;
}
}
if (!hasMushrooms) productsICanEat.push(product);
}
}

expect(productsICanEat.length).toBe(FILL_ME_IN);
});

it("should find a pizza I can eat (functional) because I'm allergic to nuts and hate mushrooms", function () {

var productsICanEat = [];
it("should find a pizza I can eat (functional) because I'm allergic to nuts and hate mushrooms", () => {
var productsICanEat = [];

/* solve using filter() & all() / any() */
/* solve using filter(), every()/some() */

expect(productsICanEat.length).toBe(FILL_ME_IN);
expect(productsICanEat.length).toBe(FILL_ME_IN);
});

/*********************************************************************************/

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () {

var sum = 0;
for(var i=1; i<1000; i+=1) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", () => {
let sum = 0;
for (let n = 1; n < 1000; n += 1) {
if (n % 3 === 0 || n % 5 === 0) {
sum += n;
}
}

expect(sum).toBe(FILL_ME_IN);
});

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {
it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", () => {
var sum = FILL_ME_IN; /* try chaining Array(length), map(), and reduce() */

var sum = FILL_ME_IN; /* try chaining range() and reduce() */

expect(233168).toBe(FILL_ME_IN);
expect(sum).toBe(233168);
});

/*********************************************************************************/
it("should count the ingredient occurrence (imperative)", function () {
it("should count the ingredient occurrence", () => {
var ingredientCount = { "{ingredient name}": 0 };

for (i = 0; i < products.length; i+=1) {
for (j = 0; j < products[i].ingredients.length; j+=1) {
ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1;
}
for (const product of products) {
for (const ingredient of product.ingredients) {
ingredientCount[ingredient] = (ingredientCount[ingredient] || 0) + 1;
}
}

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
});

it("should count the ingredient occurrence (functional)", function () {
var ingredientCount = { "{ingredient name}": 0 };

/* chain() together map(), flatten() and reduce() */

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
expect(ingredientCount["mushrooms"]).toBe(FILL_ME_IN);
});

/*********************************************************************************/
/* UNCOMMENT FOR EXTRA CREDIT */
/*
it("should find the largest prime factor of a composite number", function () {
it("should find the largest prime factor of a composite number", () => {

});

it("should find the largest palindrome made from the product of two 3 digit numbers", function () {
it("should find the largest palindrome made from the product of two 3 digit numbers", () => {

});

it("should find the smallest number divisible by each of the numbers 1 to 20", function () {

it("should find the smallest number divisible by each of the numbers 1 to 20", () => {

});

it("should find the difference between the sum of the squares and the square of the sums", function () {
it("should find the difference between the sum of the squares and the square of the sums", () => {

});

it("should find the 10001st prime", function () {
it("should find the 10001st prime", () => {

});
*/
Expand Down
Loading