From 299a85a603c3d96ab1e05fc9e727ebeb445d826f Mon Sep 17 00:00:00 2001 From: Marie Date: Mon, 24 Oct 2016 10:25:19 +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 6bb7dbaebdb6a21699a18822a5e64ecc89a56cd9 Mon Sep 17 00:00:00 2001 From: Marie Date: Mon, 24 Oct 2016 14:59:31 +0200 Subject: [PATCH 2/5] difference between substr ad substring --- src/start.html | 37 ++++++++++++++++++++++++++++++++ src/start.js | 2 ++ src/types/number.js | 15 +++++++++++++ src/types/strings.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/start.js create mode 100644 src/types/number.js create mode 100644 src/types/strings.js diff --git a/src/start.html b/src/start.html index f3e471e..640f3e5 100644 --- a/src/start.html +++ b/src/start.html @@ -3,8 +3,45 @@ 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/number.js b/src/types/number.js new file mode 100644 index 0000000..d171224 --- /dev/null +++ b/src/types/number.js @@ -0,0 +1,15 @@ +var x = 2; +console.log(typeof x); + +x=x+0.2; +console.log('x : ',x, typeof x); + +x=parseInt('4'); //récupère la valeur numérique du texte// +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); + diff --git a/src/types/strings.js b/src/types/strings.js new file mode 100644 index 0000000..c1f851a --- /dev/null +++ b/src/types/strings.js @@ -0,0 +1,51 @@ +var string = "Hello world"; +string += "!"; +console.log('string :', string); + +var index = string.indexOf('lo'); +console.log('index of lo :', index); + +function contains(haystack, needle) { + var index = haystack.indexOf(needle); + if (index >= 0) { + return true; + } + else { + return false; + } + //résumé : 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); +//en fait la fonction existait déjà, mais depuis ES6 en 2015// +console.log('Jack is back'.includes('ack')); + +/** + *Expression Reguliere (expression entre / / ) + */ + +var hello = "Hello world"; +var found = hello.search(/orl/); +var alsoFound = hello.search(/world/i);//i : pour la casse// +console.log('found :', found, 'also :', alsoFound); + +var x = /(.)*(world)/i.test('Hello world'); +console.log('x :',x); + +var strangelyTrue = /(.)*(world)/i.test('Hello Worlds here'); +console.log('strangelyTrue :', strangelyTrue); + +//not conforme : we must finish witj (world) (à cause du $) +var moreStrict=/(.)*(world)$/i.test('Hello world here'); +console.log('moreStrict: ',moreStrict); + +//substr +var test = "I love funkoPop"; +console.log('Print beginning at 1, for 4 characters',test.substr(1,4)); +console.log('Print beginning at 1',test.substr(1)); + +//substring +var test = "I love funkoPop"; +console.log('Print between index 1 and 4 ', test.substring(1,4)); \ No newline at end of file From 4624d027014c3a769853388f9d6709ddbac835e0 Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 25 Oct 2016 16:16:45 +0200 Subject: [PATCH 3/5] Happy of my wonderful work! --- src/functions/closure | 26 ++++++++++++ src/functions/es5.js | 79 +++++++++++++++++++++++++++++++++++++ src/functions/foreach.js | 17 ++++++++ src/functions/formats.js | 16 ++++++++ src/functions/operations.js | 31 +++++++++++++++ src/functions/sort.js | 24 +++++++++++ src/types/arrays.js | 69 ++++++++++++++++++++++++++++++++ src/types/boolean.js | 3 ++ src/types/objects.js | 34 ++++++++++++++++ src/types/strings.js | 22 ++++++++++- src/types/typeof.js | 10 +++++ 11 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 src/functions/closure 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/operations.js create mode 100644 src/functions/sort.js create mode 100644 src/types/arrays.js create mode 100644 src/types/boolean.js create mode 100644 src/types/objects.js create mode 100644 src/types/typeof.js diff --git a/src/functions/closure b/src/functions/closure new file mode 100644 index 0000000..249316d --- /dev/null +++ b/src/functions/closure @@ -0,0 +1,26 @@ +var denver ={ + name : 'Denver', + age : 12 +} + +var petitPied = { + name: 'Petit pied', + age : -6500000 +} + +function pure(a,b) { + //a.age= 15; NOT PURE + + return a.age + b.age; //PURE : for somre input, will always return a value +} + +console.log('pure :', pure(denver,petitPied)); + +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); + //a closure : to avoid +} +unpure; \ No newline at end of file diff --git a/src/functions/es5.js b/src/functions/es5.js new file mode 100644 index 0000000..3ec8c79 --- /dev/null +++ b/src/functions/es5.js @@ -0,0 +1,79 @@ +var topics = require('../data').topics; + +var result = topics.filter(function (topic) { + //reutrn true if we keep this topic + return topic.user.name === 'Leonard'; +}); +console.log("Leonard's comments", result); + +result = topics.filter(topic => topic.user.name === 'Leonard'); +console.log("Leonard's comments2", result); + +var titles = topics.map(function (topic) { + return topic.title; +}); +console.log('titles', titles); + +titles = topics.map(topic => topic.title); +console.log('titles', titles); + +console.log('##########'); +var hasViolence = topics.some(function (topic) { + return topic.tags.includes('violence') +}); +console.log('has violence', hasViolence); + +console.log('##########'); + +//1.topic titles where sheldon wrote at least one comment +var filtered = topics.filter(function (topic) { + return topic.comments.some(function (comment) { + return comment.user.name === 'Sheldon'; + }); +}); +console.log('number of filtered topics', filtered.length); + + +var titles = filtered.map(function (topic) { + return topic.title; +}); + + +//FAT ARROW + +titles = (topics.filter(topic=>topic.comments.some(comment =>comment.user.name === 'Sheldon'))).map(topic=>topic.title); +console.log("Topics' names", titles); + +//2.Sorted id of Penny's comment +var titlesId = []; +topics.forEach(function (topic) { + topic.comments.forEach(function (comment) { + if (comment.user.name === 'Penny') { + titlesId.push(comment.id); + } + }); +}); +console.log('id', titlesId); + +var sortFunction = (a, b) => a < b ? -1 : 1; +titlesId.sort(sortFunction); +console.log("id sorted of Penny's comment", titlesId); + +//3.Contents of the comments written by a standard user, tagged 'violence' +var filteredContent = []; +topics.forEach(function (topic) { + topic.comments.forEach(function (comment) { + if (comment.tags !== undefined && !comment.user.admin) { + comment.tags.forEach(function (tag) { + if (tag === 'violence') { + filteredContent.push(comment.content); + } + }); + } + ; + }); +}); +console.log('violent contents', filteredContent); + +//4.search + diff --git a/src/functions/foreach.js b/src/functions/foreach.js new file mode 100644 index 0000000..04e52bd --- /dev/null +++ b/src/functions/foreach.js @@ -0,0 +1,17 @@ +var users = require('../data').users; + +users.forEach(function (user) { + console.log('user name', user.name); +}); + +console.log('#####'); +users.forEach(user => console.log('user name', user.name)); +//not very good, because there is not a result, or that's what we attend with a fat arrow + +var admins = []; +users.forEach(function (user) { + if (user.admin === true){ + admins.push(user.name); + } +}); +console.log('admins', admins); \ No newline at end of file diff --git a/src/functions/formats.js b/src/functions/formats.js new file mode 100644 index 0000000..6432d98 --- /dev/null +++ b/src/functions/formats.js @@ -0,0 +1,16 @@ + +console.log('2+3', add(2,3)); + +//variable can't be under the consol.log +var mult = function (a,b) { + return a*b; +} +console.log('4*3 :', mult(4,3)); + +// function can be at the bottom +function add(a,b) { + return a+b; +} + +var divide = (a,b) => a/b; +console.log('6/3',divide(6,3)); \ No newline at end of file diff --git a/src/functions/operations.js b/src/functions/operations.js new file mode 100644 index 0000000..bf0c0bb --- /dev/null +++ b/src/functions/operations.js @@ -0,0 +1,31 @@ +function add(a, b) { + return a + b; +} +console.log('2 +3 =', add(2, 3)); + +function divide(a, b) { + return a / b; +} +console.log('6/3 =', divide(6, 3)); + +function multiply(a, b) { + return a * b; +} +console.log('2*6 :', multiply(2, 6)); + + +function minus(a, b) { + return a - b; +} +console.log('6-3', minus(6, 3)); + + +function operation(op, x, y) { + return op(x, y); +} +console.log('multpiply', operation(multiply, 56, 59)); + + +var ops = [add, divide, multiply, minus]; +var random= ops[Math.floor(Math.random() * ops.length)]; +console.log('?', operation(random, 56, 59)); \ No newline at end of file diff --git a/src/functions/sort.js b/src/functions/sort.js new file mode 100644 index 0000000..c40868d --- /dev/null +++ b/src/functions/sort.js @@ -0,0 +1,24 @@ +var x = [2, 8, 6, 4, 32, 75, -3, 34, 15]; + +var sortFunction = function (a, b) { + if (a === b) { + return 0; + } + return a < b ? -1 : 1; +}; + +console.log('array sorted', x.sort(sortFunction)); + +var users = require('../data').users; //function require is link to node, very specific +console.log('users',users); + + +function sortUsers(user1, user2) { + if (user1.name === user2.name) { + return 0; + } + return user1.nameuser.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..fd124c6 --- /dev/null +++ b/src/types/arrays.js @@ -0,0 +1,69 @@ +var x = [2, 8, 6, 4, 32, 75, -3]; + +//Add an element at the end + +x [8] = 50; //We have to now the size of the table +console.log('table', x); + +//Better way to insert +x.push(50); +console.log('tablebis', x); + +//remove last element frome array +var lastElement = x.pop();//delete last element from table and put it in lastElement +console.log('table3', x); + +//remove first element +x.shift(); +console.log('table4', x); + +//insert +x.unshift('I am the first'); +console.log('X after unshift', x); + +console.log('1er element', x[0]); +x[2] = -35; // modify array + +//deviation +x[-2] = "what??"; +console.log('found :', x[-2]); //Don't do it + +//console.log('all table', x); +//x[99] = 149; +//console.log('big table now :', x); + +/*Let's start over and do serious stuff*/ +x = [2, 8, 6, 4, 32, 75, -3]; + +x.sort(); +console.log('x sorted', x); + +x.sort(function (a, b) { //on définit pour la fonction comment calculer qui es tle plus petit et le plus grand// + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + return 0; +}) +console.log('x correcly sorted', x); + +var sortFunction = (a, b) => a < b ? -1 : 1; //on écrit la fonction de manière plus dense : function fat arrow +x.sort(sortFunction); +console.log('ES 2015 sorted', x); + +var z = 2<10? "Little" : "big" +//z = Little -> Elvis operator (else if...) + + + +var filtered = x.filter (function (number) { //x ne change pas, on cret un novueau tableau// + return number>=0; +}) +console.log('filtered x :', filtered); + +var fat = (number) => number>=0 ; +filtered = x.filter(fat); +console.log('filtered fat :', filtered); + +direct = x.filter(number=> number>=0); diff --git a/src/types/boolean.js b/src/types/boolean.js new file mode 100644 index 0000000..c2683e2 --- /dev/null +++ b/src/types/boolean.js @@ -0,0 +1,3 @@ +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..5ddac1a --- /dev/null +++ b/src/types/objects.js @@ -0,0 +1,34 @@ +var x = {}; +console.log('x', typeof x); + +var john = { + name : 'john', + address : 'London', + friend : { + name : 'Jim', + age : 12 + } +}; + +console.log("john's adress :", john.address); +console.log("John's friend's age :", 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 c1f851a..e801a23 100644 --- a/src/types/strings.js +++ b/src/types/strings.js @@ -48,4 +48,24 @@ console.log('Print beginning at 1',test.substr(1)); //substring var test = "I love funkoPop"; -console.log('Print between index 1 and 4 ', test.substring(1,4)); \ No newline at end of file +console.log('Print between index 1 and 4 ', test.substring(1,4)); + +//to uppercase +var test = "I love funkoPop"; +console.log('the title is in uppercase', test.toUpperCase()); + +//to lowercase +var test = "I HATE FUNKOPOP"; +console.log('the title is in lowercase', test.toLowerCase()); + +//trim +var test = " I love funkoPop "; +console.log('" I love funkoPop "with trim',test.trim()); + + + +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..0a05a6b --- /dev/null +++ b/src/types/typeof.js @@ -0,0 +1,10 @@ +console.log(typeof'I am a string'); + +console.log(typeof 2); +console.log(typeof {name:'Jo'}); + +console.log(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 595434632156d39289e38fd1373c7615b489c29b Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 27 Oct 2016 16:12:01 +0200 Subject: [PATCH 4/5] jquery, before Ajax --- src/functions/closure.js | 2 +- src/prototype/bind.js | 27 ++++++++++++++++++++++++ src/prototype/priority.js | 18 ++++++++++++++++ src/prototype/prototype.js | 42 ++++++++++++++++++++++++++++++++++++++ src/prototype/this.js | 26 +++++++++++++++++++++++ src/work/search.js | 37 +++++++++++++++++++++++++++++++++ src/work/work.html | 30 +++++++++++++++++++++++++-- src/work/work.js | 2 ++ 8 files changed, 181 insertions(+), 3 deletions(-) diff --git a/src/functions/closure.js b/src/functions/closure.js index 249316d..b143682 100644 --- a/src/functions/closure.js +++ b/src/functions/closure.js @@ -21,6 +21,6 @@ function unpure() { //capture the external petitPied object //depends on external context, and not only params console.log('kick', petitPied.name); - //a closure : to avoid + //a closure.js : to avoid } unpure; \ No newline at end of file diff --git a/src/prototype/bind.js b/src/prototype/bind.js index e69de29..e078d13 100644 --- a/src/prototype/bind.js +++ b/src/prototype/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); + +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..676357e 100644 --- a/src/prototype/priority.js +++ b/src/prototype/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'); +//new give access to a prototype + +const petitPied = new Dinosaurus('Petit Pied'); + +console.log('denver size', denver.size); +//console.log('denver prototype size', denver.prototype.size);//denver est un objet et un objet n'a pas de prototype. +denver.size=150; +console.log('new denver size :',denver.size); + diff --git a/src/prototype/prototype.js b/src/prototype/prototype.js index e69de29..f5cac03 100644 --- a/src/prototype/prototype.js +++ b/src/prototype/prototype.js @@ -0,0 +1,42 @@ +/** + * *Note that there is a Uppercase + * It's just a covnention + */ + + +function Dinosaurus(name) { + this.size = 12; + this.name = name; +} + +Dinosaurus.prototype.age = -65000000; + +const denver = new Dinosaurus('Denver'); +//new give access to a prototype + +const petitPied = new Dinosaurus('Petit Pied'); +petitPied.size = 3; + +console.log("Denver's name :", denver.name); +console.log('denver age :', denver.age); +console.log('Petit Pied 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(); + rex.eat(petitPied); + diff --git a/src/prototype/this.js b/src/prototype/this.js index e69de29..1607aaa 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/search.js b/src/work/search.js index e69de29..1e46523 100644 --- a/src/work/search.js +++ b/src/work/search.js @@ -0,0 +1,37 @@ +/*const contents=search('it'); +contents.forEach(function (content) { + const node = document.createTextNode(content); + document.querySelector('body').appendChild(node); +}) +*/ + +// $('body').text(contents.join('')); + +function displaySearch(term) { + clearResult('section.search'); + var contents = search(term); + contents.forEach(function (content) { + const pElement = $('

').text(content); //là je demande la création d'un paragraphe en html et on y mets du texte + $('.search').append(pElement); + }); + +} +function displaySearchValue() { + displaySearch($('input.search').val()); +} + +function clearResult(localisation) { + $(localisation).empty(); +} + +$('button').on('click', displaySearchValue); +/*function () { + //const term = $('input.search'); + // console.log('test', term.val());//on veut la valeur du terme + // displaySearch(term.val()); + displaySearchValue(); + });*/ + + + + diff --git a/src/work/work.html b/src/work/work.html index f7e5884..ff5abeb 100644 --- a/src/work/work.html +++ b/src/work/work.html @@ -2,9 +2,35 @@ - $Title$ + Displaying amazing work -$END$ +

Les meilleurs moments du forum

+ +
+ +

+ De grandes discussions, des échanges de qualité et en bonus, les commentaires de nos utilisateurs!
+ Les voici : +

+ + + +
+ + + + + +
+ +
Bientôt d'autres exemples!
+ + + + + + \ No newline at end of file diff --git a/src/work/work.js b/src/work/work.js index 59f8517..19c61ef 100644 --- a/src/work/work.js +++ b/src/work/work.js @@ -276,3 +276,5 @@ console.log('search', search('it')); + + From a93204df4e5946f6df331acfcc00b28d34e9bb01 Mon Sep 17 00:00:00 2001 From: Marie Date: Fri, 28 Oct 2016 11:23:39 +0200 Subject: [PATCH 5/5] jquery Ajax --- src/work/ajax.js | 33 +++++++++++++++++++++++++++++++++ src/work/search.js | 1 + src/work/work.html | 13 ++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/work/ajax.js diff --git a/src/work/ajax.js b/src/work/ajax.js new file mode 100644 index 0000000..58e837b --- /dev/null +++ b/src/work/ajax.js @@ -0,0 +1,33 @@ +$.get('http://localhost:3000/api/users/', function (users) { + console.log('receive users', users); + // const user = $('

').text(users.map (user => user.name)); + //$('section.users').append(user); + /* users.sort(function (user1, user2) { + if (user1.name < user2.name) { + return -1; + } else if (user1.name > user2.name) { + return 1; + } + return 0; + });*/ + users.sort((user1, user2)=>user1.name'); + users.forEach(function (user) { + const pUser = $('

  • ').text(user.name); + block.append(pUser); + }); + $('section.users').append(block); +} + + +$.get('http://localhost:3000/api/users/admins/', function (admins) { + console.log('receive admin', admins); +}); + +$.get('http://localhost:3000/api/users/admins/', function (admins) { + console.log('receive admin', admins.map(admin => admin.name)); +}); \ No newline at end of file diff --git a/src/work/search.js b/src/work/search.js index 1e46523..05e6e17 100644 --- a/src/work/search.js +++ b/src/work/search.js @@ -7,6 +7,7 @@ contents.forEach(function (content) { // $('body').text(contents.join('')); + function displaySearch(term) { clearResult('section.search'); var contents = search(term); diff --git a/src/work/work.html b/src/work/work.html index ff5abeb..8571abb 100644 --- a/src/work/work.html +++ b/src/work/work.html @@ -5,7 +5,17 @@ Displaying amazing work -

    Les meilleurs moments du forum

    + +

    Connected users

    +
    + + +
    + + + + +

    Recherche sur le forum par mot-clé

    @@ -31,6 +41,7 @@

    Les meilleurs moments du forum

    + \ No newline at end of file