Answer the question
In order to leave comments, you need to log in
What function is suitable for getting an array of strings of random length?
What function can be used to get an array of strings of random length from values that should not be repeated from the photos object?
const photos = {
colors: [
'white',
'red',
'blue',
'green',
'brown',
'black'
],
link: [
'firstLink',
'secondLink',
'thirdLink'
]
};
const getRandomArrayElement = (elements) => {
return elements[_.random(0, elements.length - 1)];
};
function getRandomElement () {
return {
colors: getRandomArrayElement(colors),
link: getRandomArrayElement(link)
}
};
const commonArr = new Array(5).fill(' ').map(() => getRandomElement());
console.log(commonArr)
Answer the question
In order to leave comments, you need to log in
You can go through all the elements and randomly decide for each whether to include it in the next selection:
const getSome = (arr) => arr.filter(() => Math.random() >= 0.5);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question