Complete the JS part of the mad libs game.
Finish implementing the JS part of battleship
Finish the JS part of rock paper scissors
Function constructors with functions defined
1. Build a function constructor called Question to describe a question. A question should include: a) question itself b) the answers from which the player can choose the correct one (choose an adequate data structure here, array, object, etc.) c) correct answer (I would use a number for this) 2. Create a couple of questions using the constructor 3. Store them all inside an array 4. Select one random question and log it on the console, together with the possible answers (each question should have a number) (Hint: write a method for the Question objects for this task). 5. Use the 'prompt' function to ask the user for the correct answer. The user should input the number of the correct answer such as you displayed it on Task 4. 6. Check if the answer is correct and print to the console whether the answer is correct ot nor (Hint: write another method for this).
These are examples of closures, functions that return functions within.
Sometimes you want to create functions that run write when the browser is initialized, meaning you do not need to call this function. It will execute on initialization. IIFE stands for immediately invoked function expression. Run the code below, notice how parameters are defined.
Functions can also return other functions. Run the code below and see what the output shows.
This shows how functions can be passed as arguments. Run the following code and see what the output looks like.
In lines 1 -5, we are creating a Person object. This is called a function constructor, it contains properties such as name, year of birth and job. We are using the this keyword to make our own copy of the variables, which is distinct from the function parameters. A prototype is a way of creating properties for the object which will be universal to all objects that inherit from this object. For Mark, Jane and John - their names, years of birth and job may be different. However the process of calculating their age is universal for all of them. We are also assuming they are siblings since we have given them all the same last name. Lines 11-13 connect to the idea of inheritance. John inherits the properties of Person, but also defines its own content for these properties. Name, year of birth and job. Lines 14-16 are calculating age for these people.