MDN JS Skills Test: Functions 1

The Rando Name Picker App

The aim of this skill test is to assess whether Lester has understood the JavaScript Test your skills: Functions article.

The chooseName() function, which prints a random name in the grey rectangle above, calculates a random number between a range of two values, the largest being the number of items in the array of names.The other is generated with a JavaScript built-in math object, the result of which is saved in a variable and passed as an index value to select one of the names in the array based on its numeric position within the array.

Passing Arguments In Functions

The colored rectangle above is dynamically added to this page with a JavaScript function. I'm purposely not describing the background and border colors because of the way the function that produces it is constructed.

The function creates a new <div> element in the document with a specific height, width, border style, and background color using default parameters which can be overriden at runtime by passing arguments into the function, if desired.

When passing arguments, it's important to remember to use the primitive value, undefined, that's automatically assigned to variables which have been declared in place of any parameters not being changed if there is more than one passed into the function. They have to be called in order; otherwise, there will be an error.

If a programmer wants to use the default parameters, they can simply call the function as is, meaning without passing any arguments. Conversely, they can pass some or all new arguments to override the defaults for an effect they desire.

An undefined variable means it exists but has no value which is different from an undeclared variable which throws the 'Uncaught ReferenceError: varName is not defined', meaning the variable itself has not been initialized using the let or const keywords or, perhaps, that the variable is out of scope or other potential problem. There are many posts and articles discussing this topic which you're welcome to explore.

The Prettified Name Algorithm

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ducimus laborum, odio necessitatibus maiores corporis nostrum quam ea molestiae, culpa voluptates ipsam? Nostrum architecto laudantium nobis voluptas impedit deserunt ipsa enim?