forked from tcorral/javascript-challenges-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,194 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.grunt | ||
.bookignore | ||
.gitignore | ||
Gruntfile.js | ||
package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var path = require("path"); | ||
|
||
module.exports = function (grunt) { | ||
grunt.loadNpmTasks('grunt-gitbook'); | ||
grunt.loadNpmTasks('grunt-gh-pages'); | ||
grunt.loadNpmTasks('grunt-contrib-clean'); | ||
|
||
grunt.initConfig({ | ||
'gitbook': { | ||
development: { | ||
output: path.join(__dirname, ".grunt/gitbook"), | ||
input: "./", | ||
title: "Javascript Challenges", | ||
description: "Challenge your self to learn and understand the most obscure and tricky parts of Javascript.", | ||
github: "tcorral/javascript-challenges-book" | ||
} | ||
}, | ||
'gh-pages': { | ||
options: { | ||
base: '.grunt/gitbook' | ||
}, | ||
src: ['**'] | ||
}, | ||
'clean': { | ||
files: '.grunt' | ||
} | ||
}); | ||
|
||
grunt.registerTask('publish', [ | ||
'gitbook', | ||
'gh-pages', | ||
'clean' | ||
]); | ||
grunt.registerTask('default', 'gitbook'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
# Javascript Challenges | ||
This book will challenge you to get and understand the most obscure and tricky parts of Javascript. | ||
This book will challenge you to learn and understand the most obscure and tricky parts of Javascript. | ||
|
||
I hope you enjoy this book. | ||
In order to perform the best that you can I recommend you not to cheat taking a look at solutions before you suggest one. | ||
|
||
This book can be used as a learning resource for Javascript training if you want, but please send a tweet recommending us to other people. | ||
|
||
I hope you enjoy this book because this is the purpose of this book. | ||
|
||
Feedback is welcome. | ||
|
||
Thanks a lot to [GITBOOK team](http://www.gitbook.io/) for it's amazing project to write your own books using Github repos. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Using Array.prototype.map and parseInt | ||
|
||
## Snippet 1 | ||
``` | ||
var result = ['1','10','100','1000','10000', '100000', '1000000'].map(parseInt) | ||
``` | ||
|
||
## Expected | ||
``` | ||
var result = [1, 10, 100, 1000, 10000, 100000, 100000]; | ||
``` | ||
--- | ||
What's the result of executing Snippet 1 code? | ||
```js | ||
var result = ; | ||
``` | ||
```js | ||
var result = [1, NaN, 4, 27, 256, 3125, 46656]; | ||
``` | ||
```js | ||
assert(result[0] === 1 && isNaN(result[1]) && result[2] === 4 && result[3] === 27 && result[4] === 256 &&& result[5] === 3125 && result[6] === 46656); | ||
``` | ||
--- | ||
|
||
--- | ||
Why? | ||
```js | ||
|
||
``` | ||
```js | ||
Array.prototype.map has three arguments that pass to the callback we set as argument: | ||
* value | ||
* index | ||
* arr | ||
When we check the specifications of parseInt we can see that parseInt could receive two arguments. | ||
The former is the string to be parsed and the latter is the ratio to convert the value. | ||
|
||
When we execute the previous code, this is that it's executed when we run the Snippet 1 code: | ||
* parseInt(1, 0) => 1 | ||
* parseInt(10, 1) => NaN | ||
* parseInt(100, 2) => 4 | ||
* parseInt(1000, 3) => 27 | ||
* parseInt(10000, 4) => 256 | ||
* parseInt(100000, 5) => 3125 | ||
* parseInt(1000000, 6) => 46656 | ||
``` | ||
```js | ||
assert(true); | ||
``` | ||
--- | ||
--- | ||
We need to get the same array as in Expected 1, please fix the code: | ||
```js | ||
var result = ['1','10','100','1000','10000', '100000', '1000000'].map(parseInt) | ||
``` | ||
```js | ||
var result = ['1','10','100','1000','10000', '100000', '1000000'].map(Number) | ||
``` | ||
```js | ||
assert(result[0] === 1 && result[1] === 10 && result[2] === 100 && result[3] === 1000 && result[4] === 10000 && result[5] === 100000 && result[6] === 1000000 ); | ||
``` | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# #1 Fooling around boolean | ||
|
||
Look at the following "implementation" of a xor method on the prototype of the Boolean type. | ||
|
||
``` | ||
Boolean.prototype.xor = function ( value ) { return !!this !== !!value; }; | ||
``` | ||
|
||
When we execute the following statement we get an unexpected result. | ||
|
||
``` | ||
false.xor(false); // => true | ||
``` | ||
|
||
--- | ||
Why does xor resolves in an unexpected manner? | ||
|
||
```js | ||
|
||
``` | ||
```js | ||
Because this is not false, this inside the function is the complete object and it evaluates to true when it's converted to true the same way that !!{} is true. | ||
``` | ||
```js | ||
assert(true); | ||
``` | ||
|
||
--- | ||
|
||
--- | ||
Write the code to fix the implementation of xor method: | ||
```js | ||
Boolean.prototype.xor = function ( value ) { return !!this !== !!value; }; | ||
``` | ||
```js | ||
Boolean.prototype.xor = function ( value ) { return !!this.valueOf() !== !!value; }; | ||
``` | ||
```js | ||
assert(false.xor(false) === false); | ||
``` | ||
--- |
Oops, something went wrong.