Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JS] Losowy element tablicy
Dominator
post
Post #1





Grupa: Zarejestrowani
Postów: 565
Pomógł: 15
Dołączył: 11.10.2010

Ostrzeżenie: (20%)
X----


Czemu nie działa?

  1. var messages = new Array();
  2. var messages[1]="Ada";
  3. var messages[2]="Adela";
  4. var messages[3]="Adelajda";
  5. var messages[4]="Adriana";
  6. var messages[5]="Adrianna";
  7. var messages[6]="Agata";
  8. var messages[7]="Agnieszka";
  9. var messages[8]="Agrypina";
  10.  
  11. function getMessage() {
  12. return messages[Math.floor(Math.random() * messages.length)];
  13. }
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
pyro
post
Post #2





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

Ostrzeżenie: (0%)
-----


  1. function getRandomInt(min, max) {
  2. return Math.floor(Math.random() * (max - min + 1)) + min;
  3. }
  4.  
  5. var randomIndex = getRandomInt(0, (messages.length-1));
  6.  
  7. console.log(messages[randomIndex]);
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 13.10.2025 - 09:16