diff --git a/.gitignore b/.gitignore index 62c8935..9f11b75 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.idea/ \ No newline at end of file +.idea/ diff --git a/src/fonctions/closure.js b/src/fonctions/closure.js new file mode 100644 index 0000000..736f569 --- /dev/null +++ b/src/fonctions/closure.js @@ -0,0 +1,25 @@ +var denver = { + name: 'Denver', + age:12 +}; + +var petitPied ={ + name: 'Petit Pied', + age:-65000000 +}; + +function pure(a,b){ + //a.age = 15 ; NOT PURE because it changes a + return a.age + b.age; //PURE : for some input, will always return a value +} + +console.log('pure :', pure(denver, petitPied)) + +function unpure(){ + // "sonsole.log" or "screen()" will modify a stream + // capture the external petitPied object + // depens on external context and not only parameters + console.log('kick '+petitPied.name); +} + +unpure(); \ No newline at end of file diff --git a/src/fonctions/es5.js b/src/fonctions/es5.js new file mode 100644 index 0000000..a14e79f --- /dev/null +++ b/src/fonctions/es5.js @@ -0,0 +1,99 @@ +var topics = require('../data').topics; + +// ******* Filtered topics ******* +var result = topics.filter(function (topic) { + // returns true if we keep this topic + return topic.user.name === 'Leonard'; +}); +console.log('result :', result); + +console.log('########'); + +var resultFat = topics.filter(topic => topic.user.name === 'Leonard'); +console.log('resultFat :', resultFat); + +console.log('########'); + + +// ******* Mapped topics ******* +var titles = topics.map(function (topic) { + return topic.title; +}); +console.log('titles :', titles); + +console.log('########'); + +var fatTitles = topics.map(topic => topic.title); +console.log('titles :', fatTitles); + +// ******* Contain violence ******* + +var hasViolence = topics.some(function (topic) { + return topic.tags.includes('violence'); +}); +console.log('has violence : ', hasViolence); +console.log('########'); + + + +// ******* Sheldon's comments ******* +var filtered = topics.filter(function (topic) { + return topic.comments.some(function (comment) { + return comment.user.name === 'Sheldon'; + }); +}); +console.log('filtered : ', filtered); + +var title = filtered.map(function (topic) { + return topic.title; +}); +console.log('Sheldon Comments : ', title); + +//fat arrow style +var fatFiltered = topics.filter(topic => topic.comments.some(comment => comment.user.name === 'Sheldon')).map(topic => topic.title); +console.log('Fat Sheldon Comments : ', fatFiltered); + + +// ******* Penny's sorted ids comments ******** +var pennyComment = []; +var fatPennyComment = []; + +var sortFunction = function (a, b) { + if (a === b) { + return 0; + } + return a < b ? -1 : 1; +}; + +topics.forEach(function (topic) { + return topic.comments.forEach(function (comment) { + if (comment.user.name === 'Penny') { + pennyComment.push(comment.id) + } + }); +}); +console.log('filtered : ', pennyComment.sort(sortFunction)); + +//fat arrow style +topics.filter(topic => topic.comments.forEach(comment => comment.user.name === 'Penny' ? fatPennyComment.push(comment.id) : 0)); +console.log('filtered : ', fatPennyComment.sort(sortFunction)); + + +// ******* "Fun" tags for non admin ******** +var tableTag =[]; + +topics.forEach(function (topic){ + topic.comments.forEach(function (comment) { + if(!comment.user.admin && comment.tags != undefined){ + comment.tags.forEach(function (tag) { + if(tag.includes('fun')){ + tableTag.push(comment.content); + } + + }) + } + + }) +}); + +console.log('tags : ', tableTag); \ No newline at end of file diff --git a/src/fonctions/foreach.js b/src/fonctions/foreach.js new file mode 100644 index 0000000..89ecbf7 --- /dev/null +++ b/src/fonctions/foreach.js @@ -0,0 +1,26 @@ +var users = require('../data').users; + +users.forEach(function (user) { + console.log('user name : ', user.name); +}); + +console.log('######'); +// no result sent by clog() +users.forEach(user => console.log('user name : ', user.name)); + + +//************** forEach - display admin users ************** +var admins = []; +var adminsFat = []; + +// function style +users.forEach( function (user){ + if(user.admin === true){ + admins.push(user.name); + } +}); +console.log('result : ',admins); + +//fat arrow style (not very pertinent) +users.forEach( (user) => user.admin ? adminsFat.push(user.name) : ''); +console.log('resultFat : ',adminsFat); \ No newline at end of file diff --git a/src/fonctions/format.js b/src/fonctions/format.js new file mode 100644 index 0000000..de6a97a --- /dev/null +++ b/src/fonctions/format.js @@ -0,0 +1,12 @@ +function add(a, b) { // more safe + return a + b; +} +console.log('2+3 = ', add(2, 3)); + +var mult = function (a, b) { // mult is a variable + return a * b; +}; +console.log('4*3 = ', mult(4,3)); + +var divide = (a,b) => a/b; // fat arrow style (ES2015) +console.log('10/2 =', divide(10,2)); \ No newline at end of file diff --git a/src/fonctions/operations.js b/src/fonctions/operations.js new file mode 100644 index 0000000..4543712 --- /dev/null +++ b/src/fonctions/operations.js @@ -0,0 +1,33 @@ +function add(a, b) { + return a + b; +} + +function divide(a, b) { + return a / b; +} + +function multiply(a, b) { + return a * b; +} + +var minus = function (a, b) { + return a - b; +}; + +console.log('a =', 2, 'b =', 6); +console.log('a*b =', multiply(2, 6)); +console.log('a*b =', divide(2, 6)); +console.log('a*b =', add(2, 6)); +console.log('a*b =', minus(2, 6)); + +function operation(op, x, y) { + return op(x,y); +} + +console.log('add ', operation(add, 56, 59)); +console.log('add ', operation(multiply, 56, 59)); + +var ops = [add, divide, multiply, minus]; + +var rand = ops[Math.floor(Math.random() * ops.length)]; +console.log('add random ', rand ,operation(rand, 56,59)); \ No newline at end of file diff --git a/src/fonctions/sort.js b/src/fonctions/sort.js new file mode 100644 index 0000000..0e8d20d --- /dev/null +++ b/src/fonctions/sort.js @@ -0,0 +1,29 @@ +// ********* classer des nombres dans un tableau ********* + +var x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; +console.log('tableau de x :', x); + +var sortFunction = function (a, b) { + if (a === b) { + return 0; + } + return a < b ? -1 : 1; +}; + +x.sort(sortFunction); +console.log('tableau de x :', x); + + +// ********* classer des objets ********* +var users = require('../data').users; +// console.log('users', users); + +function sortUsers(user1, user2) { + if (user1.name < user2.name) return -1; + if (user1.name > user2.name) return 1; + return 0; + +} + +users.sort(sortUsers); +console.log('users', users.map(user => user.name)); \ No newline at end of file diff --git a/src/functions/closure.js b/src/functions/closure.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/es5.js b/src/functions/es5.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/foreach.js b/src/functions/foreach.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/formats.js b/src/functions/formats.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/operation.js b/src/functions/operation.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/sort.js b/src/functions/sort.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/prorotypes/bind.js b/src/prorotypes/bind.js new file mode 100644 index 0000000..9088b53 --- /dev/null +++ b/src/prorotypes/bind.js @@ -0,0 +1,27 @@ +function Dinosaurus(name){ + this.size = 12; + this.name = name; +} +Dinosaurus.prototype.age = -65000000; + +const denver = new Dinosaurus('Denver'); + +function Carnivore(){ +} +Carnivore.prototype.eat = function (obj){ + console.log(this.name + ' eats ' + obj.name); +} + +const tRex = new Carnivore(); +tRex.name = 'T Rex'; + +//modification : +const eating = tRex.eat; +eating(tRex); + +//use bind to apply function to an object +boundToDenver = eating.bind(denver); +console.log('bound eating to denver'); +boundToDenver(tRex); + +eating.bind(denver)(tRex); \ No newline at end of file diff --git a/src/prorotypes/priority.js b/src/prorotypes/priority.js new file mode 100644 index 0000000..19bea6c --- /dev/null +++ b/src/prorotypes/priority.js @@ -0,0 +1,18 @@ +function Dinosaurus(name){ + this.size = 12; + this.name = name; +} +Dinosaurus.prototype.age = -65000000; +Dinosaurus.prototype.size = 20; + +const denver = new Dinosaurus('Denver'); + +const petitPied = new Dinosaurus('Petit Pied'); + +console.log('denver size : ', denver.size); + +//Denver object is built from a prototype +//But denver has no prototype :( +//console.log('denver prototype size : ', denver.prototype.size); +denver.size = 150; +console.log('new denver size : ', denver.size); \ No newline at end of file diff --git a/src/prorotypes/prototype.js b/src/prorotypes/prototype.js new file mode 100644 index 0000000..a42b0f8 --- /dev/null +++ b/src/prorotypes/prototype.js @@ -0,0 +1,46 @@ +/** + * Note that : we have a Uppercase + * It's just a convention + */ + +function Dinosaurus(name){ + this.size = 12; + this.name = name; +} +Dinosaurus.prototype.age = -65000000; + +const denver = new Dinosaurus('Denver'); +// new gives access to a prototype + +const petitPied = new Dinosaurus('Petit Pied'); +petitPied.size = 3; + +console.log('name : ', denver.name); +console.log('size : ', denver.size); +console.log('name : ', petitPied.name); +console.log('size :', petitPied.size); + +console.log('denver age : ',denver.age); +console.log('denver age : ',petitPied.age); + +function TRex(){ + this.name = 'Rex'; +} +function Carnivore(){ +} +Carnivore.prototype.eat = function (obj){ + console.log(this.name + ' eats ' + obj.name); +} + + +// TRex.prototype = Dinosaurus.prototype; +// TRex.prototype = Carnivore.prototype; + +Object.assign(TRex.prototype, Dinosaurus.prototype); +Object.assign(TRex.prototype, Carnivore.prototype); + +const rex = new TRex(); + +console.log('rex age : ', rex.age); +console.log('rex size : ', rex.size); +rex.eat(petitPied); \ No newline at end of file diff --git a/src/prorotypes/this.js b/src/prorotypes/this.js new file mode 100644 index 0000000..23b1d95 --- /dev/null +++ b/src/prorotypes/this.js @@ -0,0 +1,25 @@ +function Dinosaurus(name){ + this.size = 12; + this.name = name; +} +Dinosaurus.prototype.age = -65000000; + +const denver = new Dinosaurus('Denver'); + +function Carnivore(){ +} +Carnivore.prototype.eat = function (obj){ + console.log(this.name + ' eats ' + obj.name); +} + +const tRex = new Carnivore(); +tRex.eat(denver); + +tRex.name = 'T Rex'; +tRex.eat(denver); + +denver.eat = tRex.eat; +denver.eat(tRex); // Object this : denver + +const eating = tRex.eat; +eating(denver); // Object this : {} \ No newline at end of file diff --git a/src/start.html b/src/start.html index b73b456..0b544c8 100644 --- a/src/start.html +++ b/src/start.html @@ -4,13 +4,12 @@
You are so nice !
+You are so nice !
@@ -18,24 +17,25 @@