////////// 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.'; [ 'To be', 'question', 'nonexistent', 'TO BE', '', ].forEach((aSubStr) => { console.log(`'${aStr}'.includes('${aSubStr}') returns ${aStr.includes(aSubStr)}`); }); console.log(`'${aStr}'.includes('To be', 1) returns ${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 ////////// // Converts the non-negative integer `num` as a string in base `base` // (version until base 9, imperative) function convert2Base (num, base) { let res = ''; while (num > 0) { res = `${num % base}` + res; num = Math.floor(num / base); // Works on strings } return res; } // Works with string values console.log(`convert2Base ("666", 9) = ${convert2Base ("666", 9)}`); // -> '820' // And with numerical values console.log(`convert2Base(0, 2) = ${convert2Base(0, 2)}`); // -> '' console.log(`convert2Base(666, 2) = ${convert2Base(666, 2)}`); // -> '1010011010' console.log(`convert2Base(666, 3) = ${convert2Base(666, 3)}`); // -> '220200' // Converts the integer `num` as a string in base `base` // (version until base 35, imperative) function convert2BaseFull (num, base) { let res = ''; while (num > 0) { const initCode = "A".charCodeAt(0); // char code for "A" const digit = num % base; const sdigit = (digit < 10) ? digit.toString() : String.fromCharCode(digit - 10 + initCode); res = sdigit + res; num = Math.floor(num / base); } return res; } console.log(`convert2BaseFull(0, 2) = ${convert2BaseFull(0, 2)}`); // -> '0' console.log(`convert2BaseFull(666, 2) = ${convert2BaseFull(666, 2)}`); // -> '1010011010' console.log(`convert2BaseFull(666, 16) = ${convert2BaseFull(666, 16)}`); // -> '29A' console.log(`convert2BaseFull(666, 35) = ${convert2BaseFull(666, 35)}`); // -> 'J1' // Converts the string `nums` in base `base` into a base-10 integer // (i.e not a string as for the previous functions) function convert2Int (nums, base) { let res = 0; for (let i = 0; i < nums.length; i++) { res *= base; res = res + parseInt(nums[i]); } return res; } console.log(`convert2Int('1010011010', 2) = ${convert2Int('1010011010', 2)}`); // -> 666 console.log(`convert2Int('220200', 3) = ${convert2Int('220200', 3)}`); // -> 666 // Check that the functions are inverse from each other [ [ 1234, 9 ], [ 123456, 3 ], ].forEach((params) => { console.log(`convert2Int(convert2Base(${params[0]}, ${params[1]}), ${params[1]}) = ` + `${convert2Int(convert2Base(params[0], params[1]), params[1])}`); });