Why do we need tests?.… Automated testing means that tests are written separately, in addition to the code.… Tests start to fail.
Go to 3, update the implementation till tests give no errors.… Let’s add one more test to check that pow(3, 4) = 81.… (or test groups).
points:
Because of that last property, in computer graphics it’s possible to optimize intersection tests… The formula for a 2-points curve:
P = (1-t)P1 + tP2
For 3 control points:
P = (1−t)2P1 + 2(1−t)tP2… + t2P3
For 4 control points:
P = (1−t)3P1 + 3(1−t)2tP2 +3(1−t)t2P3 + t3P4
These are vector equations… For instance, the 3-point curve is formed by points (x,y) calculated as:
x = (1−t)2x1 + 2(1−t)tx2 +… 3 control points, and then as t moves from 0 to 1, for each value of t we’ll have (x,y) of the curve.
Please note:
“Start of a line” formally means “immediately after a line break”: the test… Please note:
“End of a line” formally means “immediately before a line break”: the test… As we can see, there are 2 matches instead of 3.… That’s because there’s no newline after 3 (there’s text end though, so it matches $).… Unlike the anchors ^ $, that only test the condition (start/end of a line), \n is a character, so it
Tests: isFinite and isNaN.Remember these two special numeric values?… are special functions to check for them:
isNaN(value) converts its argument to a number and then tests… For regular number tests:
isNaN(value) converts its argument to a number and then tests it for being… NaN
Number.isNaN(value) checks whether its argument belongs to the number type, and if so, tests it… Infinity
Number.isFinite(value) checks whether its argument belongs to the number type, and if so, tests
It has 3 modes:
If the regexp doesn’t have flag g, then it returns the first match as an array with… There are 3 differences from match:
It returns an iterable object with matches instead of an array.… If there are no parentheses in the regexp, then there are only 3 arguments: func(str, offset, input).… Same global regexp tested… That’s exactly because regexp.lastIndex is non-zero in the second test
Several conditions: “else if”.Sometimes, we’d like to test… But after a closer look, we can see that it’s just an ordinary sequence of tests:
The first question… mark checks whether age < 3.
For instance, the loop below outputs i while i < 3:… The loop below runs alert(i) for i from 0 up to (but not including) 3:… condition
i < 3
Checked before every loop iteration. If false, the loop stops.… That is, begin executes once, and then it iterates: after each condition test… Summary.We covered 3 types of loops:
while – The condition is checked before each iteration.
do..while
.
\', \", \`
Quotes
\\
Backslash
\t
Tab
\b, \f, \v
Backspace, Form Feed, Vertical Tab –… Note that \n is a single “special” character, so the length is indeed 3.… There is a slight inconvenience with indexOf in the if test.… It’s the right choice if we need to test for the match, but don’t need its position:… There are 3 types of quotes.
Equals: a == b, please note the double equality sign == means the equality test, while a single one a… That’s why (3) null >= 0 is true and (1) null > 0 is false.… The equality check (3) returns false because undefined only equals null, undefined, and no other value
How Map compares keys
To test keys for equivalence, Map uses the algorithm SameValueZero.… Iteration over Map.For looping over a map, there are 3… The callback function passed in forEach has 3 arguments: a value, then the same value valueAgain, and
It has 3 working modes:
If the regular expression has flag g, it returns an array of all matches:… Testing
Sets.For instance, [eao] means any of the 3 characters: 'a', 'e', or 'o'.
That’s called a set.… For instance:
\d – is the same as [0-9],
\w – is the same as [a-zA-Z0-9_],
\s – is the same as [\t\… 𝒳𝒴] – are not two, but four characters:
left half of 𝒳 (1),
right half of 𝒳 (2),
left half of 𝒴 (3)
We’ll talk about let/const variables here
In JavaScript, there are 3 ways to declare a variable… practical uses, for instance, as a random number generator to generate random values for automated tests… Step 3.… If we call counter() multiple times, the count variable will be increased to 2, 3… In the code below, all 3 of them:
A
Here’s a more flexible test… ;p> first child (taking all but two first letters of "Example: ")
ends at the position 3… endOffset – node and offset of the end,
in the example above: first text node inside <b> and 3.… Here’s the test stand to see them in action:… Here’s a screenshot of a selection with 3 ranges, made in Firefox:
They may tweak results of “artificial tests” compared to “normal usage”, especially when we benchmark… The character "T" is used as the delimiter.… () that gives the number of milliseconds from the start of page loading with microsecond precision (3
It has a numeric value:
elem.nodeType == 1 for element nodes,
elem.nodeType == 3 for text nodes,
elem.nodeType… In modern scripts, we can use instanceof and other class-based tests… It has a numeric value: 1 for elements,3 for text nodes, and a few others for other node types.
The first number has 6 digits, and then a number of 3… Some regular expression engines have tricky tests and finite automations that allow to avoid going through
For instance, let’s test if the text starts with Mary:… Similar to this, we can test if the string ends with snow using snow$:… Regular expressions should be used for more complex tests.… Testing for a full match.Both anchors together ^...$ are often used to test whether or not a string fully… Anchors have “zero width”
Anchors ^ and $ are tests. They have zero width.
A word boundary \b is a test, just like ^ and $.… So, it matches the pattern \bHello\b, because:
At the beginning of the string matches the first test… Then the test \b matches again, as we’re between o and a comma.… Word boundary \b doesn’t work for non-latin alphabets
The word boundary test \b checks that… But \w means a latin letter a-z (or a digit or an underscore), so the test doesn’t work for other characters
Here the switch starts to compare a from the first case variant that is 3.… Here the execution of case 3 starts from the line (*) and goes through case 5, because there’s no break… But for 3, the result of the prompt is a string "3", which is not strictly equal === to the… number 3.… So we’ve got a dead code in case 3! The default variant will execute.
Otherwise, if the first digit is 2, then the next must be [0-3].… other first digit is allowed)
We can write both variants in a regexp using alternation: [01]\d|2[0-3]… If we glue hours and minutes together, we get the pattern: [01]\d|2[0-3]:[0-5]\d.… The alternation | now happens to be between [01]\d and 2[0-3]:[0-5]\d.… Let’s correct that by enclosing “hours” into parentheses: ([01]\d|2[0-3]):[0-5]\d.
Please note: the lookahead is merely a test… More complex tests are possible, e.g. X(?=Y)(?=Z) means:
Find X.… If both tests passed, then the X is a match, otherwise continue searching.… =€) is just a test that it should be followed by €.
gets “input” variables and produces certain “outcome” is clearer, less prone to errors and easier to test… Using for polyfills.We use the global object to test for support of modern language features.… For instance, test if a built-in Promise object exists (it doesn’t in really old browsers):
Let’s see what happens during the pow(2, 3) call.
pow(2, 3).In the beginning of the call pow(2, 3) the… We can sketch it as:
Context: { x: 2, n: 3, at line 1 }
pow(2, 3)
That’s when the function… Then the previous context is restored:
Context: { x: 2, n: 3, at line 5 }
pow(2, 3)
When… it finishes, we have a result of pow(2, 3) = 8.… The recursion depth in this case was: 3.
As var ignores code blocks, we’ve got a global variable test… If we used let test instead of var test, then the variable would only be visible inside if:
Becomes…
undefined
NaN
null
0
true and false
1 and 0
string
Whitespaces (includes spaces, tabs \t,… It happens in logical operations (later we’ll meet condition tests and other similar things) but can… null
0
true / false
1 / 0
string
The string is read “as is”, whitespaces (includes spaces, tabs \t,
The range: {3,5}, match 3-5 times
To find numbers from 3 to 5 digits we can put the limits into curly… Then a regexp \d{3,} looks for sequences of digits of length 3 or more:
Let’s say we have 3 nested elements FORM > DIV > P with a handler on each of them:… So if we click on <p>, then we’ll see 3 alerts: p → div → form.… The standard DOM Events describes 3 phases of event propagation:
Capturing phase – the event goes down… Note that while formally there are 3 phases, the 2nd phase (“target phase”: the event reached the element… one that has the handler on it)
event.eventPhase – the current phase (capturing=1, target=2, bubbling=3)
Property existence test, “in” operator.A notable feature of objects in JavaScript, compared to many other… So we can easily test whether the property exists:… If we omit quotes, that means a variable should contain the actual name to be tested.
whole project (not just the open file), and integrates with a version management system (like git), a testing
That may be helpful for automated testing.… For automated testing, to “click the button” in the script and see if the interface reacts correctly.
the example above, the result of expression (a = b + 1) is the value which was assigned to a (that is 3)… Then, 3 + 4 is evaluated and returned as the result.… Without them: a = 1 + 2, 3 + 4 evaluates + first, summing the numbers into a = 3, 7, then the assignment… operator = assigns a = 3, and the rest is ignored.… It’s like (a = 1 + 2), 3 + 4.
To do the request, we need 3 steps:
Create XMLHttpRequest:… An XMLHttpRequest object travels them in the order 0 → 1 → 2 → 3… → … → 3 → 4.… State 3 repeats every time a data packet is received over the network.… There are 3 methods for HTTP-headers:
setRequestHeader(name, value)
Sets the request header with the
Then in result[2] goes the group from the second opening paren ([a-z]+) – tag name, then in result[3]… The array has the length of 3,… The array length is permanent: 3.… Just like match, it looks for matches, but there are 3 differences:
It returns not an array, but an
So the test of obj instanceof Class can be rephrased as Class.prototype.isPrototypeOf(obj).
The element was removed, but the array still has 3… elements, we can see that arr.length == 3.… In the next example, we remove 3 elements and replace them with the other two:… On the 3rd run, sum = 3 and we add one more element to it, and so on…
The calculation flow:… the third call
3
3
6
the fourth call
6
4
10
the fifth call
10
5
15
Here we can clearly see how
For instance, the Promise.all below settles after 3 seconds, and then its result is an array [1, 2, 3… For instance, here the results are [1, 2, 3]:
If we check event.code == 'KeyZ' in our code, then for people with German layout such test will pass… prevents the default action, so nothing appears in the <input> for keys that don’t pass the test
A separate function is not only easier to test and debug – its very existence is a great comment!… The second variant uses an additional function isPrime(n) to test
Constructor mode test: new.target.
read about new bleeding-edge features, including those that are “almost standard” (so-called “stage 3”
And also there are tests for them:
Object.isExtensible(obj)
Returns false if adding properties is forbidden
For instance, when we create an array [1, 2, 3], the default new Array() constructor is used internally… Here’s the overall picture (for 3 built-ins to fit):
Let’s check
Similar to that, \2 would mean the contents of the second group, \3 – the 3rd group, and so on.
Most of the time, OR || is used in an if statement to test
For instance, this function returns a resolved promise with the result of 1; let’s test it:
But of course in the result only the first two will be counted, so the result in the code above is 3.… Now let’s say we have an array [3,
code above:
We load 1.js, then if there’s no error…
We load 2.js, then if there’s no error…
We load 3.… loadScript('3.js', function(error, script) {
if (error) {
handleError(error);
Proxies can’t intercept a strict equality test ===
Proxies can intercept many operators,… But there’s no way to intercept a strict equality test for objects.… Object equality tests === can’t be intercepted.
You might want to open this page in two browser windows to test the code below.
Only first 50 results are shown.