Skip to content

Commit 9dca86e

Browse files
committed
fix correction
1 parent b61e765 commit 9dca86e

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎TP/JS1/content/JS1.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function range(stop, start, step) {
4646
return res;
4747
}
4848

49-
/* Exercice 3 : Calculatrice polonaise inverse */
49+
/* Exercice 4 : Calculatrice polonaise inverse */
5050
let evaluate = function (expr) {
5151
console.log("[js] evaluate (" + expr + ")");
5252
let results = [];

‎TP/JS1/demo.js‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Exercice : objet avec une méthode : l'objet magique `this`
22

33
o = {
4-
x: "Romu",
5-
y: 41,
4+
x: "Guillaume",
5+
y: 40,
66
do: function () {
77
console.log(this.x, this.y);
88
},
@@ -41,6 +41,24 @@ iterate_array_while([1, 1, 2, 3, 5, 8]);
4141
console.log("Array.forEach");
4242
[1, 1, 2, 3, 5, 8].forEach(x => console.log(x))
4343

44+
// Exercice 1 : 99 Bottles of Beer
45+
// ----------------------------
46+
47+
function bottles(beers) {
48+
console.log("[js] bottles (" + beers + ")");
49+
let res = "";
50+
51+
const bottle = n => `${n || "No more"} bottle${n === 1 ? "" : "s"} of beer`;
52+
53+
for (let n = beers; n > 0; n--) {
54+
res += `${bottle(n)} on the wall, ${bottle(n).toLowerCase()}.
55+
Take one down and pass it around, ${bottle(n - 1).toLowerCase()} on the wall.
56+
57+
`;
58+
}
59+
60+
return res.trim();
61+
}
4462

4563
// Exercice 2 : fonction `range`
4664
// ----------------------------
@@ -78,3 +96,30 @@ function fibonacci_fa(n) {
7896

7997
console.log(fibonacci_fa(8));
8098

99+
// Exercice 4 : calculatrice polonaise inverse
100+
// ----------------------------
101+
102+
let evaluate = function (expr) {
103+
console.log("[js] evaluate (" + expr + ")");
104+
let results = [];
105+
106+
const ops = {
107+
"+": (a, b) => a + b,
108+
"-": (a, b) => a - b,
109+
"*": (a, b) => a * b,
110+
"/": (a, b) => a / b,
111+
};
112+
113+
expr.split(" ").forEach(item => {
114+
let nb = parseFloat(item);
115+
116+
if (!isNaN(nb)) results.push(nb);
117+
else {
118+
let b = results.pop();
119+
let a = results.pop();
120+
results.push(ops[item](a, b));
121+
}
122+
});
123+
124+
return results.pop() || 0;
125+
};

0 commit comments

Comments
 (0)