9.1 KiB
9.1 KiB
In [33]:
// partie de dodgeball
class DodgeballGame{
constructor(joueurs) {
this.joueursEnVie = joueurs;
this.joueursMorts = [];
}
kill(joueur){
this.joueursMorts.push(joueur);
const nouveauJoueursEnVie = []
for(const joueurEnVie of this.joueursEnVie){
if(this.joueursMorts.indexOf(joueurEnVie) === -1){
nouveauJoueursEnVie.push(joueurEnVie);
}
}
this.joueursEnVie = nouveauJoueursEnVie;
}
catchBall(){
if(this.joueursMorts.length < 1){
return;
}
const jesus = this.joueursMorts.shift();
this.joueursEnVie.push(jesus);
}
}
let players = ["a", "b", "c", "d", "e", "f"];
let game = new DodgeballGame(["a", "b", "c", "d", "e", "f"]);
game.kill("c");
game.kill("d")
game.catchBall();
console.log(game.joueursEnVie);
console.log(game.joueursMorts);
[ [32m"a"[39m, [32m"b"[39m, [32m"e"[39m, [32m"f"[39m, [32m"c"[39m ] [ [32m"d"[39m ]
In [2]:
class Politicien {
}
const politicien = new Politicien();In [12]:
class JackOfAllTrade{
add(a, b){
return a + b;
}
multiply(a, b){
return a * b;
}
add1(x){
return this.add(x, 1);
}
}In [13]:
let joat = new JackOfAllTrade()
joat.add(109, 90)Out [13]:
[33m199[39m
In [14]:
joat.add1(100)Out [14]:
[33m101[39m
In [19]:
class ChangeCase{
toSnake(str){
str = str.replace(" ", "_");
return str;
}
}
let case1 = new ChangeCase();
case1.toSnake("hello world foifoi");Out [19]:
[32m"hello_world foifoi"[39m
In [1]:
let s = "bonjour monde"In [2]:
let idx = s.indexOf(" ")In [3]:
s = s.replace(" ", "")Out [3]:
[32m"bonjourmonde"[39m
In [5]:
s.substring(idx, idx + 1).toUpperCase()Out [5]:
[32m"M"[39m
In [8]:
import {Point} from "https://cdn.jsdelivr.net/gh/didiercrunch/cours-420-301-ah@tp1.1/modules-examples/mod.js"In [10]:
let point = new Point(34, 78)In [11]:
point.getX()Out [11]:
[33m34[39m
In [12]:
point.getY()Out [12]:
[33m78[39m
In [13]:
class Point2D{
constructor(x, y){
this.x = x;
this.y = y;
}
distanceFromOrigin(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}In [15]:
let point2D = new Point2D(34, 78)
point2D.distanceFromOrigin()Out [15]:
[33m85.0881895447306[39m
In [17]:
new URL("https://example.com/didier/rocks")Out [17]:
URL {
href: [32m"https://example.com/didier/rocks"[39m,
origin: [32m"https://example.com"[39m,
protocol: [32m"https:"[39m,
username: [32m""[39m,
password: [32m""[39m,
host: [32m"example.com"[39m,
hostname: [32m"example.com"[39m,
port: [32m""[39m,
pathname: [32m"/didier/rocks"[39m,
hash: [32m""[39m,
search: [32m""[39m
}In [ ]: