Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- /********* TABLEAUX **********/
- let tableau1 = ["Gilles","Nina","Pierre","Léa","Jacques","Anne","Fabrice","Margaux","Maxime"];
- let tableau2 = [4,8,1,2,9,10,7,12,24,8,33];
- let tableau3 = [32,6,90,3,12,56,22,8];
- /* objets et annuaire */
- let annuaire = {
- "Dumat": {
- "nom": "Dumat",
- "prenom": "Marlène",
- "email": "marlene@sarasonic.com",
- "telephone": "06.31.17.18.12"
- },
- "Lepres": {
- "nom": "Lepres",
- "prenom": "Marc",
- "telephone": "06.96.93.50.31"
- },
- "Pico": {
- "nom": "Pico",
- "prenom": "Laurent",
- "email": "laurent.pico@netur.com"
- },
- "Milon": {
- "nom": "Milon",
- "prenom": "Louise"
- }
- };
- // Exercice 1: Tableaux
- console.log("// Tableaux");
- function lastElement(tab) {
- return(tab[tab.length-1]);
- }
- console.log(lastElement(tableau1));
- function evenFromTable(tab) {
- let tableau = [];
- for(let i = 0; i < tab.length;i++) {
- if(i%2==0) {
- tableau.push(tab[i]);
- }
- }
- return tableau;
- }
- console.log(evenFromTable(tableau1));
- function lastElementFromTable(tab, n) {
- let tabLength = tab.length;
- if(n > tabLength) {
- console.log("La valeur de n entrée est incorrect");
- } else {
- return tab.slice(tabLength-n,tabLength);
- }
- }
- console.log(lastElementFromTable(tableau1,4));
- function multiply(tab) {
- let res = 1;
- for(let i = 0; i < tab.length; i++) {
- res *= tab[i];
- }
- return res;
- }
- console.log(multiply(tableau2));
- function divisibleBy(tab,n) {
- let table = [];
- for(let i = 0; i < tab.length; i++) {
- if(tab[i]%n==0) {
- table.push(tab[i])
- }
- }
- return table;
- }
- console.log(divisibleBy(tableau2,2));
- console.log(divisibleBy(tableau2,3));
- function commonElement(tab1, tab2) { // Erreur si un des tableaux est plus grand que l'autre ==> Faire un test de longeuur
- let commonElements = [];
- for(let i = 0; i < tab1.length; i++) {
- if (tab2.indexOf(tab1[i]) != -1 && commonElements.indexOf(tab1[i]) == -1) {
- commonElements.push(tab1[i]);
- }
- }
- return commonElements;
- }
- console.log(commonElement(tableau2,tableau3));
- function distinctElement(tab1, tab2) {
- let tables = [tab1, tab2];
- let distinctElements = [];
- for(let i = 0; i < 2; i++) {
- for(let j = 0; j < tab2.length; j++) {
- if(distinctElements.indexOf(tables[i][j]) == -1) {
- distinctElements.push(tables[i][j]);
- }
- }
- }
- return distinctElements;
- }
- console.log(distinctElement(tableau2, tableau3));
- function intToAscii(tab) {
- let string = '';
- for(let i = 0; i < tab.length; i++) {
- tab[i] = 97+tab[i]%26;
- }
- for(let j = 0; j < tab.length; j++) {
- string += String.fromCodePoint(tab[j]);
- }
- return string;
- }
- console.log(intToAscii(tableau2));
- // Objets
- console.log("// Objets");
- let objet = {
- title: "un titre pour cet objet",
- liste: [5,8,19],
- }
- console.log(objet);
- let film = {
- "movie": {
- "title": "Men in black",
- "year": 1997,
- },
- "cast": {
- "firstActor": {
- "name": "Tommy Lee Jones",
- "role": "Kay",
- },
- "secondActor": {
- "name": "Will Smith",
- "role": "Jay",
- },
- "thirdActor": {
- "name": "Linda Fiorentino",
- "role": "Laurel",
- },
- },
- };
- console.log(film);
- // Objets, suite
- console.log("// Objets, suite");
- function createPoint(x,y) {
- return {"x": x, "y": y};
- }
- let A = createPoint(2,-6);
- let B = createPoint(-2,2);
- let C = createPoint(5,-3);
- let D = createPoint(7, -7);
- console.log("A",A, "B", B, "C", C, "D", D);
- function distance(A, B) {
- return Math.sqrt((B["x"]-A["x"])**2+(B["y"]-A["y"])**2);
- }
- console.log(distance(A,B))
- function isColinear(A,B,C,D) {
- let vect1 = [B["x"]-A["x"],B["y"]-A["y"]];
- let vect2 = [D["x"]-C["x"],D["y"]-C["y"]];
- return vect1[0]*vect2[1]==vect1[1]*vect2[0];
- }
- console.log(isColinear(A,B,C,D));
- console.log(isColinear(A, C, D, B));
- // Objets encore
- console.log("// Objets encore");
- console.log(Object.keys(annuaire));
- console.log("L'annuaire contient t-il la clé Toto ?", annuaire.hasOwnProperty("Toto"));
- console.log("L'annuaire contient t-il la clé Pico ?", annuaire.hasOwnProperty("Pico"));
- function showPersons(obj) {
- for(const prop in obj) {
- if(obj[prop].hasOwnProperty("telephone")) {
- console.log(obj[prop]["prenom"], obj[prop]["nom"],"a pour téléphone", obj[prop]["telephone"]);
- } else {
- console.log(obj[prop]["prenom"], obj[prop]["nom"],"n'a pas de téléphone.");
- }
- if(obj[prop].hasOwnProperty("email")) {
- console.log(obj[prop]["prenom"], obj[prop]["nom"],"a pour addresse mail", obj[prop]["email"]);
- } else {
- console.log(obj[prop]["prenom"], obj[prop]["nom"],"n'a pas d'addresse mail");
- }
- }
- }
- showPersons(annuaire);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement