Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/functions/ESS.js
Original file line number Diff line number Diff line change
@@ -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<b ? -1 : 1;
fromPenny.sort(sortFunction);
console.log(fromPenny);

console.log('===============================================================');

var contentVio = [];

topics.forEach(function(topic){
return topic.comments.forEach(function(comment){
if(comment.tags !== undefined ){
if(comment.tags.includes('violence') && !comment.user.admin){
contentVio.push(comment.content);
}
}
})
});

console.log(contentVio);

console.log('===============================================================');

var searched = [];
function search(term) {
topics.forEach(function (topic) {
topic.comments.forEach(function (comment) {
if (comment.content.toLowerCase().includes(term.toLowerCase())) {
searched.push(comment.content);
}
})
});
return searched;
}

console.log('search is present in :', search('it'));
25 changes: 25 additions & 0 deletions src/functions/closure.js
Original file line number Diff line number Diff line change
@@ -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
return a.age + b.age; // pure : for some 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);
};

unpure();
23 changes: 23 additions & 0 deletions src/functions/foreach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var users = require('../data').users;

users.forEach(function(user){
console.log('user name : ', user.name);
});

users.forEach(user => 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);


18 changes: 18 additions & 0 deletions src/functions/format.js
Original file line number Diff line number Diff line change
@@ -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));

39 changes: 39 additions & 0 deletions src/functions/operations.js
Original file line number Diff line number Diff line change
@@ -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));
25 changes: 25 additions & 0 deletions src/functions/sort.js
Original file line number Diff line number Diff line change
@@ -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));
26 changes: 26 additions & 0 deletions src/prototype/bind.js
Original file line number Diff line number Diff line change
@@ -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);
19 changes: 19 additions & 0 deletions src/prototype/priority.js
Original file line number Diff line number Diff line change
@@ -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);

43 changes: 43 additions & 0 deletions src/prototype/prototype.js
Original file line number Diff line number Diff line change
@@ -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);
26 changes: 26 additions & 0 deletions src/prototype/this.js
Original file line number Diff line number Diff line change
@@ -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 : {}

Loading