What is the best way to label user-inputted arrays?

Majesty

I am creating a word generator like Awkwords that randomly picks a letter from a labelled array and puts it in a sequence.

I am currently just having the label be the first item of its array. Here's a simple version:

let sequence = 'CVC'
let word = ''
let consonants = ['C', 'b', 'c', 'd',]
let vowels = ['V', 'a', 'e', 'i',]
let lists = [consonants, vowels]

for (letter of sequence) {
  for (list of lists) {
    if (list[0] === letter) {
      pick random letter out of the list other than its first value and add it to the word
    }
  }
}

I'm almost brand new to programming in general so I have no idea if this is the way I should be doing this. Is there a more efficient way to label an array?

There will be a max of 26 lists (one for each letter) and the items within them will change depending on what the user inputs.

Ross Murray

One way to get something like a "labeled array" is to use a javascript object. Here you could use the letters as keys (or properties) and the associated list as the value. This would also save you looping through the lists to search for the desired letter.

Try something like below:

let sequence = "CVC";
let word = "";
let patterns = {
  C: ["b", "c", "d"],
  V: ["a", "e", "i"]
};

for (letter of sequence) {
  // do something here with patterns[letter]
  console.log(patterns[letter])
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

what is the best way to define label and TextBox

The best way of recommending restaurants by location using user inputted locations - using python

What is the best way to merge 2 byte arrays?

What is the best way to merge nested arrays

What is the best way to implement user account activation?

What is the best way to authorize the user in Angular 5

What is the best way to store a user feature?

What is the best way to add a user to the sudoer group?

What is the best way to get a user's city?

What is the best way to authenticate user with node and angularjs?

What's the best way to document an array of arrays of arrays using JSDoc?

What is the best way of using Arrays.asList() to initialize a List

What is the best way to "merge" 2 arrays by each value?

What is a best way to intersect multiple arrays with numpy array?

What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

What is the best way to iteratively instantiate a class from a dictionary of lists/arrays?

What is the best way to hold multiple 3D arrays in SQL?

In Swift, what is the best way to assign two arrays to key/value of a dictionary?

What is the best way to store multiple entries / arrays on MYSQL table

What is the best way to combine two arrays using javascript?

Is there a way to repeat every third word written by a user inputted text

Is there a way to find out if the user has inputted the same value?

is there any way to let the user choose which variable is to be inputted?

What is the best way to ensure a user only enters natural numbers?

What is the best way to read user inputs via scanner?

What is the best way to communicate a kernel module with a user space program?

What is the best way to pass in authenticated user information to a sql query?

What's the best way to test if a user can sudo in Bash?

What is the best way to make function for getting current user coordinates in RN