Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • e40792u/typescript_bailly_clement
1 result
Show changes
Commits on Source (9)
Exercice :
1) On place le point d'arret à la ligne "j -= 10;" afin d'obtenir les valeurs successives de j.
2) On place le point d'arret sur l'affection de tab.
3) x=10, y=20 ; x=10, y=10 ; x=20, y=10; Le dernier console.log affiche x=10, y=20.
On utilise echanger comme nom pour la fonction. La fonction ne fonctionne pas car les valeurs échangées sont uniquement à l'intérieur de la fonction.
4) xy passe de [10,20] à [20,10]. La fonction marche car on change directement les éléments du programme principal.
Le dernier console.log affiche [20,10] car on ne modifie plus le tableau du programme principal mais un nouveau.
5) j = Uncaught ReferenceError : Cannot access 'j' from debugger
j = Uncaught ReferenceError : Cannot access 'j' from debugger
j = 0, j = 2, j = 4, j = 6, j = 8, j = 10, j = 12, j = 14, j = 16
j ne marche que dans la fonction.
6) Les indices 1 des deux tableaux ont changé.
Seul le premier tableau à changer.
7) Le programme renvoie le produit de x et y.
On regarde la valeur la plus grande et la valeur la plus petite et on met la valeur la plus petite dans la boucle for.
Le programme ne fonctionne pas avec les chiffres négatifs.
8) return 5 * factorielle(4), return 4 * factorielle(3), return 3 * factorielle(2), return 2 * factorielle(1), return 1
factorielle(5) = 120
class MathError extends Error {
constructor(message: string) {
super(message);
this.name = "MathError";
}
}
class OpérateurError extends Error {
constructor(message: string) {
super(message);
this.name = "OpérateurError";
}
}
function calcule(tableau: Array<number>,indice: number,operateur: string,operande: number): number {
if (indice < 0 || indice >= tableau.length) {
throw new RangeError("Indice invalide");
}
switch (operateur) {
case "+":
return tableau[indice] + operande;
case "-":
return tableau[indice] - operande;
case "*":
return tableau[indice] * operande;
case "/":
if (operande === 0) {
throw new MathError("Division par zéro");
}
return tableau[indice] / operande;
default:
throw new OpérateurError("Opérateur inconnu");
}
}
try {
console.log(calcule([1,2,3,4],1,"/ ",4));
console.log(calcule([1,2,3,4],2,'+',3));
}
catch (e) {
console.log(e);
}
try {
console.log(calcule([1, 2, 3], 8, "-", 5));
}
catch (e) {
console.log(e);
}
try {
console.log(calcule([1, 2, 3], 2, "addition", 10));
}
catch (e) {
console.log(e);
}
console.log(calcule([1,2,3,4],2,'+',3))
\ No newline at end of file
function deb1():number{
let j = 200;
let tab : number[] = new Array(10);
for (let i = 0; i < 10; i++){
j -=10;
tab[i] = j;
}
return j;
}
function echanger(x:number, y:number):void{
const tmp = y;
y = x ;
x = tmp;
console.log(x,y)
}
function deb3(xy : number[]):void{
let tmp =xy[0];
xy[0] = xy[1];
xy[1] = tmp;
xy=[100,200];
console.log(xy);
}
function deb4():number{
let j = 25;
for (let i = 0; i < 10; i++){
let j =2*i;
console.log(j)
}
return j;
}
let tab1= [1,2,3];
let tab2 = [...tab1];
tab1[1] = 5;
produit(3,5);
function produit(x:number,y:number):number{
let val = 0;
let valMin = Math.abs(x);
let valMax = Math.abs(y);
let negatif : boolean;
if (valMin > valMax){
let tmp = valMin;
valMin = valMax;
valMax = tmp;
}
negatif = (x < 0) !== (y < 0);
for (let i = 0; i < valMin; i++){
val += valMax;
}
if (negatif){
return -val;
}
return val;
}
// console.log(produit(0,0));
// console.log(produit(5,3));
// console.log(produit(3,5));
// console.log(produit(-5,-2));
// console.log(produit(-2,-5));
// console.log(produit(-74,2));
// console.log(produit(-1,75));
// console.log(produit(10,-25));
// console.log(produit(10,3));
let n=5;
console.log(factorielle(n));
function factorielle(n : number):number{
if (n < 0){
throw new Error("n ne peut pas être négatif.")
}
if (n===0){
return 1;
}
let valeur = n* factorielle(n-1);
return valeur
}
class INEError extends Error{
constructor(message: string) {
super(message);
this.name = "INEError";
}
}
function isLetter(c: string): boolean {
return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z");
}
function isDigit(c: string): boolean {
return c >= "0" && c <= "9";
}
class Etudiant{
private ine : string;
private nom : string;
private prenom : string;
constructor(nom : string, prenom : string){
this.ine = "";
this.nom = nom;
this.prenom = prenom;
}
getINE():string{
return this.ine;
}
getPrenom():string{
return this.prenom;
}
getNom():string{
return this.nom;
}
setINE(s : string) : void{
if (s.length !== 11){
throw new INEError("Format incorrect.");
}
for (let i = 0;i<9; i++){
if (isLetter(s[i])){
throw new INEError("Format incorrect");
}
}
if (!(isLetter(s[9]))){
if(!(isLetter(s[10]))){
throw new INEError("Format incorrect.");
}
this.ine = s;
}
else if (isLetter(s[9])){
if(!(isLetter(s[10]))){
throw new INEError("Format incorrect.");
}
this.ine = s;
}
else{
throw new INEError("Format incorrect.");
}
}
}
let e2 : Etudiant;
let probleme : boolean;
do{
probleme = false;
try{
let ine = String(prompt("Donner l'INE de l'étudiant : "));
e2 = new Etudiant('Darwin', 'Charles')
e2.setINE(ine)
}
catch (e){
console.log(e);
probleme = true
}
}
while(probleme);
class DateInvalideError extends Error {
constructor(message = "La date n'est pas au bon format") {
super(message);
this.name = "DateInvalideError";
}
}
class DateUSError extends Error {
constructor(message = "La date est au format US") {
super(message);
this.name = "DateUSError";
}
}
function extraitDate(date: string): Array<number> {
let us = false;
let tab = date.split("/");
if (tab.length !== 3) {
tab = date.split("-");
if (tab.length === 3) {
us = true;
} else {
throw new DateInvalideError("La date doit être au format jj/mm/aaaa");
}
}
const iTab = new Array<number>();
for (const s of tab) {
for (const car of s) {
if (!isDigit(car)) {
throw new DateInvalideError("La date doit être au format jj/mm/aaaa");
}
}
iTab.push(Number.parseInt(s));
}
let iJour = 0;
let iMois = 1;
if (us) {
iJour = 1;
iMois = 0;
}
if (iTab[iJour] < 1 || iTab[iJour] > 31) {
throw new DateInvalideError("les jours sont entre 1 et 31");
}
if (iTab[iMois] < 1 || iTab[iMois] > 12) {
throw new DateInvalideError("les mois sont entre 1 et 12");
}
if (us) {
throw new DateUSError();
}
return iTab;
}
function testeDate(date: string): void {
console.log("Date testée : " + date);
try {
console.log(extraitDate(date));
} catch (e) {
console.log(e);
}
}
class NomEtPrenomVideError extends Error {
constructor(message: string) {
super(message);
this.name = "NomEtPrenomVideError";
}
}
class NomVideError extends Error {
constructor(message: string) {
super(message);
this.name = "NomVideError";
}
}
class PrenomVideError extends Error {
constructor(message: string) {
super(message);
this.name = "PrenomVideError";
}
}
function creeInitiales(nom: string, prenom: string): string {
if ((nom === "") && (prenom === "")){
throw new NomEtPrenomVideError("Le nom et le prénom ne doivent pas etre vide.");
}
else if (nom === ""){
throw new NomVideError("Le nom ne doit pas etre vide.");
}
else if (prenom === ""){
throw new PrenomVideError("Le prenom ne doit pas etre vide.");
}
else{
return nom[0].toUpperCase() + prenom[0].toUpperCase();
}
}
try{
creeInitiales("","clement");
}
catch (e){
console.log(e)
}
finally{
console.log("Fin du programme")
}
// Question 2 : TypeError : Cannot read properties of undefined (reading 'toUpperCase')
let nom = 'bailly'
let prenom = 'clément'
console.log(creeInitiales(nom,prenom))
\ No newline at end of file
console.log("1");
console.log("2");
\ No newline at end of file