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

Tags:



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.

Answer

You could do like this:

function createRandomVowels(number) {
  const vowels = ['a', 'e', 'i', 'o', 'u', 'y' ];
  const result = [];
  for (let i = 0; i < number; i++) {
     result.push(vowels[Math.floor(Math.random() * vowels.length)]);
  }
  
  return result;
}

console.log(createRandomVowels(2));


Source: stackoverflow