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 : -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.js : to avoid
}
unpure;
79 changes: 79 additions & 0 deletions src/functions/es5.js
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions src/functions/foreach.js
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 16 additions & 0 deletions src/functions/formats.js
Original file line number Diff line number Diff line change
@@ -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));
Empty file removed src/functions/operation.js
Empty file.
31 changes: 31 additions & 0 deletions src/functions/operations.js
Original file line number Diff line number Diff line change
@@ -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));
24 changes: 24 additions & 0 deletions src/functions/sort.js
Original file line number Diff line number Diff line change
@@ -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.name<user2.name ? -1 : 1;
};
console.log('users sorted', users.sort(sortUsers));

console.log("users's names", users.map(user =>user.name));
27 changes: 27 additions & 0 deletions src/prototype/bind.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 = -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);
18 changes: 18 additions & 0 deletions src/prototype/priority.js
Original file line number Diff line number Diff line change
@@ -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);

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

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 : {}
32 changes: 19 additions & 13 deletions src/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,40 @@
</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">
alert("Hello JS");
</script>

<script type="text/javascript">
var array = [2, 4, 6, 8, 10, 11, 0, -3];
var array = [2, 4, 6, 10, 11, 0, -3];
var group = _.groupBy(array, function (num) {
return num % 2
});
console.log(group);
console.log(_.max(array));
</script>

</script>

<script type="text/javascript">
var x=Number(prompt("Entrez un nombre positif :"));
var result=1;
function factoriel(x){
for(var i=1;i<=x;i++){
result=result*i;}
return result;
};
console.log('le factoriel de '+x+' est '+factoriel(x));

function factoriel(x) {
var result = 1;
for (var i = 1; i < x + 1; i++) {
result = result * i;
}
return result;
}

var result = factoriel(5);
console.log(result);

console.log('with function :', factoriel(250));
</script>


</html>
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