Skip to content
Advertisement

creating a function such as “createRandomVowels” that returns an array containing random vowels [closed]

let sampleData = createRandomVowels(4)

The sampleData should store 4 random vowels. E.g. [‘a’, ‘a’, ‘o’, ‘i’]

More examples Input(s): n (Number) Output: An array of ‘n’ random vowels

How would I go about creating a function such as “createRandomVowels” that returns an array containing n random vowels? I need to get a user input (1-4) and based on there selection, print out “number” of the vowels in the array. It sounds simple, but clearly I am not able to do it.

Sorry that I do not have too much code to show.

Advertisement

Answer

You could do like this:

function createRandomVowels(length) {
  const vowels = ['a', 'e', 'i', 'o', 'u', 'y' ];
  return Array.from({ length }, () => vowels[Math.floor(Math.random() * vowels.length)]);
}

console.log(createRandomVowels(2));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement