Skip to content

Commit 6280162

Browse files
author
ralph14
committed
Code files
1 parent e70a30c commit 6280162

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+26754
-0
lines changed

Chapter01/chapter_01.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function fact(n) {
2+
if (n === 0) {
3+
return 1;
4+
} else {
5+
return n * fact(n - 1);
6+
}
7+
}
8+
console.log(fact(5)); // 120
9+
10+
const fact2 = n => {
11+
if (n === 0) {
12+
return 1;
13+
} else {
14+
return n * fact2(n - 1);
15+
}
16+
};
17+
console.log(fact2(5)); // also 120
18+
19+
const fact3 = n => (n === 0 ? 1 : n * fact3(n - 1));
20+
console.log(fact3(5)); // again 120
21+
22+
23+
function newCounter() {
24+
let count = 0;
25+
return function() {
26+
count++;
27+
return count;
28+
};
29+
}
30+
const nc = newCounter();
31+
console.log(nc()); // 1
32+
console.log(nc()); // 2
33+
console.log(nc()); // 3
34+
35+
function sum3(a, b, c) {
36+
return a + b + c;
37+
}
38+
39+
const x = [1, 2, 3];
40+
const y = sum3(...x); // equivalent to sum3(1,2,3)
41+
console.log(y); // 6
42+
43+
const f = [1, 2, 3];
44+
const g = [4, ...f, 5]; // [4,1,2,3,5]1
45+
console.log(g);
46+
47+
const h = [...f, ...g]; // [1,2,3,4,1,2,3,5]
48+
console.log(h);
49+
50+
const p = { some: 3, data: 5 };
51+
const q = { more: 8, ...p }; // { more:8, some:3, data:5 }
52+
console.log(q);
53+
54+
const numbers = [2, 2, 9, 6, 0, 1, 2, 4, 5, 6];
55+
const minA = Math.min(...numbers); // 0
56+
console.log(minA);
57+
58+
const maxArray = arr => Math.max(...arr);
59+
const maxA = maxArray(numbers); // 9
60+
console.log(maxA);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Jasmine Spec Runner v2.6.1</title>
7+
8+
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.6.1/jasmine_favicon.png">
9+
<link rel="stylesheet" href="lib/jasmine-2.6.1/jasmine.css">
10+
11+
<script src="lib/jasmine-2.6.1/jasmine.js"></script>
12+
<script src="lib/jasmine-2.6.1/jasmine-html.js"></script>
13+
<script src="lib/jasmine-2.6.1/boot.js"></script>
14+
15+
<script src="src2/once.js"></script>
16+
<script src="tests2/once.test.sin.window.js"></script>
17+
</head>
18+
19+
<body>
20+
</body>
21+
22+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Jasmine Spec Runner v2.6.1</title>
7+
8+
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.6.1/jasmine_favicon.png">
9+
<link rel="stylesheet" href="lib/jasmine-2.6.1/jasmine.css">
10+
11+
<script src="lib/jasmine-2.6.1/jasmine.js"></script>
12+
<script src="lib/jasmine-2.6.1/jasmine-html.js"></script>
13+
<script src="lib/jasmine-2.6.1/boot.js"></script>
14+
15+
<script src="src4/circle.js"></script>
16+
<script src="src4/isOldEnough3.js"></script>
17+
<script src="src4/roundFix2.js"></script>
18+
<script src="src4/getRandomFileName.js"></script>
19+
<script src="src4/shuffle.js"></script>
20+
21+
<script src="tests4/isOldEnough3.test.js"></script>
22+
<script src="tests4/circle.test.js"></script>
23+
<script src="tests4/roundFix2.test.js"></script>
24+
<script src="tests4/getRandomLetter.test.js"></script>
25+
<script src="tests4/getRandomFileName.test.js"></script>
26+
<script src="tests4/shuffle.test.js"></script>
27+
</head>
28+
29+
<body>
30+
</body>
31+
32+
</html>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js` and `jasmine_html.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.
3+
4+
If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.
5+
6+
The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.
7+
8+
[jasmine-gem]: http://github.com/pivotal/jasmine-gem
9+
*/
10+
11+
(function() {
12+
13+
/**
14+
* ## Require &amp; Instantiate
15+
*
16+
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
17+
*/
18+
window.jasmine = jasmineRequire.core(jasmineRequire);
19+
20+
/**
21+
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
22+
*/
23+
jasmineRequire.html(jasmine);
24+
25+
/**
26+
* Create the Jasmine environment. This is used to run all specs in a project.
27+
*/
28+
var env = jasmine.getEnv();
29+
30+
/**
31+
* ## The Global Interface
32+
*
33+
* Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged.
34+
*/
35+
var jasmineInterface = jasmineRequire.interface(jasmine, env);
36+
37+
/**
38+
* Add all of the Jasmine global/public interface to the global scope, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`.
39+
*/
40+
extend(window, jasmineInterface);
41+
42+
/**
43+
* ## Runner Parameters
44+
*
45+
* More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface.
46+
*/
47+
48+
var queryString = new jasmine.QueryString({
49+
getWindowLocation: function() { return window.location; }
50+
});
51+
52+
var filterSpecs = !!queryString.getParam("spec");
53+
54+
var catchingExceptions = queryString.getParam("catch");
55+
env.catchExceptions(typeof catchingExceptions === "undefined" ? true : catchingExceptions);
56+
57+
var throwingExpectationFailures = queryString.getParam("throwFailures");
58+
env.throwOnExpectationFailure(throwingExpectationFailures);
59+
60+
var random = queryString.getParam("random");
61+
env.randomizeTests(random);
62+
63+
var seed = queryString.getParam("seed");
64+
if (seed) {
65+
env.seed(seed);
66+
}
67+
68+
/**
69+
* ## Reporters
70+
* The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any).
71+
*/
72+
var htmlReporter = new jasmine.HtmlReporter({
73+
env: env,
74+
onRaiseExceptionsClick: function() { queryString.navigateWithNewParam("catch", !env.catchingExceptions()); },
75+
onThrowExpectationsClick: function() { queryString.navigateWithNewParam("throwFailures", !env.throwingExpectationFailures()); },
76+
onRandomClick: function() { queryString.navigateWithNewParam("random", !env.randomTests()); },
77+
addToExistingQueryString: function(key, value) { return queryString.fullStringWithNewParam(key, value); },
78+
getContainer: function() { return document.body; },
79+
createElement: function() { return document.createElement.apply(document, arguments); },
80+
createTextNode: function() { return document.createTextNode.apply(document, arguments); },
81+
timer: new jasmine.Timer(),
82+
filterSpecs: filterSpecs
83+
});
84+
85+
/**
86+
* The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript.
87+
*/
88+
env.addReporter(jasmineInterface.jsApiReporter);
89+
env.addReporter(htmlReporter);
90+
91+
/**
92+
* Filter which specs will be run by matching the start of the full name against the `spec` query param.
93+
*/
94+
var specFilter = new jasmine.HtmlSpecFilter({
95+
filterString: function() { return queryString.getParam("spec"); }
96+
});
97+
98+
env.specFilter = function(spec) {
99+
return specFilter.matches(spec.getFullName());
100+
};
101+
102+
/**
103+
* Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack.
104+
*/
105+
window.setTimeout = window.setTimeout;
106+
window.setInterval = window.setInterval;
107+
window.clearTimeout = window.clearTimeout;
108+
window.clearInterval = window.clearInterval;
109+
110+
/**
111+
* ## Execution
112+
*
113+
* Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
114+
*/
115+
var currentWindowOnload = window.onload;
116+
117+
window.onload = function() {
118+
if (currentWindowOnload) {
119+
currentWindowOnload();
120+
}
121+
htmlReporter.initialize();
122+
env.execute();
123+
};
124+
125+
/**
126+
* Helper function for readability above.
127+
*/
128+
function extend(destination, source) {
129+
for (var property in source) destination[property] = source[property];
130+
return destination;
131+
}
132+
133+
}());

0 commit comments

Comments
 (0)