I do not know how to implement it.

There is a code that should output in a row 3 random words without repetitions. It works, but how to make a word in front of you add a permanent description, for example:

1 gray: bear
2 white: hare
3 brown: fox

It is necessary that only the names of animals change, the list of colors is always the same. Here, for example, 5 words, but their number may be different. `

If not difficult, can you explain in more detail?

'use strict'; var numWords = 3, words = ["Π²ΠΎΠ»ΠΊ ","лиса ","заяц ","мСдвСдь ","Π΅ΠΆΠΈΠΊ "]; var rand = function(from, to) { var n = Math.floor((to - from + 1) * (Math.random() %100)); return (from + n); } var newWords = words; var loops = (words.length - numWords); for (var i = 0; i < loops; i++) { newWords.splice(rand(0, words.length - 1), 1); } console.log(newWords.join('')); 

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

1 answer 1

For example, you can:

  function randAccess(arr){ return arr.splice([Math.floor(Math.random()*arr.length)],1) } var color = ["Π±Π΅Π»Ρ‹ΠΉ","ΠΆΠ΅Π»Ρ‚Ρ‹ΠΉ","синий","красный","Ρ„ΠΈΠΎΠ»Π΅Ρ‚ΠΎΠ²Ρ‹ΠΉ"]; var animal = ["заяц","Π²ΠΎΠ»ΠΊ","ΠΊΠΈΡ‚","ΠΊΠΎΡ‚","собака"]; color.forEach(function(item, index){ console.log(index + " " + item + ":" + randAccess(animal)); }) 

You need to have two arrays, one with flowers and the second with animals. Further in the cycle to each color choose an animal, while so that the animals do not repeat. To do this, the element can be removed from the array by selecting splice .