Skip to content

Commit

Permalink
Third commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tcorral committed Apr 11, 2014
1 parent 7d852e2 commit 931804b
Show file tree
Hide file tree
Showing 32 changed files with 1,194 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .bookignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.grunt
.bookignore
.gitignore
Gruntfile.js
package.json
35 changes: 35 additions & 0 deletions Gruntfile.js
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');
};
12 changes: 10 additions & 2 deletions README.md
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.
20 changes: 19 additions & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,22 @@
* [Don't judge a book by its cover](utf8/README.md)
* [Encapsulate collection](encapsulate_collection/README.md)
* [Even or Odd](even_odd/README.md)
* [Exit nested loop](nested_loop/README.md)
* [Exit nested loop](nested_loop/README.md)
* [#1 Fooling around boolean](fooling_around_boolean/README.md)
* [#2 Fooling around boolean](fooling_around_boolean_2/README.md)
* [Ghost Array](ghost_array/README.md)
* [Input Search](input_search/README.md)
* [Invaluable](invaluable/README.md)
* [JSON](json/README.md)
* [Nested Scopes](nested_scopes/README.md)
* [Now you see me...](now_you_see_me/README.md)
* [Frozen](frozen/README.md)
* [Point](point/README.md)
* [Running Man](running_man/README.md)
* [Scope](scope/README.md)
* [Spartacus](spartacus/README.md)
* [Terminator](terminator/README.md)
* [Timers](timers/README.md)
* [Undefined Values in Arrays](undefined_values_in_array/README.md)
* [Array Map and ParseInt](array_map_parseint/README.md)
* [Variable Scope](variable_scope/README.md)
63 changes: 63 additions & 0 deletions array_map_parseint/README.md
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 );
```
---
18 changes: 9 additions & 9 deletions autoexecute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
I want to set variable 'a' to 3 using a Self Invoking Function, can you help me?

```
var a;
function test() { a = 3; }();
var testValue;
function test() { testValue = 3; }();
```

---
Expand All @@ -27,7 +27,7 @@ assert(true);

---

What is the value of variable 'a'?
What is the value of variable 'testValue'?

```js

Expand All @@ -53,7 +53,7 @@ Why?
```

```js

The value of testValue is undefined because the function has not been autoexecuted.
```

```js
Expand All @@ -67,17 +67,17 @@ assert(true);
Write the code to execute this function adding only one more character to the sentence.

```js
var a;
function test() { a = 3; }();
var testValue;
function test() { testValue = 3; }();
```

```js
var a;
!function test() { a = 3; }();
var testValue;
!function test() { testValue = 3; }();
```

```js
assert(a == 3);
assert(testValue == 3);
```

---
6 changes: 3 additions & 3 deletions banking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var yourChange = calculateChange();
What returns calculateAmountOfStockOptions ? Input the number value.

```js
var stockOptions =
var stockOptions = ;
```

```js
Expand All @@ -43,7 +43,7 @@ assert(stockOptions == 1.94392523364486);
What is the value of calculateChange ? Input the number value.

```js
var change =
var change = ;
```

```js
Expand All @@ -65,7 +65,7 @@ Why?
```

```js

'Javascript has several problems operating with floating point, this is one of the causes that it should not be to operate with floats.'
```

```js
Expand Down
4 changes: 3 additions & 1 deletion conditionals_functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ What's the reason of this behaviour?
```

```js

The execution of Snippet 1 shows "That's true" because function expressions are evaluated in execution time.
The execution of Snippet 2 shows "That's false" because function declarations are evaluated in evaluation time, and the second one overwrittes the first one.
The execution of Snippet 3 shows "That's true" because when the code has been evaluated it has changed to the function that could return "That's false" but when the code has been executed it has been overwritten again with the function expression.
```

```js
Expand Down
25 changes: 17 additions & 8 deletions delete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Animal = function(){};
Animal.prototype.name = 'animal';
mammal = new Animal();
mammal.name = 'mammal';
delete name;
Expand Down Expand Up @@ -46,57 +47,65 @@ The execution of this code logs:
```

---
Why #1: John is logged?

Why **#1: John** is logged?

```js

```
```js

John is logged because name is a global variable and global variables can't be deleted.
```
```js
assert(true);
```
---
---
Why #2: undefined is logged?
Why **#2: undefined** is logged?
```js
```
```js

undefined is logged because we have deleted the name property of obj, properties or members of objects can be deleted excluding the properties or members of the global object.
```
```js
assert(true);
```
---
---
Why #3: function toString() { [native code] } is logged?
Why **#3: function toString() { [native code] }** is logged?
```js
```
```js

function toString() { [native code] } is logged because toString is an inherited method from Object and inherited methods or members can't be deleted.
```
```js
assert(true);
```

---

---
Why #4: animal is logged?

Why **#4: animal** is logged?

```js

```
```js

animal is logged because we have deleted the own mammal.name property but the inherited property is shown.
```
```js
assert(true);
```

---
2 changes: 1 addition & 1 deletion even_odd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Please explain why Number.MAX_VALUE has not been added:
```

```js

Number.MAX_VALUE can't be handled properly by Javascript to work with it in operations because the overflow issue.
```
```js
Expand Down
41 changes: 41 additions & 0 deletions fooling_around_boolean/README.md
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);
```
---
Loading

0 comments on commit 931804b

Please sign in to comment.