////////// Exercice 1 ////////// ////////// Exercice 2 ////////// ////////// Exercice 3 ////////// // computes the square of its argument `x` function square (x) { return x * x; } console.log(`square(5) = ${square(5)}`); // computes the discriminant of the polynomial ax^2+bx+c function discriminant (a, b, c) { return (b * b) - (4 * a * c); } console.log(`discriminant(2, 2, 2) = ${discriminant(2, 2, 2)}`); // evaluates the polynomial ax^2+bx+c function evalQuadratic (a, b, c, x) { return a * x * x + b * x + c; } console.log(`evalQuadratic(1, 1, 1, 1) = ${evalQuadratic(1, 1, 1, 1)}`); // computes a string characterizing the roots of the // polynomial ax^2+bx+c. This string can either be // 'Two real roots', 'Two complex roots' or 'One real root'. function caracQuadratic (a, b, c) { const d = discriminant(a, b, c); if (d > 0) return 'Two real roots'; else if (d < 0) return 'Two complex roots'; else return 'One real root'; } console.log(`caracQuadratic(1, 1, 1) = ${caracQuadratic(1, 1, 1)}`); console.log(`caracQuadratic(1, 2, 1) = ${caracQuadratic(1, 2, 1)}`); console.log(`caracQuadratic(1, 3, 1) = ${caracQuadratic(1, 3, 1)}`); // computes the roots of the polynomial ax^2+bx+c function roots (a, b, c) { const d = discriminant(a, b, c); if (d >= 0) { return [ (-b - Math.sqrt(d)) / (2 * a), (-b + Math.sqrt(d)) / (2 * a), ]; } else { throw new Error('racine complexe'); } } console.log(`roots(1, 2, 1) = ${roots(1, 2, 1)}`); // The code requires a "try-catch" block in case of error, otherwise // it will fail at the error point (duh) ////////// Exercice 4 ////////// // examples taken from the documentation const aStr = 'To be, or not to be, that is the question.'; console.log(`aStr = ${aStr}`); [ 'To be', 'question', 'nonexistent', 'TO BE', '', ].forEach((aSubStr) => { console.log(`aStr.includes(${aSubStr}) -> ${aStr.includes(aSubStr)}`); }); console.log(`aStr.includes('To be', 1) -> ${aStr.includes('To be', 1)}`); ////////// Exercice 5 ////////// // Displays the list of powers of two from `min` to `max` function powers2 (min, max) { for (let i = min; i < max; i++) { console.log(`2 ** ${i} = ${2 ** i}`); } } powers2(40, 60); // 40 and 60 are arbitrary values const twobig = 2n; // The two following values are different : console.log(`2 ** 55 = ${2 ** 55}`); // -> 36028797018963970 console.log(`2n ** 55 = ${twobig ** 55n}`); // -> 36028797018963968 // console.log(twobig + 5); // type error console.log(`2n + 5 = ${twobig + '5'} and it's weird`); // no error, implicit conversion ////////// Exercice 6 ////////// ../Javascript/number conversion imperative.correc