JavaScript Functions - "Ins & Outs" - Study
-
Fork and clone this repository.
-
Create a new branch,
training
, for your work. -
Install dependencies with
npm install
.
JavaScript function argument and return values
By the end of this lesson, students should be able to:
- Create and invoke functions that take an arbitrary number of arguments
- Create and invoke functions that take reference types as arguments
- Create and invoke functions that return reference types
- Create and invoke functions that take functions as arguments
- Create and invoke functions that return functions
JavaScript provides a mechanism to handle arguments not in the function
definition: the arguments
object.
This object is referred to as array like
and is available within any function.
We'll examine how this object is used by creating some seemingly parameterless
functions.
const product = function product() {
let result = 1;
for (let i = 0; i < arguments.length; i++) {
result = result * arguments[i];
}
return result;
};
// takes an arbitrary number of arguments (each arg
// should be a number), finds the largest one, and
// returns that member
const max = function max() {
};
Could we accomplish something similar using a single argument?
Write functions that take an array and return a product or max.
Reference types passed as arguments can be modified within the functions.
// Write a function that takes an array full of integers, doubles each value, and
// returns a new array with those values.
const arrayTimes2 = function arrayTimes2() {
let result = [];
for (let i = 0; i < arguments[0].length; i++) {
result[i] = arguments[0][i] * 2;
}
return result;
};
const addProperty = function addProperty(obj, prop, val) {
// this function takes an object and adds a property
// to it
};
Functions are valid arguments.
const transform = function (values, predicate, mutator) {
// if the predicate is true, mutate the value, otherwise don't mutate it
};
Reference type literals returned from functions create new instances of the type specified.
const createArray = function createArray() {
let result = [];
for (let i = 0; i < arguments.length; i++) {
result[i] = arguments[i];
}
return result;
};
Write a function that takes an array, a predicate, and a mutator. It should create a new array to hold all of either an existing element in the array or the transformed value for that element for which the predicate returns true. The replacement value should be the result of invoking the mutator on the existing element.
const arrayTransform = function arrayTransform(array, predicate, mutator) {
// if the predicate is true, mutate the value, otherwise don't mutate it
};
const createPerson = function createPerson(givenName, surname, bornOn, height, weight, eyeColor) {
};
Functions returned from functions generate a closure. Closures provide great utility.
const memoFactory = function (rememberMe) {
let memo = rememberMe;
return function () {
return memo;
};
};
Functions returned from functions generate a closure. Closures provide great utility.
const counterFactory = function(count) {
};
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].