From 29a97d7ee18fa05ad41da4d65d37ea29272b7334 Mon Sep 17 00:00:00 2001 From: Etienne Date: Mon, 24 Oct 2016 10:25:37 +0200 Subject: [PATCH 1/5] My first commit. --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file From 8a3fe13956d1fc5a14ed5ef6940c8aff490fb449 Mon Sep 17 00:00:00 2001 From: Etienne Date: Mon, 24 Oct 2016 15:16:53 +0200 Subject: [PATCH 2/5] funny or not funny --- src/types/strings.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/types/strings.js diff --git a/src/types/strings.js b/src/types/strings.js new file mode 100644 index 0000000..f0d0d86 --- /dev/null +++ b/src/types/strings.js @@ -0,0 +1,47 @@ +var string = "Hello World"; +string += "!"; +console.log('string : ', string); + +var index = string.indexOf('lo'); +console.log('index of lo :', index); + +function contains(haystack, needle) { + /** + * return true if haystack contains needle + * @param haystack + * @param needle + */ + return haystack.indexOf(needle) >= 0; + +} +var isTrue = contains('Jack', 'ack'); +var isAlsoTrue = contains('Jim', 'Jim'); +var isFalse = contains('Mac Donalds', 'ack'); + +console.log(isTrue, isAlsoTrue, isFalse); +console.log('Jack is back'.includes('ack')); +/** + * Expression Reguliere (permet de verifier la verasité de qql dans qqlchose) + */ + var hello = "Hello World"; +var found = hello.search(/orld/); +var alsoFound = hello.search(/world/i); +console.log('found : ' , found, ' also : ', alsoFound); + + // /i for +var x = /(.)*(world)/i.test('Hello World here'); +console.log('x : ', x); + +// is it conform +var strangelyTrue = /(.)*(world)/i.test('Hello World here'); +console.log('strangelyTrue : ', strangelyTrue); + +// $ defini l'endroit (ici world doit etre le dernier mot) +// must finish by world +var moreStrict = /(.)*(world)$/i.test('Hello World here'); +console.log('moreStrict : ', moreStrict); + + +var hitthis = 'funny not funny'; +console.log('you are '+ hitthis.substring(-9, 9));// exclusion des 9 dernier char +console.log('you are '+ hitthis.substr(-9, 9));// depart des 9 derniers char From b8552ab2c810e3dc407a35f7fa813e01ae3edb56 Mon Sep 17 00:00:00 2001 From: Etienne Date: Tue, 25 Oct 2016 16:16:52 +0200 Subject: [PATCH 3/5] funny or not funny --- src/functions/closure.js | 26 ++++++++++++++++ src/functions/es5.js | 27 ++++++++++++++++ src/functions/foreach.js | 26 ++++++++++++++++ src/functions/formats.js | 12 ++++++++ src/functions/multifunction.js | 44 ++++++++++++++++++++++++++ src/functions/operation.js | 36 ++++++++++++++++++++++ src/functions/sort.js | 21 +++++++++++++ src/types/arrays.js | 56 ++++++++++++++++++++++++++++++++++ src/types/bool.js | 5 +++ src/types/objects.js | 31 +++++++++++++++++++ src/types/strings.js | 7 +++++ src/types/typeof.js | 6 ++++ 12 files changed, 297 insertions(+) create mode 100644 src/functions/closure.js create mode 100644 src/functions/es5.js create mode 100644 src/functions/foreach.js create mode 100644 src/functions/formats.js create mode 100644 src/functions/multifunction.js create mode 100644 src/functions/operation.js create mode 100644 src/functions/sort.js create mode 100644 src/types/arrays.js create mode 100644 src/types/bool.js create mode 100644 src/types/objects.js create mode 100644 src/types/typeof.js diff --git a/src/functions/closure.js b/src/functions/closure.js new file mode 100644 index 0000000..089838d --- /dev/null +++ b/src/functions/closure.js @@ -0,0 +1,26 @@ +var denver = { + name: 'Denver', + age: 12 +}; + +var petitPied = { + name: 'Petit Pied', + age: -65000000 +}; + +function pure(a, b) { + // a.age = 15; NOT PURE + return a.age + b.age; //PURE : for some input, will always return a value +}; + +console.log('pure :', pure(denver, petitPied)); + +//care about hard memory use ! (fuite memoire care une fonction peu appelé une fonction qui appelle un objet qui appel une fonction...) +function unpure() { + //console.Log ou screen () : will modify a stream + // capture the external petitPied object + // depends on external context, and not only params + console.log('kick '+petitPied.name); +}; +unpure(); + diff --git a/src/functions/es5.js b/src/functions/es5.js new file mode 100644 index 0000000..c181692 --- /dev/null +++ b/src/functions/es5.js @@ -0,0 +1,27 @@ +var topics = require('../data').topics; +console.log('topics', topics); +var reuslt = topics.filter(function (topic) { + ///return true if we keep this topic + return (topic.user.name === "Leonard"); +}); +console.log('filter topic', reuslt); + +fatReuslt = topics.filter(topic => topic.user.name === 'Leonard'); +console.log('fat filter ',fatReuslt); + +var titles = topics.map(function (topic) { + return topic.title; +}); +console.log('titles', titles); + +fatTitles = topics.map(topic => topic.title); +console.log('fatTitles ', fatTitles); + +console.log('##################'); + +var hasViolence = topics.some(function (topic) { + + return topic.tags.includes('violence') + + }); +console.log('violence ?:', hasViolence); \ No newline at end of file diff --git a/src/functions/foreach.js b/src/functions/foreach.js new file mode 100644 index 0000000..15aa701 --- /dev/null +++ b/src/functions/foreach.js @@ -0,0 +1,26 @@ +var users = require('../data').users; + +users.forEach(function (user) { + console.log('user name :', user.name); +}); + +//no result sent by clog() +users.forEach(user => console.log('user name :', user.name)); + +users.forEach(user => user.name ? console.log('user name :', user.name) : 'NoName'); + + +var admins = []; +var noAdmins = []; +//users.forEach(user => user.admin ? admins.push(user) : noAdmins.push(user)); + +function admin(user) { + if (user.admin === true) { + admins.push(user.name); + } else { + noAdmins.push(user.name); + } +}; +users.forEach(admin); +console.log('admins : ', admins); +console.log('noAdmins :', noAdmins); diff --git a/src/functions/formats.js b/src/functions/formats.js new file mode 100644 index 0000000..d258595 --- /dev/null +++ b/src/functions/formats.js @@ -0,0 +1,12 @@ +function add(a, b) { + return a + b; +} +console.log('2+3 :', add(2, 3)); + +var mult = function (a, b) { + return a * b; +} +console.log('4*3 :', mult(4, 3)); + +var divide = (a, b) => a / b; +console.log('div :', divide(6, 3)); \ No newline at end of file diff --git a/src/functions/multifunction.js b/src/functions/multifunction.js new file mode 100644 index 0000000..d880b23 --- /dev/null +++ b/src/functions/multifunction.js @@ -0,0 +1,44 @@ +var users = require('../data').users; +var topics = require('../data').topics; + +// Topic titles where Sheldon wrote at least one comment +var getTitle = topics.filter(function (topic) { + return topic.comments.some(function (comment) { + return comment.user.name === 'Sheldon' + }) +}).map(function (topic) { + return topic.title; +}); +console.log('filter topic', getTitle); + +var fatGetTitle = topics.filter(topic => topic.comments.some(comment => comment.user.name === 'Sheldon')).map(topic => topic.title); +console.log('megaFat ', fatGetTitle); + +//var titles = getTitle.map(function (topic) { +// return topic.title; +//}); +//console.log('titles topics sheldon', titles); + +var getId = []; +topics.forEach(function (topic) { + var group = topic.comments.filter(function (comment) { + return comment.user.name === 'Penny' + }); + group.forEach(function (comment) { + + getId.push(comment.id); + }) +}); + +console.log('getId ', getId); + + +var contents = [] + +topics.forEach(function (topic) { + var group = topic.comments.filter(comment => !comment.user.admin && comment.tags && comment.tags.includes('violence')); + group.forEach(comment => contents.push(comment.content)); +}); +console.log('contents', contents); + + diff --git a/src/functions/operation.js b/src/functions/operation.js new file mode 100644 index 0000000..b495628 --- /dev/null +++ b/src/functions/operation.js @@ -0,0 +1,36 @@ +function add(a, b) { + return a + b; +} +console.log('2+6 : ', add(56, 59)); + +function divide(a, b) { + return a / b; +} +console.log('2/6 : ', divide(56, 59)); + +function multiply(a, b) { + return a * b; +} +console.log('2*6 : ', multiply(56, 59)); + +var minus = function (a, b) { + return a - b; +} +console.log('2-6 : ', minus(56, 59)); + +function operation(op, x, y) { + return op(x, y); +} +console.log('add : ', operation(add, 56, 59)); + +var ops = [add, divide, minus, multiply]; +var randomElement = ops[Math.floor(Math.random()*ops.length)]; +console.log('random : ', operation(randomElement, 56,59 )); + +var data= [3,4]; +data.operation = function (op) { + return op (this[0], this[1]) +}; +console.log(data.operation(multiply)); + + diff --git a/src/functions/sort.js b/src/functions/sort.js new file mode 100644 index 0000000..5b58ea1 --- /dev/null +++ b/src/functions/sort.js @@ -0,0 +1,21 @@ +var x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; + +var sortFunction = function (a, b) { + if (a === b) { + return 0; + } + return a < b ? -1 : 1; +}; +x.sort(sortFunction) +console.log('sorted x : ', x); + +var users = require('../data').users; + +function sortUsers(user1, user2) { + if (user1.name === user2.name) { + return 0; + } + return user1.name < user2.name ? -1 : 1; +}; +users.sort(sortUsers); +console.log('sortedName : ', users.map(user => user.name)); \ No newline at end of file diff --git a/src/types/arrays.js b/src/types/arrays.js new file mode 100644 index 0000000..405f2db --- /dev/null +++ b/src/types/arrays.js @@ -0,0 +1,56 @@ +var x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; +x.push(50); +console.log('add ',x); + +// remove first element +var firstElement = x.shift(); +console.log('first : ', x; + +// remove last element +var lastElement = x.pop(); +console.log('del last ',x); + +//insert at the begining +x.unshift('i am the first'); +console.log('x after unshift', x); + +//modify array +x[2] = -42; + +//deviation +x[-2] = "whaht ????"; +console.log('found :', x[-2]); + +console.log('all table', x); + +/* let's start over and serious stuff */ +x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; +x.sort(); +console.log('x sorted : ', x); + +x.sort(function (a, b) { + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + return 0; + +}); +console.log('x correctly sorted : ', x); + +var sortFunction = (a, b) =>a < b ? -1 : 1; +x.sort(sortFunction); +console.log('ES 2015 sorted :', x); + +var filtered = x.filter(function (number) { + return number >= 0; +}); +console.log('filtred x : ', filtered); + +var fatfiltered = (number) => number >= 0; +filtered = x.filter(fatfiltered); +console.log('filtred x : ', filtered); +direct = x.filter(number => number >= 0).filter(n=>n < 10); +console.log('filtred x : ', direct); + diff --git a/src/types/bool.js b/src/types/bool.js new file mode 100644 index 0000000..2ccc77b --- /dev/null +++ b/src/types/bool.js @@ -0,0 +1,5 @@ +console.log('this is bad', 2 == "2"); + +console.log('this is good', 2==="2"); + +console.log('this is also true', 2!=="2"); \ No newline at end of file diff --git a/src/types/objects.js b/src/types/objects.js new file mode 100644 index 0000000..345ff13 --- /dev/null +++ b/src/types/objects.js @@ -0,0 +1,31 @@ +var x = {}; +console.log('x : ', x, typeof x); + +var john = { + name: 'John', + address: 'London', + friend: { + name: 'Jim', + age: 12 + } +}; +console.log('John habite à : ', john.address); +console.log('Jim s ages ', john.friend.age); + +var funkyCop = { + name: 'Robocop' +}; +var ennemy = { + name: 'Dick' +}; +funkyCop.foe = ennemy; +console.log('funck : ', funkyCop); +ennemy.foe = funkyCop; +funkyCop.name = "Venere Robocop"; + +console.log('ennemy : ', ennemy); +ennemy.dead = true; +console.log('ennemy : ', ennemy); +delete funkyCop.foe; + +console.log('funky now : ', funkyCop); \ No newline at end of file diff --git a/src/types/strings.js b/src/types/strings.js index f0d0d86..ac629ec 100644 --- a/src/types/strings.js +++ b/src/types/strings.js @@ -45,3 +45,10 @@ console.log('moreStrict : ', moreStrict); var hitthis = 'funny not funny'; console.log('you are '+ hitthis.substring(-9, 9));// exclusion des 9 dernier char console.log('you are '+ hitthis.substr(-9, 9));// depart des 9 derniers char + +// Substring +var all = '\t\nmy String '.toLowerCase().trim(); +console.log(`

all

+multi +lines +

: >>>${all.toUpperCase()}<<<

`); \ No newline at end of file diff --git a/src/types/typeof.js b/src/types/typeof.js new file mode 100644 index 0000000..9fc9bb5 --- /dev/null +++ b/src/types/typeof.js @@ -0,0 +1,6 @@ +console.log('I m a string'); + +console.log(typeof 2); + +console.log(typeof {name xx}); + From bbf1110397cc21089e6234beac080510b1cbcbe6 Mon Sep 17 00:00:00 2001 From: Etienne Date: Tue, 25 Oct 2016 16:17:32 +0200 Subject: [PATCH 4/5] New changelist --- src/start.html | 34 +++++++++++++++++++++++++++++++++- src/start.js | 2 ++ src/types/numbers | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/start.js create mode 100644 src/types/numbers diff --git a/src/start.html b/src/start.html index f3e471e..9516fec 100644 --- a/src/start.html +++ b/src/start.html @@ -3,8 +3,40 @@ Starting JS + + - +

Hello JavaScript

+

+ You are so nice !! +

+ + + + + + \ No newline at end of file diff --git a/src/start.js b/src/start.js new file mode 100644 index 0000000..e3375bf --- /dev/null +++ b/src/start.js @@ -0,0 +1,2 @@ +var x = 2+2; +console.log(x); diff --git a/src/types/numbers b/src/types/numbers new file mode 100644 index 0000000..0fc0656 --- /dev/null +++ b/src/types/numbers @@ -0,0 +1,14 @@ +var x= 2; +console.log(typeof x); + +x = x+0.2; +console.log('x : ', x, typeof x); + +x= parseInt('4'); +console.log('x : ', x, typeof x); + +var y = parseFloat('2.345'); +console.log('y : ', y, typeof y); + +var z = parseFloat('BAITED'); +console.log('z : ', z, typeof z); \ No newline at end of file From 1efbcff4003d8a2334f668a0e9125cfb6c7174bb Mon Sep 17 00:00:00 2001 From: Etienne Date: Fri, 28 Oct 2016 11:23:43 +0200 Subject: [PATCH 5/5] Merge branch 'master' of https://github.com/nicolas-zozol/javascript-training into dev # Conflicts: # src/start.html # src/start.js # src/types/strings.js --- src/prototype/bind.js | 32 +++++++++++++++++++--- src/prototype/priority.js | 25 +++++++++++++++--- src/prototype/prototype.js | 45 ++++++++++++++++++++++++++++++- src/prototype/this.js | 30 ++++++++++++++++++--- work/ajax.js | 21 +++++++++++++++ work/jquery-events.js | 47 ++++++++++++++++++++++----------- work/work.html | 54 +++++++++++++++++++++++++++++++++++--- 7 files changed, 226 insertions(+), 28 deletions(-) create mode 100644 work/ajax.js diff --git a/src/prototype/bind.js b/src/prototype/bind.js index 7cfbe9e..efb1410 100644 --- a/src/prototype/bind.js +++ b/src/prototype/bind.js @@ -1,3 +1,29 @@ -/** - * Created by AELION on 26/10/2016. - */ +function Dinosaurus(name) { + + this.size = 12; + this.name = name; +} + +Dinosaurus.prototype.age = -6500000; + +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); + +boundToDenver = eating.bind(denver); +eating(tRex); +console.log('bound eating to denver '); +boundToDenver(tRex); + +eating.bind(denver)(tRex) \ No newline at end of file diff --git a/src/prototype/priority.js b/src/prototype/priority.js index 7cfbe9e..9e40b06 100644 --- a/src/prototype/priority.js +++ b/src/prototype/priority.js @@ -1,3 +1,22 @@ -/** - * Created by AELION on 26/10/2016. - */ +function Dinosaurus(name) { + + this.size = 12; + this.name = name; +} + +Dinosaurus.prototype.age = -6500000; +Dinosaurus.prototype.size = 20; + + +const denver = new Dinosaurus('Denver'); +// new give access to a prototype + +const petitPied = new Dinosaurus('Petit Pied'); +console.log('denver size : ', denver.size); + +// Denver object is built from prototype +// But denver has no prototype :( +// console.log('denver prototype size : ', denver.prototype.size); + +denver.size = 150; +console.log('news denver size : ', denver.size); \ No newline at end of file diff --git a/src/prototype/prototype.js b/src/prototype/prototype.js index 7cfbe9e..3b85688 100644 --- a/src/prototype/prototype.js +++ b/src/prototype/prototype.js @@ -1,3 +1,46 @@ /** - * Created by AELION on 26/10/2016. + * Note that we have here a Uppercase + * It's just a convention + * */ + +function Dinosaurus(name) { + + this.size = 12; + this.name = name; +} + +Dinosaurus.prototype.age = -6500000; + +const denver = new Dinosaurus('Denver'); +// new give access to a prototype + +const petitPied = new Dinosaurus('Petit Pied'); +petitPied.size = 3; +//console.log('denver name : ', denver.name); +//console.log('denver size : ', denver.size); +//console.log('petitPied size : ', petitPied.size); +//console.log('denver age : ', denver.age); +//console.log('petitPied 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); diff --git a/src/prototype/this.js b/src/prototype/this.js index 7cfbe9e..864bc7a 100644 --- a/src/prototype/this.js +++ b/src/prototype/this.js @@ -1,3 +1,27 @@ -/** - * Created by AELION on 26/10/2016. - */ +function Dinosaurus(name) { + + this.size = 12; + this.name = name; +} + +Dinosaurus.prototype.age = -6500000; + +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/work/ajax.js b/work/ajax.js new file mode 100644 index 0000000..a299987 --- /dev/null +++ b/work/ajax.js @@ -0,0 +1,21 @@ +$.get('http://localhost:3000/api/users/', function (users) { + console.log('reveive user ', users); + /*console.log(users.sort(function (user1, user2) { + return users1.name < user2.name ? -1 : 1; + }));*/ + users.sort((user1, user2) => user1.name < user2.name ? -1 : 1); + + displayUsers(users); +}); + +$.get('/api/users/admins/', function (admins) { + console.log('receive admins', admins.map(admin => admin.name)); +}); +/* + $.get('http://localhost:3000/api/users/', function (users) { + console.log('reveive users ', users); + const pUsers = $('

').text(users.map(users => users.name)); + $('section.users').append(pUsers); + console.log('receive users name ', users.map(users => users.name)); + }); + }*/ \ No newline at end of file diff --git a/work/jquery-events.js b/work/jquery-events.js index c11fb52..e20bca2 100644 --- a/work/jquery-events.js +++ b/work/jquery-events.js @@ -1,17 +1,17 @@ - /* contents.forEach(function (content) { - const node = document.createTextNode(content); - document.querySelector('body').appendChild(node); - })*/ - function displaySearch(term) { - clear('section.search'); - var contents = search(term); - contents.forEach(function (content) { - const pElement = $('

').text(content); - $('section.search').append(pElement); - - }); - - } +/* contents.forEach(function (content) { + const node = document.createTextNode(content); + document.querySelector('body').appendChild(node); + })*/ +function displaySearch(term) { + clear('section.search'); + var contents = search(term); + contents.forEach(function (content) { + const pElement = $('

').text(content); + $('section.search').append(pElement); + + }); + +} function displaySearchValue() { displaySearch($('input.search').val()); @@ -22,4 +22,21 @@ function clear(location) { $(location).empty(); } -$('button').on('click', displaySearchValue) ; +$('button').on('click', displaySearchValue); + + + +function displayUsers(users) { + // clear('section.users'); + const block = $('

    '); + users.forEach(function (user) { + const pUsers = $('
  • ').text(user.name); + block.append(pUsers); + }); + $('section.users').append(block); +} +function displayUsersValue() { + + displayUser($('input.users').val()); + +} diff --git a/work/work.html b/work/work.html index f7e5884..26ad9df 100644 --- a/work/work.html +++ b/work/work.html @@ -1,10 +1,58 @@ - + - $Title$ + + Displaying amazing work + + + -$END$ +

    Connected Users

    +
    + +
    + + +

    + This is the main event of the EVENING +

    + + +

    + Voici le resultat de la recherche :
    +

    + + +
    + + + +
    + + + + +
    + This is it ! +
    + + + + + + \ No newline at end of file