issue98:tutoriel_javascript
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue98:tutoriel_javascript [2015/06/30 11:02] – créée auntiee | issue98:tutoriel_javascript [2015/07/20 14:07] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | In Part 2, we covered JavaScript Objects and Functions, and introduced the Questions and Solutions section; in this part we are going further and we will do some basic programming on the server side using node (don't be scared, it will be easy). | + | **In Part 2, we covered JavaScript Objects and Functions, and introduced the Questions and Solutions section; in this part we are going further and we will do some basic programming on the server side using node (don't be scared, it will be easy). |
Node (http:// | Node (http:// | ||
Ligne 11: | Ligne 11: | ||
nodejs -v | nodejs -v | ||
- | The result of this command should display something like v0.12.4 (this is the latest version of node at the publication date). | + | The result of this command should display something like v0.12.4 (this is the latest version of node at the publication date).** |
- | We will write a lotto number generator application which is capable of generating random numbers between one and 90, it will generate five numbers which we could play on lotto later :-) | + | Dans la partie 2 de la série, nous avons traité des Objets et Fonctions Javascript, et présenté la section Questions et Solutions ; dans cet article, nous allons poursuivre en faisant un peu de programmation de base, côté serveur, en utilisant node (pas de panique, ce sera facile). |
+ | |||
+ | Node (http:// | ||
+ | |||
+ | L' | ||
+ | |||
+ | sudo apt-get install nodejs | ||
+ | |||
+ | Après installation, | ||
+ | |||
+ | nodejs -v | ||
+ | |||
+ | Le résultat de cette commande devrait afficher quelque chose comme v0.12.4 (c'est la dernière version à la date de cette publication). | ||
+ | |||
+ | **We will write a lotto number generator application which is capable of generating random numbers between one and 90, it will generate five numbers which we could play on lotto later :-) | ||
Shown above we have the source code. | Shown above we have the source code. | ||
Ligne 23: | Ligne 37: | ||
We have a for cycle which has five iterations, because we want to generate five lotto numbers. We create two temporary variables, isCorrectNumber is a flag which will signal if the number we generated was generated before or not, and we will use this to generate numbers till these are not correct. The other variable is selectedNumber, | We have a for cycle which has five iterations, because we want to generate five lotto numbers. We create two temporary variables, isCorrectNumber is a flag which will signal if the number we generated was generated before or not, and we will use this to generate numbers till these are not correct. The other variable is selectedNumber, | ||
- | The calculation of selectedNumberIndex seems a little odd, but lets analyze it. We say the selectedNumberIndex should be equal to a number which is generated Math.random() method, is multiplied by 1000, we take the mathematical floor of the value and calculate the remainder when divided by 90 (ie, 0-89). | + | The calculation of selectedNumberIndex seems a little odd, but lets analyze it. We say the selectedNumberIndex should be equal to a number which is generated Math.random() method, is multiplied by 1000, we take the mathematical floor of the value and calculate the remainder when divided by 90 (ie, 0-89).** |
- | Lets take a concrete example: | + | Nous allons écrire une application de génération de nombre du loto, qui soit capable de générer des nombres aléatoires entre 1 et 90 ; il générera cinq nombres permettant ensuite de jouer au loto :-). |
+ | |||
+ | Le code source est présenté ci-dessus. | ||
+ | |||
+ | À la première ligne, nous avons écrit un message pour la console, indiquant aux utilisateurs ce que fera le programme. Ensuite, nous définissons une matrice appelée predefinedNumbers. Dans Javascript, les matrices peuvent être créées en utilisant l' | ||
+ | |||
+ | La ligne de code suivante est une boucle for qui remplit la matrice predefinedNumbers avec les nombres de 1 à 90. La boucle for du Javascript est très proche des boucles for qu'on trouve dans les langages basés sur la syntaxe C. Après ce remplissage initial, nous envoyons un message (Initialized the set of numbers - Le jeu de nombres est initialisé). Puis, nous créons une nouvelle matrice vide (lottoNumbers) qui contiendra les résultats. Après cela, vient la partie la plus compliquée du programme, la génération des nombres aléatoires (ici à droite). | ||
+ | |||
+ | Nous avons une boucle for à cinq itérations, | ||
+ | |||
+ | Le calcul de selectedNumberIndex peut paraître un peu bizarre, mais analysons-le. Nous disons que selectedNumberIndex doit être égal à un nombre qui est généré avec la méthode Math.ramdom(), | ||
+ | |||
+ | **Lets take a concrete example: | ||
Math.random() returns 0.7323351332452148, | Math.random() returns 0.7323351332452148, | ||
Ligne 38: | Ligne 64: | ||
node lottoGenerator.js | node lottoGenerator.js | ||
- | I would be happy to hear from you! Please let me know what topics you are interested in. | + | I would be happy to hear from you! Please let me know what topics you are interested in.** |
+ | |||
+ | Prenons un exemple concret : | ||
+ | Math.random() retourne la valeur 0, | ||
+ | |||
+ | Nous utilisons le nombre calculé comme un index pour choisir un nombre dans la matrice predefinedNumbers et nous analysons le nombre sélectionné ; s'il a déjà été sélectionné, | ||
+ | |||
+ | Nous pouvons lancer le script en ligne de commande en tapant : | ||
+ | |||
+ | nodejs lottoGenerator.js | ||
+ | |||
+ | ou : | ||
+ | |||
+ | node lottoGenerator.js | ||
+ | |||
+ | Je serais heureux d' |
issue98/tutoriel_javascript.1435654949.txt.gz · Dernière modification : 2015/06/30 11:02 de auntiee