Advertisement
SkyWeapons

TP02

Jan 22nd, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. /********* TABLEAUX **********/
  4.  
  5. let tableau1 = ["Gilles","Nina","Pierre","Léa","Jacques","Anne","Fabrice","Margaux","Maxime"];
  6. let tableau2 = [4,8,1,2,9,10,7,12,24,8,33];
  7. let tableau3 = [32,6,90,3,12,56,22,8];
  8.  
  9.  
  10.  
  11.  
  12.  
  13. /* objets et annuaire */
  14.  
  15. let annuaire = {
  16.     "Dumat": {
  17.         "nom": "Dumat",
  18.         "prenom": "Marlène",
  19.         "email": "marlene@sarasonic.com",
  20.         "telephone": "06.31.17.18.12"
  21.     },
  22.     "Lepres": {
  23.         "nom": "Lepres",
  24.         "prenom": "Marc",
  25.         "telephone": "06.96.93.50.31"
  26.     },
  27.     "Pico": {
  28.         "nom": "Pico",
  29.         "prenom": "Laurent",
  30.         "email": "laurent.pico@netur.com"
  31.     },
  32.     "Milon": {
  33.         "nom": "Milon",
  34.         "prenom": "Louise"
  35.     }
  36. };
  37.  
  38. // Exercice 1: Tableaux
  39.  
  40. console.log("// Tableaux");
  41.  
  42. function lastElement(tab) {
  43.     return(tab[tab.length-1]);
  44. }
  45.  
  46. console.log(lastElement(tableau1));
  47.  
  48. function evenFromTable(tab) {
  49.     let tableau = [];
  50.     for(let i = 0; i < tab.length;i++) {
  51.         if(i%2==0) {
  52.             tableau.push(tab[i]);
  53.         }
  54.     }
  55.     return tableau;
  56. }
  57.  
  58. console.log(evenFromTable(tableau1));
  59.  
  60. function lastElementFromTable(tab, n) {
  61.     let tabLength = tab.length;
  62.     if(n > tabLength) {
  63.         console.log("La valeur de n entrée est incorrect");
  64.     } else {
  65.         return tab.slice(tabLength-n,tabLength);
  66.     }
  67. }
  68.  
  69. console.log(lastElementFromTable(tableau1,4));
  70.  
  71. function multiply(tab) {
  72.     let res = 1;
  73.     for(let i = 0; i < tab.length; i++) {
  74.         res *= tab[i];
  75.     }
  76.     return res;
  77. }
  78.  
  79. console.log(multiply(tableau2));
  80.  
  81. function divisibleBy(tab,n) {
  82.     let table = [];
  83.     for(let i = 0; i < tab.length; i++) {
  84.         if(tab[i]%n==0) {
  85.             table.push(tab[i])
  86.         }
  87.     }
  88.     return table;
  89. }
  90.  
  91. console.log(divisibleBy(tableau2,2));
  92. console.log(divisibleBy(tableau2,3));
  93.  
  94. function commonElement(tab1, tab2) { // Erreur si un des tableaux est plus grand que l'autre ==> Faire un test de longeuur
  95.     let commonElements = [];
  96.     for(let i = 0; i < tab1.length; i++) {
  97.         if (tab2.indexOf(tab1[i]) != -1 && commonElements.indexOf(tab1[i]) == -1) {
  98.             commonElements.push(tab1[i]);
  99.         }
  100.     }
  101.     return commonElements;
  102. }
  103.  
  104. console.log(commonElement(tableau2,tableau3));
  105.  
  106. function distinctElement(tab1, tab2) {
  107.     let tables = [tab1, tab2];
  108.     let distinctElements = [];
  109.     for(let i = 0; i < 2; i++) {
  110.         for(let j = 0; j < tab2.length; j++) {
  111.             if(distinctElements.indexOf(tables[i][j]) == -1) {
  112.                 distinctElements.push(tables[i][j]);
  113.             }
  114.         }
  115.     }
  116.     return distinctElements;
  117. }
  118.  
  119. console.log(distinctElement(tableau2, tableau3));
  120.  
  121. function intToAscii(tab) {
  122.     let string = '';
  123.     for(let i = 0; i < tab.length; i++) {
  124.         tab[i] = 97+tab[i]%26;
  125.     }
  126.     for(let j = 0; j < tab.length; j++) {
  127.         string += String.fromCodePoint(tab[j]);
  128.     }
  129.     return string;
  130. }
  131.  
  132. console.log(intToAscii(tableau2));
  133.  
  134. // Objets
  135.  
  136. console.log("// Objets");
  137.  
  138. let objet = {
  139.     title: "un titre pour cet objet",
  140.     liste: [5,8,19],
  141. }
  142.  
  143. console.log(objet);
  144.  
  145. let film = {
  146.     "movie": {
  147.         "title": "Men in black",
  148.         "year": 1997,
  149.     },
  150.     "cast": {
  151.         "firstActor": {
  152.             "name": "Tommy Lee Jones",
  153.             "role": "Kay",
  154.         },
  155.         "secondActor": {
  156.             "name": "Will Smith",
  157.             "role": "Jay",
  158.         },
  159.         "thirdActor": {
  160.             "name": "Linda Fiorentino",
  161.             "role": "Laurel",
  162.         },
  163.     },
  164. };
  165.  
  166. console.log(film);
  167.  
  168. // Objets, suite
  169.  
  170. console.log("// Objets, suite");
  171.  
  172. function createPoint(x,y) {
  173.     return {"x": x, "y": y};
  174. }
  175.  
  176. let A = createPoint(2,-6);
  177. let B = createPoint(-2,2);
  178. let C = createPoint(5,-3);
  179. let D = createPoint(7, -7);
  180.  
  181. console.log("A",A, "B", B, "C", C, "D", D);
  182.  
  183. function distance(A, B) {
  184.     return Math.sqrt((B["x"]-A["x"])**2+(B["y"]-A["y"])**2);
  185. }
  186.  
  187. console.log(distance(A,B))
  188.  
  189. function isColinear(A,B,C,D) {
  190.     let vect1 = [B["x"]-A["x"],B["y"]-A["y"]];
  191.     let vect2 = [D["x"]-C["x"],D["y"]-C["y"]];
  192.     return vect1[0]*vect2[1]==vect1[1]*vect2[0];
  193. }
  194.  
  195. console.log(isColinear(A,B,C,D));
  196. console.log(isColinear(A, C, D, B));
  197.  
  198. // Objets encore
  199.  
  200. console.log("// Objets encore");
  201.  
  202. console.log(Object.keys(annuaire));
  203.  
  204. console.log("L'annuaire contient t-il la clé Toto ?", annuaire.hasOwnProperty("Toto"));
  205. console.log("L'annuaire contient t-il la clé Pico ?", annuaire.hasOwnProperty("Pico"));
  206.  
  207. function showPersons(obj) {
  208.     for(const prop in obj) {
  209.         if(obj[prop].hasOwnProperty("telephone")) {
  210.             console.log(obj[prop]["prenom"], obj[prop]["nom"],"a pour téléphone", obj[prop]["telephone"]);
  211.         } else {
  212.             console.log(obj[prop]["prenom"], obj[prop]["nom"],"n'a pas de téléphone.");
  213.         }
  214.         if(obj[prop].hasOwnProperty("email")) {
  215.             console.log(obj[prop]["prenom"], obj[prop]["nom"],"a pour addresse mail", obj[prop]["email"]);
  216.         } else {
  217.             console.log(obj[prop]["prenom"], obj[prop]["nom"],"n'a pas d'addresse mail");
  218.         }
  219.     }
  220. }
  221.  
  222. showPersons(annuaire);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement