Answer the question
In order to leave comments, you need to log in
Combine two identical scripts into one, how?
Need to make the second number work
https://jsfiddle.net/enp6a2zd/
Ps. thanks in advance
Answer the question
In order to leave comments, you need to log in
In the current version
// Функция получения случайного числа
const getRandomNumber = (() => {
return Math.floor(Math.random() * 1000);
});
// Функция получения случайного имени
const getRandomName = (() => {
const arr = ['Alex', 'Bob', 'Mark', 'Peter'];
let randomNumber = Math.floor(Math.random() * arr.length);
return arr[randomNumber];
});
// Начальный объект
const obj = {
name: getRandomName(),
id: getRandomNumber()
};
// 1.2 Добавление нового поля в объект
obj.number = getRandomNumber();
const randomArray = () => Array(6).fill().map(() => obj);
console.log(randomArray());
0..999
To prevent repeated values, you can create an array with possible values, and take it out of it - this way the absence of repetitions is guaranteed.
The object is now created only once, and the same one is nested in the array. Maybe it's easier to do it in a loop? Array.splice(i, 1)
Array.push()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question