Home

>

Prompts Database

>

Coding
Write a JavaScript Function
avatar
Buffy Summers
Share
Leave a comment
614 days ago

Prompt type:

Write code

Category:

Coding

Media type:

Use with AI:

ChatGPT from OpenAI
View Full Output

Example output


Sure, here's an example of a JavaScript function that accepts an array of letters and returns an array of words:

function lettersToWords(letters) { const letterMap = { 'a': ['apple', 'ant', 'arrow'], 'b': ['banana', 'ball', 'bird'], 'c': ['cat', 'car', 'cup'], // Add more letters and corresponding words as needed }; const words = []; letters.forEach(letter => { if (letterMap[letter]) { words.push(...letterMap[letter]); } }); return words; } // Example usage const letters = ['a', 'b', 'c']; const words = lettersToWords(letters); console.log(words); // Output: ['apple', 'ant', 'arrow', 'banana', 'ball', 'bird', 'cat', 'car', 'cup']

In this example, the function lettersToWords takes an array of letters as input and uses a predefined letterMap object to map each letter to an array of words. The function then iterates through the input letters, checks if they exist in the letterMap, and adds the corresponding words to the words array. Finally, it returns the array of words. You can customize the letterMap object to include more letters and their corresponding words as needed.

Description or instructions

The prompt requires the creation of a JavaScript function that accepts a specified input and returns a specified output. The function should be designed to perform a specific task or computation based on the given input and produce the desired output.

Prompt

Write a JavaScript function. It accepts [input] and returns [output].