From e0af3244dd1f31286003061636c9ca8f008f7f23 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 24 Oct 2016 10:25:22 +0200 Subject: [PATCH 1/5] myfirstpush --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ From 29e4b82e858cc41646c00c553e287213587a2f32 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 24 Oct 2016 15:05:48 +0200 Subject: [PATCH 2/5] exsubstring --- src/start.html | 40 ++++++++++++++++++++++++++++ src/start.js | 3 +++ src/types/number.js | 14 ++++++++++ src/types/string.js | 63 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 src/start.js create mode 100644 src/types/number.js create mode 100644 src/types/string.js diff --git a/src/start.html b/src/start.html index f3e471e..d5bacb4 100644 --- a/src/start.html +++ b/src/start.html @@ -3,8 +3,48 @@ 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..e85865d --- /dev/null +++ b/src/start.js @@ -0,0 +1,3 @@ +var x = 2+2; +console.log(x); + diff --git a/src/types/number.js b/src/types/number.js new file mode 100644 index 0000000..8267d3e --- /dev/null +++ b/src/types/number.js @@ -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('ah ah ah'); +console.log('z', z, typeof z); \ No newline at end of file diff --git a/src/types/string.js b/src/types/string.js new file mode 100644 index 0000000..cecc14d --- /dev/null +++ b/src/types/string.js @@ -0,0 +1,63 @@ +var string = "Hello World"; + +string += " !"; + +console.log('string : ', string); + +var index = string.indexOf('lo'); +console.log('index of lo : ', index); + +var index = string.indexOf('jo'); +console.log('index of jo : ', index); + +function contains(haystack, needle){ + if(haystack.indexOf(needle)==-1){ + return false; + }else{ + return true; + } +} + +//return (haystack.indexOf(needle)>=0; + +var isTrue = contains('Jack', 'ack'); +var isFalse = contains('Mac Donalds', 'ack'); +var isAlsoTrue = contains('Jim', 'Jim'); + +console.log(isTrue, isFalse, isAlsoTrue); + +console.log('Jack is back'.includes('ack')); //que depuis ES2015 + + +/** + * Expression Reguliere + */ + +var hello = "Hello World"; +var found = hello.search(/orl/); +var alsoFound = hello.search(/world/i); // /i = case insensitive +console.log('found : ', found, 'also : ', alsoFound); + +var x = /(.)*(world)/i.test('Hello World'); +console.log('x : ', x); + +var y = /(.)*(world)/i.test('Hello World here'); +console.log('y : ', y); +//true : contient au moins la string de base + +var z = /(.)*(world)$/i.test('Hello World here'); +console.log('z : ', z); +//false avec l'ajout du $ : contient strictement la string de base à la fin + +//Substring + +//substr + +var test = "utilisation"; + +// substr from char number 3 with a length of 5 char +console.log(test.substr(3, 5)); + + +// substring from char number 5 to char number 8 +console.log(test.substring(5, 8)); \ No newline at end of file From 3860ff32d42bf7d4eb969f08d1d949cafc3995d0 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 24 Oct 2016 15:26:26 +0200 Subject: [PATCH 3/5] toloweruppertrim --- src/types/string.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/types/string.js b/src/types/string.js index cecc14d..45d1e0d 100644 --- a/src/types/string.js +++ b/src/types/string.js @@ -60,4 +60,22 @@ console.log(test.substr(3, 5)); // substring from char number 5 to char number 8 -console.log(test.substring(5, 8)); \ No newline at end of file +console.log(test.substring(5, 8)); + + + +var original = "ORIGinal"; + +var tolower = original.toLowerCase() == "original"; +console.log(tolower); +//true + +var toupper = original.toUpperCase() == "ORIGINAL"; +console.log(toupper); +//true + + +var aerien = " superaérien "; +console.log(aerien.trim() == "superaérien"); +//true + From 6e7b1b1456107f58354155f8778d56135f2862c0 Mon Sep 17 00:00:00 2001 From: Sarah Date: Tue, 25 Oct 2016 16:16:51 +0200 Subject: [PATCH 4/5] Because I'm Happy ! --- src/functions/ESS.js | 100 ++++++++++++++++++++++++++++++++++++ src/functions/closure.js | 25 +++++++++ src/functions/foreach.js | 23 +++++++++ src/functions/format.js | 18 +++++++ src/functions/operations.js | 39 ++++++++++++++ src/functions/sort.js | 25 +++++++++ src/types/arrays.js | 67 ++++++++++++++++++++++++ src/types/booleans.js | 4 ++ src/types/objects.js | 40 +++++++++++++++ src/types/typeof.js | 12 +++++ 10 files changed, 353 insertions(+) create mode 100644 src/functions/ESS.js create mode 100644 src/functions/closure.js create mode 100644 src/functions/foreach.js create mode 100644 src/functions/format.js create mode 100644 src/functions/operations.js create mode 100644 src/functions/sort.js create mode 100644 src/types/arrays.js create mode 100644 src/types/booleans.js create mode 100644 src/types/objects.js create mode 100644 src/types/typeof.js diff --git a/src/functions/ESS.js b/src/functions/ESS.js new file mode 100644 index 0000000..f2ff636 --- /dev/null +++ b/src/functions/ESS.js @@ -0,0 +1,100 @@ +var topics = require('../data').topics; + +var fromLeo = []; + +var result = topics.filter(function(topic){ + ///returns true if we keep this topic + return (topic.user.name === 'Leonard') +}); + +console.log(result); + +console.log('==============================================================='); + +var leofat = topics.filter(topic => topic.user.name === 'Leonard'); +console.log(leofat); + +console.log('==============================================================='); + +var titles = topics.map(function(topic){ + return topic.title; +}); +console.log('titles : ', titles); + +console.log('==============================================================='); + +var titlefat = topics.map(topic => topic.title); +console.log('fat titles : ',titlefat); + +console.log('==============================================================='); + +var violence = topics.some(function(topic){ + return topic.tags.includes('violence') +}); + +console.log(violence); + +console.log('==============================================================='); + +var fromSheldon = topics.filter(function(topic){ + return topic.comments.some(function(comment){ + return comment.user.name === 'Sheldon'; + }) +}); + +console.log(fromSheldon.map(topic=>topic.title)); + +console.log('==============================================================='); + +var fatSheldon = topics.filter(topic => topic.comments.some(comment => comment.user.name === 'Sheldon')).map(topic=>topic.title); + +console.log(fatSheldon); + +console.log('==============================================================='); + + +var fromPenny = []; + +topics.forEach(function(topic){ + return topic.comments.forEach(function(comment){ + if(comment.user.name === 'Penny'){ + fromPenny.push(comment.id); + }; + }); +}); + +var sortFunction = (a,b) => a console.log('fat arrow user : ',user.name)); + +var admins = []; +var noAdmins = []; +users.forEach(function(user){ + if(user.admin){ + admins.push(user.name); + }else{ + noAdmins.push(user.name); + } +}); + +//users.forEach(user => user.admin ? admins.push(user.name) : noAdmins.push(user.name)); + +console.log('admins : ',admins, ' /// not admin : ', noAdmins); + + diff --git a/src/functions/format.js b/src/functions/format.js new file mode 100644 index 0000000..ef39f34 --- /dev/null +++ b/src/functions/format.js @@ -0,0 +1,18 @@ +// une fonction est disponible quelque soit son emplacement dans le fichier +function add(a,b){ + return a+b; +}; + +console.log('2 + 3 : ' ,add(2,3)); + +// la variable n'est disponible qu'au moment de sa lecture +var mult = function(a,b){ + return a*b; +}; + +console.log('4 * 3 : ',mult(4,3)); + +var divide = (a, b) => a/b; + +console.log('10 / 5 : ' ,divide(10,5)); + diff --git a/src/functions/operations.js b/src/functions/operations.js new file mode 100644 index 0000000..e766737 --- /dev/null +++ b/src/functions/operations.js @@ -0,0 +1,39 @@ +function add(a,b) { + return a+b; +}; + +function divide(a,b){ + if(b!==0){ + return a/b; + } +}; + +function multiply(a,b){ + return a*b; +}; + +var minus = function(a,b){ + return a-b; +}; + +console.log('a=2 et b=6 : ' , 'a+b = ', add(2,6) ,',a/b = ', divide(2,6),',a*b = ',multiply(2,6),',a-b = ', minus(2,6)); + +function operation(op, x, y){ + return op(x,y); +}; + +console.log('add ', operation(add, 56 ,59)); +console.log('divide ', operation(divide, 56 ,59)); +console.log('multiply ', operation(multiply, 56 ,59)); +console.log('minus ', operation(minus, 56 ,59)); + +var ops = [add, divide, multiply, minus]; +var random = ops[Math.floor(Math.random()*ops.length)]; +console.log(random.name, operation(random,56,59)); + +var data = [4,3]; + +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..496f098 --- /dev/null +++ b/src/functions/sort.js @@ -0,0 +1,25 @@ +var x = [0,3,5,6,10,12,-12,45,34]; +console.log(x); + +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; +//console.log('users', users); + +function sortUsers(user1, user2){ + if(user1.name === user2.name){ + return 0; + } + return user1.name < user2.name ? -1 : 1; +} + +users.sort(sortUsers); +console.log(users.map(user => user.name)); diff --git a/src/types/arrays.js b/src/types/arrays.js new file mode 100644 index 0000000..692005d --- /dev/null +++ b/src/types/arrays.js @@ -0,0 +1,67 @@ +/*var x = [0,3,5,6,10,12,-12,45,34]; + +console.log('first : ', x[0]); + +x[2] = -42; //modification de la 3eme valeur du tableau + +// deviation +x[-2] = "what ???"; +console.log('found : ',x[-2]); + +console.log('all table', x); +x[150] = 149; +console.log('big table now : ', x); + + on recommence en plus sérieux*/ + +var x = [0,3,5,6,10,12,-12,45,34]; + +x.sort(); +console.log('x sorted : ',x); + +x.sort(function(a,b){ + if(ab){ + return 1; + } + return 0; +}); + +console.log('x correctly sorted : ',x); + +var sortFunction = (a,b) => a= 0; +}); + +console.log('filtered x : ', filtered); + +var FAfiltered = (a) => a>0; +console.log(x.filter(FAfiltered)); + +direct = x.filter(number => number >=0); + +//add last element +x.push(50); +//x[x.length+1]=50; +console.log(x); + +//remove last element +var lastElement = x.pop(); +//x.splice(x.length-1); +console.log(x); + +//remove first element +var first = x.shift(); +console.log('first was : ', first, x); + +//add first element +x.unshift(first); +console.log('first is ', first, 'again', x); + + + diff --git a/src/types/booleans.js b/src/types/booleans.js new file mode 100644 index 0000000..97cba83 --- /dev/null +++ b/src/types/booleans.js @@ -0,0 +1,4 @@ +// 2=="2" -> BAD +// 2==="2" -> GOOD +// 2!=="2" -> ALSO GOOD + diff --git a/src/types/objects.js b/src/types/objects.js new file mode 100644 index 0000000..b326268 --- /dev/null +++ b/src/types/objects.js @@ -0,0 +1,40 @@ +var x = {}; +console.log('x ; ', x, typeof x); + +var john = { + name : 'John', + address : 'London', + friend : { + name : 'Jim', + age : 12 + } +}; + +console.log(john.address); +console.log(john.friend.name); + + +var funkyCop = { + name : 'Robocop' +}; + +var ennemy = { + name : 'Dick' +}; + +funkyCop.foe = ennemy; +console.log('funck', funkyCop); +ennemy.foe = funkyCop; +funkyCop.name = "Venere Robocop"; + +ennemy.dead = true; +delete funkyCop.foe; + +console.log('funky now : ', funkyCop); + +var strange = { + 'Azjda/°kk': "strange key" +}; + +console.log('strange : ', strange['Azjda/°kk']); +console.log('normal : ', funkyCop.name, funkyCop['name']); diff --git a/src/types/typeof.js b/src/types/typeof.js new file mode 100644 index 0000000..0f4bb8a --- /dev/null +++ b/src/types/typeof.js @@ -0,0 +1,12 @@ +console.log(typeof 'I am a string'); + +console.log(typeof 2); + +console.log(typeof {name: 'Jo'}); + +console.log(typeof true); + +console.log(typeof [2,4]); + +var myArray = [2,4]; +console.log('is array ? ', Array.isArray(myArray), Array.isArray({name: 'Jim'})); \ No newline at end of file From d22017f6f25d73b9f7b83073280e7e0633f466bb Mon Sep 17 00:00:00 2001 From: Sarah Date: Fri, 28 Oct 2016 11:29:36 +0200 Subject: [PATCH 5/5] jQuery Ajax --- src/prototype/bind.js | 26 ++++++++++++++++++++++ src/prototype/priority.js | 19 ++++++++++++++++ src/prototype/prototype.js | 43 +++++++++++++++++++++++++++++++++++++ src/prototype/this.js | 26 ++++++++++++++++++++++ src/work/ajax.js | 20 ++++++++++++++--- src/work/jquery-event.js | 44 ++++++++++++++++++++++++++++++++++++++ src/work/work.html | 33 ++++++++++++++++++++++++++-- 7 files changed, 206 insertions(+), 5 deletions(-) diff --git a/src/prototype/bind.js b/src/prototype/bind.js index e69de29..c228e0d 100644 --- a/src/prototype/bind.js +++ b/src/prototype/bind.js @@ -0,0 +1,26 @@ +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); + +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/prototype/priority.js b/src/prototype/priority.js index e69de29..a1ff3de 100644 --- a/src/prototype/priority.js +++ b/src/prototype/priority.js @@ -0,0 +1,19 @@ +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); + diff --git a/src/prototype/prototype.js b/src/prototype/prototype.js index e69de29..d7571eb 100644 --- a/src/prototype/prototype.js +++ b/src/prototype/prototype.js @@ -0,0 +1,43 @@ +//note that we have here an 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('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 e69de29..8ed5616 100644 --- a/src/prototype/this.js +++ b/src/prototype/this.js @@ -0,0 +1,26 @@ +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 : {} + diff --git a/src/work/ajax.js b/src/work/ajax.js index a8ea34d..a6dffe5 100644 --- a/src/work/ajax.js +++ b/src/work/ajax.js @@ -1,3 +1,17 @@ -/** - * Created by AELION on 27/10/2016. - */ +$.get('http://localhost:3000/api/users/', function(users){ + console.log('receive users data', users); + console.log(users.sort(function(user1, user2){ + //users.sort((user1, user2) => user1.name < user2.name ? -1 : 1); + if(user1.name === user2.name){ + return 0; + } + return user1.name < user2.name ? -1 : 1; + })); + displayUsers(users); +}); + +$.get('http://localhost:3000/api/users/admins', function(admins){ + console.log('receive admins data', admins.map(admin => admin.name)); +}); + + diff --git a/src/work/jquery-event.js b/src/work/jquery-event.js index e69de29..e12b0af 100644 --- a/src/work/jquery-event.js +++ b/src/work/jquery-event.js @@ -0,0 +1,44 @@ +/*const contents=search('it'); + contents.forEach(function(content){ + const node = document.createTextNode(content); + document.querySelector('body').appendChild(node); + });*/ + + +function displaySearch(term){ + clearSearch(); + var contents = search(term); + + console.log(contents.join(' ')); + + contents.forEach(function(content){ + const pElement = $('

').text(content); + $('section.search').append(pElement); + }); +}; + +function displaySearchValue(){ + displaySearch($('input.search').val()); +}; + +function clearSearch(){ + $('section.search').empty(); +}; + +$('button').on('click', displaySearchValue); +/*const term=$('input.search'); + console.log('term', term, term.val()); + displaySearch(term.val());*/ +//displaySearch(); + +function displayUsers(users){ + const block = $('

    '); + users.forEach(function(user){ + const uElement = $('
  • ').text(user.name); + block.append(uElement); + }); + $('section.users').append(block); +}; + + + diff --git a/src/work/work.html b/src/work/work.html index f7e5884..fb62890 100644 --- a/src/work/work.html +++ b/src/work/work.html @@ -2,9 +2,38 @@ - $Title$ + Displaying amazing work -$END$ + +

    Connected Users

    +
    + + +
    + +

    Du texte, du texte, encore du texte !

    + + +
    + + + + +
    + + + + +Limite basse ______________ + + + + + + + \ No newline at end of file