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

27 changes: 27 additions & 0 deletions src/functions/es5.js
Original file line number Diff line number Diff line change
@@ -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);
26 changes: 26 additions & 0 deletions src/functions/foreach.js
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 12 additions & 0 deletions src/functions/formats.js
Original file line number Diff line number Diff line change
@@ -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));
44 changes: 44 additions & 0 deletions src/functions/multifunction.js
Original file line number Diff line number Diff line change
@@ -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);


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


21 changes: 21 additions & 0 deletions src/functions/sort.js
Original file line number Diff line number Diff line change
@@ -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));
29 changes: 29 additions & 0 deletions src/prototype/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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)
22 changes: 22 additions & 0 deletions src/prototype/priority.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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);
46 changes: 46 additions & 0 deletions src/prototype/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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);
27 changes: 27 additions & 0 deletions src/prototype/this.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 . {}
9 changes: 4 additions & 5 deletions src/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

</head>
<body>

<h1> Hello Javascript</h1>

<p> You are so nice !</p>

<h1> Hello JavaScript</h1>
<p>
You are so nice !!
</p>
</body>

<script type="text/javascript" src="http://underscorejs.org/underscore.js">
Expand Down
2 changes: 1 addition & 1 deletion src/start.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
var x=2+2;
var x = 2+2;
console.log(x);
Loading