languages | Cell 5 | Cell 7 | Search

A variable named keywords is declared and assigned an array of 10 string values, including common programming concepts such as variable types and function calls.

Cell 6


var keywords = ['type', 'id', 'params',
                'body', 'generator', 'async',
                'range', 'expression', 'callee',
                'arguments'];

What the code could have been:

/**
 * Defines a list of key words used in function declarations.
 * @constant {string[]} keywords
 */
const keywords = [
  // Common function declaration attributes
  'type', 'id', 'params',
  // Function behavior and syntax
  'body', 'generator', 'async',
  // Function parameter or argument properties
  'range', 'expression', 'callee',
  // Function argument properties
  'arguments'
];

/**
 * Filters a list of keywords based on a specified pattern.
 * @param {string[]} keywords - The list of keywords to filter.
 * @param {string} pattern - The pattern to match.
 * @returns {string[]} The filtered list of keywords.
 */
function filterKeywords(keywords, pattern) {
  return keywords.filter(keyword => keyword.includes(pattern));
}

// Example usage:
const filteredKeywords = filterKeywords(keywords, 'async');
console.log(filteredKeywords); // Output: ['async', 'async', 'generator']

Code Breakdown

Variable Declaration

Array Contents