Important la méthode exec
de la classe RegExp
renvoie un tableau avec :
indice | contenu |
---|---|
0 | l'expression globale reconnue |
1,2,3, ... | le contenu de chaque couple de parenthèses capturantes dans l'expression régulière |
On va partir du source suivant, qui affiche dans un tableau 9 ampoules. Vous ne pouvez que compléter la fonction init
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Memory</title> <script type="text/javascript"> function init(){ } </script> </head> <body onload="init()"> <h1>Memory</h1> <table cellspacing="0"> <tr> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> </tr> <tr> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> </tr> <tr> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> <td> <img width="50%" src="off.png" alt="" /> </td> </tr> </table> </body> </html>
Le jeu tirera aléatoirement des ampoules allumées, les dévoilera pendant une seconde au joueur, puis les cachera à nouveau. Le joueur doit alors cliquer sur chaque ampoule initialement allumée. S'il se trompe, une alerte lui signifie qu'il a perdu. Lorsque toutes les ampoules ont été retrouvées, le joueur gagne.
Math.random