You can also look at JS Learner Forms documentation for other forms.
You will be working in then jsforms-source/1_first-form.js file.
- Section 1: Variables and Operations
- Section 2: Function Creation
- Section 3: Arrays, Loops, and Function Calls
- Section 4: Combining Complex Functions
- Section 5: Copying Arrays
- Section 6: Combining existing code to solve new problems
- Section 7: Looping and inequality comparison
- Section 8: The Other JS Learner Forms
You will explore the creation and use of variables.
Here you will declare and initialize variables.
Create a variable at the top of the block called "a". You will need to use let
or var
.
Hints
Be sure you use "var" or "let" to declare your variable
Code
Example 1
const jsforms = (function () {
'use strict';
let ?;
return {
};
})();
Example 2
const jsforms = (function () {
'use strict';
var ?;
return {
};
})();
Set the initial value of a to 5.
Hints
Initialize a variable with equals: var a = ...
Code
Example 1
const jsforms = (function () {
'use strict';
let a = ?;
return {
};
})();
Example 2
const jsforms = (function () {
'use strict';
var a = ?;
return {
};
})();
Now that you have a variable you will assign it a value different then what it was initialized with.
Now assign variable a
with the value of 3 + 7
.
Hints
Add two numbers using the plus (+) operator
Code
Example 1
let a = 5;
a = ? + ?;
return {
};
Example 2
var a = 5;
a = ? + ?;
return {
};
Create a variable called b
and initialize it with the value `"Hello, World!"
Hints
Initialize a variable with equals: var b = ...
Code
Example 1
let b = ?;
return {
};
Example 2
var b = ?;
return {
};
YOu will make a call to a method on an object.
Use the console.log
method to log the value contained in variable b
to the console.
Hints
Console.log might be just the ticket...
Code
Example 1
console.log(?);
return {
};
Example 2
console.log(?);
return {
};
You well now expose values to the outside world.
Expose the variable b
to the outside world, by adding it the the object being exported.
Hints
Find return {};
add a "b" -> return {b};
Code
Example
return {
?,
};
Time to create functions and have them return a value.
Write a function called greet
that returns the value "Hello!". (note: don't forget the "!")
Hints
Functions start with the keyword function
followed by the name of the function and then a set of parenthesis ()
. All the logic of a function is contained an open curly brace {
and a closed curly brace }
. What the function returns follows the key word return
.
Code
Example
function greet() {
return _string_;
}
return {
greet,
};
Modify the greet function to take a greeting and return the greeting if it was passed.
Hints
Add condition to greet to handle custom greeting case. (Try using an "if/else" structure. "If" is a control structure)
Code
Example
function greet (greeting) {
if (_something_ === undefined) {
return _string_;
} else {
return _something_ + '!';
}
}
return {
greet,
};
Create a function that takes a number and returns its square.
The function called square
will be passed 1 and should return 1.
Hints
Add square function that returns a simple value rather then worrying about actually squaring the a value.
Code
Example
function square(_something_) {
return ?;
}
return {
square,
};
Modify the square
function such that it returns the square of the value passed in.
Hints
- Add logic to correctly square a number (remember n^2 <=> n * n)
- Move to library function to perform square operation
- Math is a built in library...
Code
Example 1
function square(_something_) {
return ? * ?;
}
return {
square,
};
Example 2
function square(_something_) {
return Math.pow(?, 2);
}
return {
square,
};
Create a function that takes a number and returns the square root of that number. (note: use the Math library).
Create a function called squareRoot
that will be passed 1 and return 1.
Hints
Create a function that returns a simple value rather then worrying about actually performing a square root of the value.
Code
Example
function squareRoot(_something_) {
return ?;
}
return {
squareRoot,
};
Modify the squareRoot
function to return the square root of the value passed.
Hints
Add logic to properly take square root (There is more than one way to solve this in one line, maybe with Math)
Code
Example
function squareRoot(_something_) {
return Math.sqrt(_something_);
}
return {
squareRoot,
};
You will use functions to work with arrays and loops.
You are going to create a function called sum
that adds numbers together.
Create a sum
function that accepts an array and returns 1.
Hints
Create a function that returns a simple number instead of worrying about actually adding numbers together.
Code
Example
function sum(_something_) {
return ?;
}
return {
sum,
};
Modify the sum
function so that it adds the first two numbers in an array.
Hints
Add logic to sum 1 or two numbers (An if
structure might help here)
Code
Example
function sum(_array_) {
if(_array_.length === 1) {
return _array_[0];
} else {
return _array_[0] + _array_[1];
}
}
return {
sum,
};
Modify the sum
function to add all numbers in an array.
Hints
Add logic to sum an arbitrary length array of numbers (A for loop might help to accomplish this loops, like ifs, are control structures)
Code
Example 1 (let)
function sum(_array_) {
let result = ?;
for(let index = _number_; _number_ < _number_; index += 1) {
result += _something_;
}
return result;
}
return {
sum,
};
Example 2 (var)
function sum(_array_) {
var result = ?;
for(let index = _number_; _number_ < _number_; index += 1) {
result += _something_;
}
return result;
}
return {
sum,
};
Create a function that squares each number in an array.
The steps you will take to building the squareAll
function are:
- Add a
squareAll
function that returns a constant value. - Chang the
squareAll
function to perform square computation on array value
Create a function called squareAll
that takes an array and returns the square of the first element.
Hints
Create a function that only squares the first value of an array and returns that value as an array.
Code
Example
function squareAll (values) {
let result = square(_array_[_number_]);
return [_something_];
}
return {
squareAll,
};
Modify the squareAll
function so that it now squares each number in the array.
Hints
Add logic to square all numbers in array (How did you solve sum?)
Code
Example
function squareAll (values) {
for(let index = _number_; _number_ < _number_; index += 1) {
result[index] = square(_something_[index]);
}
return ?;
}
return {
squareAll,
};
You will use previous functions chained to gether to give us new results.
Create a function that takes an array of numbers, squares each number and then returns the sum of those squares.
Create a function called sumOfSquares
that takes an array and returns the square of the first element.
Hints
Try to do the following:
- Add function
sumOfSquares
that returns the first element of the parameter - Then add logic to square a single number inside the
sumOfSquares
function
Code
Example
function sumOfSquares(_something_) {
return square(?[0]);
}
return {
sumOfSquares,
};
Modify the sumOfSquares
function so that it squares all numbers in the array.
Hints
Add logic to square all numbers and return the sum (squareAll and sum might be useful to accomplish this)
Code
Example
function sumOfSquares(_something_) {
return add(squareAll(?));
}
return {
sumOfSquares,
};
You are going to use functions to explore the Array.prototype.slice
method.
You will now create a function called buildVector
that returns a copy of the array as it was passed.
A vector is an ordered set of points which describes a "directed line segment," in other words, a vector is a line segment with an arrow
It returns a vector (array) containing the same numbers as the original -- try returning the array you get in your function
Create a function called buildVector
that returns the same array passed to it.
Hints
Add buildVector function (Would it be possible to just return something?)
Code
Hint
function buildVector (values) {
return ?;
}
return {
buildVector,
};
Modify the buildVector
function so that it returns a copy of the array passed to it. (note: use the slice
method on array.)
Hints
Add logic to return a copy of vector array (Slice will create a new array just like the old one...)
Code
Hint
function buildVector (values) {
let vector = _array_.slice(_number_);
return vector;
}
return {
buildVector,
};
You will now take code you wrote and use it define more complex behaviors
You will create a function called magnitude
that will calculate the magnitude of all given numbers.
A magnitude is the length of a vector. You will explore computing the magnitude in the following tests
The magnitude of a vector is computed by:
- Square all numbers in the array
- Add all squared values together
- Take the square root of the sum of squares
such that given:
a = 1 b = 2 c = 2
the magnitude is:
√d = √(a² + b² + c²)
or
√d = √(1² + 2² + 2²)
√d = √(1 + 4 + 4)
√d = √9
d = 3
Create a function called magnitude
that returns the first item in an array that is given to it.
Hints
Can you just return a value from the array?
Code
Example
function magnitude(_something_) {
return ?[0];
}
return {
magnitude,
};
Modify the magnitude
function that returns the positive value of the first item in an array that is given to it.
Hints
Add logic to ensure magnitude is always positive (Hint: squaring a negative number makes it positive)
A magnitude is the length of a vector. you will explore computing the magnitude in the following tests.
Currently the magnitude of a vector is computed by:
- Square first value in the array
- Take the square root of the sum of that value
Code
Example
function magnitude(_something_) {
return squareRoot(square(?[0]));
}
return {
magnitude,
};
Modify the magnitude
function so that it returns the magnitude of the first 2 items in an array given to it.
Hints
Add logic to perform proper magnitude calculation for length 1 and 2 vectors (A length 2 vector's magnitude can be computed by squareRoot(vector[0]^2 + vector[1]^2))
A magnitude is the length of a vector. You will explore computing the magnitude in the following tests.
Currently the magnitude of a vector is computed by:
- Square the fist 2 values of the array
- Add those values together
- Take the square root of the sum of those values
Code
Example
function magnitude(_something_) {
if (?.length === 2){
return squareRoot(square(?[0]) + square(?[1]));
} else {
return squareRoot(square(?[0]));
}
}
return {
magnitude,
};
Modify the magnitude
function so that it returns the magnitude of all the values in the array.
Hints
Add logic to compute the magnitude of an arbitrary length vector. (The general magnitude formula is the square root of the sum of the squares. Can you apply functions you have already created?)
A magnitude is the length of a vector. You will explore computing the magnitude in the following tests.
The magnitude of a vector is computed by:
- Square all numbers in the array
- Add all squared values together
- Take the square root of the sum of squares
Code
Example
function magnitude(_something_) {
return squareRoot(sumOfSquares(?));
}
return {
magnitude,
};
Now it is time to use functions to examine looping and comparisons.
You will now create a function called getVectorsShorterThan
that will take an array of arrays. It will compare each array based on its magnitude and return all arrays that have a magnitude smaller then the number provided.
the signature will be:
getVectorsShorterThan(length, arrayOfVectors)
Such that length
is the number for which You will compare magnitudes against, and arrayOfVectors
is the array of arrays, where then inner arrays are to contain numbers.
It returns an array of one vector when the vector is shorter than 5 -- arguments are: length, arrayOfVectors
Create a the function getVectorsShorterThan
where the first parameter is length
and the second parameter is arrayOfVectors
that returns all vectors where the magnitude is less then 5. There is only one vector at this point and its length is less then 5.
Hints
Can you just return the first value of the array?
Code
Example
function getVectorsShorterThan(length, arrayOfVectors) {
return [?[0]];
}
return {
getVectorsShorterThan,
};
Modify the getVectorsShorterThan
function so that it will return an empty array if the first vector has a magnitude longer then length
.
Hints
Add logic to handle the case where a vector is too long (How might you compare the magnitude of a vector to a length value?)
Code
Example 1 (let)
function getVectorsShorterThan(length, arrayOfVectors) {
let firstMagnitude = magnitude(?[0]);
if (length <= firstMagnitude) {
return ?;
}
return [?[0]];
}
return {
getVectorsShorterThan,
};
Example 2 (var)
function getVectorsShorterThan(length, arrayOfVectors) {
var firstMagnitude = magnitude(?[0]);
if (length <= firstMagnitude) {
return ?;
}
return [?[0]];
}
return {
getVectorsShorterThan,
};
Modify the getVectorsShorterThan
function so that it returns all vectors if the first one has a magnitude less then the length.
Hints
Add logic to handle 0, 1 or 2 vector cases
Code
Example 1 (let)
function getVectorsShorterThan(length, arrayOfVectors) {
let firstMagnitude = magnitude(?[0]);
if (length <= firstMagnitude) {
return [];
}
return ?;
}
return {
getVectorsShorterThan,
};
Example 2 (var)
function getVectorsShorterThan(length, arrayOfVectors) {
var firstMagnitude = magnitude(?[0]);
if (length <= firstMagnitude) {
return [];
}
return ?;
}
return {
getVectorsShorterThan,
};
Modify the getVectorsShorterThan
function so that it now returns only those vectors with a magnitude less then the length.
Hints
Move to general code for filtering vecctors (Is there a way you can use a known control structure to check all vectors?)
Code
Example 1 (let)
function getVectorsShorterThan(length, arrayOfVectors) {
let result = ?;
for(let index = _number_; _number_ < _number_; index += 1) {
let currentMagnitude = magnitude(?[index]);
if (currentMagnitude < length) {
result.push(buildVector(?[0]));
}
}
return result;
}
return {
getVectorsShorterThan,
};
Example 2 (var)
function getVectorsShorterThan(length, arrayOfVectors) {
var result = ?;
for(let index = _number_; _number_ < _number_; index += 1) {
let currentMagnitude = magnitude(?[index]);
if (currentMagnitude < length) {
result.push(buildVector(?[0]));
}
}
return result;
}
return {
getVectorsShorterThan,
};