array
without repeating them. More specifically, as part of JS training, I used it to generate a classic RPG group of characters (barbarian, magician, thief, knight, priest), without repeating classes and names. function GamePlayer(n, r, l, p) { this.nick = n; this.role = r; this.level = l; this.portrait = p; }
var player1 = new GamePlayer("Power Ranger","barbarian","64","img/barbarian.jpg")
player1
variable stores the Barbarian Power Ranger 64 level with a certain portrait; we can display any of its parameters in the body of the page using player1.nick
, player1.level
, etc.(n, r, l, p)
of GamePlayer
are responsible for receiving and ordering data in the function. If we change the n
and r
places in the example, then the powerful Barbarian ranger player1
remain in player1
, which does not quite match the task.
,
,
and
. var playerNames = ['Rabbit Helpless', 'Warm Dreaded Foal', 'Desire Kit', 'Angel Dusty', 'Sweety Frozen', 'Silver Heavy Wombat', 'Lost Puma', 'Vital Panda', 'Rolling Sun', 'Steel Runny', 'Young Fox', 'Needless Ruthless Volunteer', 'Chipmunk Cult', 'Indigo Puppy'];
var playerRoles = ['barbarian', 'mage', 'rogue', 'knight', 'priest'];
string
, from which we will then choose the values to display on the page. var playerLevels = []; for (i = 0;i <= 80;i++) { console.log(i); playerLevels[i] = i; }
playerLevels
, each cell of which contains an int
with its own number. var playerPortraits = ['img/barbarian.jpg', 'img/mage.jpg', 'img/rogue.jpg', img/knight.jpg', 'img/priest.jpg'];
background-image
parameter of the desired div
(or in the src
parameter of the desired image, for whom it is more convenient).playerPortraits
array be identical to the order of the classes in the playerRoles
array, then to generate them (so that the picture matches the class) we can use the same random
variable. for (i = 0;i<=4;i++) { }
var players = [ ];
var namerand = Math.floor(Math.random() * playerNames.length)
Marh.random()
used to generate a random number;playerNames.length
(length of the playerNames
array) - to limit the random number of names in the array;Math.floor
- to turn the resulting number into an integer.Math.floor
rounds down, but since the numbering in the arrays comes from 0, it suits us. var rolerand = Math.floor(Math.random() * (playerRoles.length));
playerRoles
array. var levelrand = Math.floor(Math.random() * (70 - 60 + 1) + 60);
Math.random() * (max - min) + min
.Math.floor
(read above). players[i] = new GamePlayer(playerNames[namerand], playerRoles[rolerand], playerLevels[levelrand], playerPortraits[rolerand]);
players
array with its own sequence number. Its parameters are:playerNames[namerand]
- name, random selection from names (cell number namerand
in playerNames
);playerRoles[rolerand]
- class, random selection from classes;playerLevels[levelrand]
- class, random level selection in the range of 60-70;playerPortraits[rolerand]
- portrait, a random selection of portraits.random
in both cases. players[i] = new GamePlayer(playerNames[namerand], playerRoles[rolerand], playerLevels[levelrand], playerPortraits[rolerand]); playerNames.splice(namerand,1); playerRoles.splice(rolerand,1); playerPortraits.splice(rolerand,1);
playerNames.splice(namerand,1)
removes the cell with the namerand
number from the playerNames
array using the splice
operation. The unit after the comma shows how many cells need to be deleted starting with the specified one; we need to delete only one, the specified cell itself.undefined
if I stumbled upon the last cell of the array (after all, we reduced it by 1). But, since our Math.random
uses playerNames.length
and others, it directly depends on the length of the array, and will only give out new, not duplicate values.Source: https://habr.com/ru/post/181874/
All Articles