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 @@ Starting JS - -

Hello Javascript

+

Hello Javascript

-

You are so nice !

+

You are so nice !

@@ -18,24 +17,25 @@

Hello Javascript

+ function fact(x) { + var result = 1; + if (x < 0) { + return "ERREUR ! Le nombre doit être supérieur ou égal à 0."; + } + + else if (x == 0) { + return 1; + } + + for (var i = 1; i < x + 1; i++) { + result *= i; + } + return result; + } + + console.log(fact(5)); - \ No newline at end of file diff --git a/src/start.js b/src/start.js index d7b9b28..e3375bf 100644 --- a/src/start.js +++ b/src/start.js @@ -1,2 +1,2 @@ -var x=2+2; +var x = 2+2; console.log(x); diff --git a/src/types/arrays.js b/src/types/arrays.js index e69de29..54056f1 100644 --- a/src/types/arrays.js +++ b/src/types/arrays.js @@ -0,0 +1,77 @@ +var x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; + +// console.log('first :', x[0]); + +// console.log('third :', x[2]); +x[2] = -42; // modification du 3eme élément +// console.log('third :', x[2]); + +// déviation (ne pas faire) +x[-2] = "what ????"; +// console.log('found :', x[-2]); + +// console.log('all table', x); +x[99] = 140; +// console.log('big table now :', x); + + +/* Let's start over and serious stuff */ +x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; + + +// *********** Sorted Array *********** +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); + +// Elvis operator +var sortFunction = (a, b) => a < b ? -1 : 1; +x.sort(sortFunction); +console.log('ES 2015 sorted', x); + + +// *********** Filtered Array *********** +var filtered = x.filter(function (number) { + return number >= 0; +}); +console.log('filtered x : ', filtered); + +// Fat Arrow +var filteredFunction = (number) => number >= 0; +filtered = x.filter(filteredFunction); +console.log('filteredFunction : ', filtered); + +// Equivalent direct +direct = x.filter(number => number >= 0); + +// *********** Ajout/Supression d'éléments dans le tableau *********** + +x = [0, 3, 5, 6, 10, 12, -12, 45, 34]; +console.log('tableau de x : ', x); + +// Remove last element +var lastElement = x.pop(); +console.log('tableau de x : ', x); + +// Add last element +x.push(50); +console.log('tableau de x : ', x); + +// Remove first element +var firstElement = x.shift(); +console.log('tableau de x : ', x); + +// Add first element +x.unshift(10); +console.log('tableau de x : ', x); \ No newline at end of file diff --git a/src/types/boolean.js b/src/types/boolean.js new file mode 100644 index 0000000..8cda64c --- /dev/null +++ b/src/types/boolean.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/cases.js b/src/types/cases.js new file mode 100644 index 0000000..dc91636 --- /dev/null +++ b/src/types/cases.js @@ -0,0 +1,11 @@ +// tolower vs toupper + +var string = 'My name is Bond' + +//to upper case : tous les caractères de la chaine deviennent des majuscules +var toUpper = string.toUpperCase(); +console.log('La phrase "',string, '" en majuscule, donne : "',toUpper,'".'); + +//to lower case : tous les caractères de la chaine deviennent des minuscules +var toLower = string.toLowerCase(); +console.log('La phrase "',string, '" en minuscule, donne : "',toLower,'".'); \ No newline at end of file diff --git a/src/types/number.js b/src/types/number.js index c9779be..186a259 100644 --- a/src/types/number.js +++ b/src/types/number.js @@ -1,14 +1,14 @@ var x =2; console.log(typeof x); -x=x+0.2; +x = x+0.2; console.log('x :', x, typeof x); -x=parseInt('4'); -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 ("ahahahah"); +var z = parseFloat('ha ha ha ha'); console.log('z :', z, typeof z); \ No newline at end of file diff --git a/src/types/objects.js b/src/types/objects.js index e69de29..3fa40de 100644 --- a/src/types/objects.js +++ b/src/types/objects.js @@ -0,0 +1,35 @@ +var x = {}; +console.log('x : ', x, typeof x); + +var john = { + name: 'John', + address: "London", + friend: { + name: 'Jim', + age: 12 + } + +}; + +console.log(john.name, 'lives in ', john.address); +console.log(john.friend.name,'is',john.friend.age); + + +var funkyCop = { + name : 'Robocop' +}; + +var enemy ={ + name : 'Dick' +}; + +funkyCop.foe = enemy; +console.log('funky :',funkyCop); +enemy.foe = funkyCop; +funkyCop.name = "Venere Robocop" +console.log('enemy :',enemy); + +enemy.dead = true; +delete funkyCop.foe; +console.log('funky now :', funkyCop); +console.log('enemy now :', enemy); \ No newline at end of file diff --git a/src/types/string.js b/src/types/string.js new file mode 100644 index 0000000..4c92092 --- /dev/null +++ b/src/types/string.js @@ -0,0 +1,93 @@ +var string = "Hello world"; + +string += " !"; +console.log('string :', string); + +var index1 = string.indexOf('lo'); +console.log('index of lo : ', index1); + +var index2 = string.indexOf('jo'); +console.log('index of lo : ', index2); + +function contains(haystack, needle) { + return haystack.indexOf(needle) >= 0; +} + +var isTrue = contains('Jack', 'ack'); +console.log('Est-ce que "ack" est contenu dans "Jack" ?', isTrue); +var isFalse = contains('Mac Donalds', 'ack'); +console.log('Est-ce que "ack" est contenu dans "Mac Donalds" ?', isFalse); +var isAlsoTrue = contains('Jim', 'Jim'); +console.log('Est-ce que "Jim" est contenu dans "Jim" ?', isAlsoTrue); + +//Equivalent +console.log('Jack is back'.includes('ack')); /*Exist since ES 2015*/ + +/* + * Regular Expression (/.../) + */ + +var hello = "Hello World !"; +var found = hello.search(/orl/); +var alsoFound = hello.search(/world/i); +console.log('found :', found, 'also :', alsoFound); + +// /i for case insensitive +var x = /(.)*(world)/i.test('Hello World !'); +console.log('x :', x); + +// it is conform +var strangelyTrue = /(.)*(world)/i.test('Hello World here'); +console.log('strangelyTrue :', strangelyTrue); + +// not conform : we MUST finish with "world" +var moreStrict = /(.)*(world)$/i.test('Hello World here'); +console.log('moreStrict :', moreStrict); + + + + +//Substring +var string = 'My name is Bond' + +var a = 3; +var b = 6; + +//return the sub string between two positions a and b +var withSubstring = string.substring(a, b); +function verifySubstring(element){ + if(element.length == b-a){ + return true; + } + else + return false; +} +console.log(withSubstring.length); +console.log(withSubstring,'verifySubstring ? : ', verifySubstring(withSubstring)); + + +// return the sub string of a length b from the position a +var withSubstr = string.substr(a, b); +function verifySubstr(string, element){ + if(element.length == b && string.indexOf(element) == a){ + return true; + } + else + return false; +} + +console.log(withSubstr.length); +console.log('verifySubst ? : ', verifySubstr(string, withSubstr)); + + +// tolower vs toupper + +var string = 'My name is Bond' + +//to upper case +var toUpper = string.toUpperCase(); +console.log('La phrase "',string, '" en majuscule, donne : "',toUpper,'".'); + +//to lower case +var toLower = string.toLowerCase(); +console.log('La phrase "',string, '" en minuscule, donne : "',toLower,'".'); \ No newline at end of file diff --git a/src/types/substring.js b/src/types/substring.js new file mode 100644 index 0000000..5919d29 --- /dev/null +++ b/src/types/substring.js @@ -0,0 +1,32 @@ +//Substring +var string = 'My name is Bond' + +var a = 3; +var b = 6; +console.log('a =',a,'et b =',b); + +//return the sub string between two positions a and b +var withSubstring = string.substring(a, b); +function verifySubstring(element){ + if(element.length == b-a){ + return true; + } + else + return false; +} +console.log('La fonction Substring renvoit "',withSubstring,'" qui est de taille', withSubstring.length); +console.log(withSubstring,'verifySubstring ? : ', verifySubstring(withSubstring)); + + +// return the sub string of a length b from the position a +var withSubstr = string.substr(a, b); +function verifySubstr(string, element){ + if(element.length == b && string.indexOf(element) == a){ + return true; + } + else + return false; +} + +console.log('La fonction Substr renvoit "',withSubstr,'" qui est de taille',withSubstr.length); +console.log('verifySubst ? : ', verifySubstr(string, withSubstr)); \ No newline at end of file diff --git a/src/types/typeof.js b/src/types/typeof.js index e69de29..97db418 100644 --- a/src/types/typeof.js +++ b/src/types/typeof.js @@ -0,0 +1,7 @@ +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 diff --git a/src/work/ajax.js b/src/work/ajax.js new file mode 100644 index 0000000..c730a1e --- /dev/null +++ b/src/work/ajax.js @@ -0,0 +1,30 @@ +$.get('http://localhost:3000/api/users/', function(users){ + console.log(users.sort((user1, user2) => user1.name < user2.name ? -1 : 1)); + // console.log(users.sort((user1, user2) => (user1.name).localeCompare(user2.name))); + displayUsers(users); +}); + + +$.get('/api/users/admins/', function(admins){ + console.log('admins', admins.map(admin => admin.name)); +}); + + +function displayUsers(users) { + const block=$('