HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <h2>Lesson notes</h2>
1 <h2>Lesson notes</h2>
2 <p><strong>condition</strong>formally looks like this:</p>
2 <p><strong>condition</strong>formally looks like this:</p>
3 <p>IF (condition) THEN do something IF (other_condition) THEN do some other thing IF (none of those conditions) THEN do something else</p>
3 <p>IF (condition) THEN do something IF (other_condition) THEN do some other thing IF (none of those conditions) THEN do something else</p>
4 <p>JavaScript function that accepts a number and returns its absolute value:</p>
4 <p>JavaScript function that accepts a number and returns its absolute value:</p>
5 <p>Conditions can be either true or false. For example, (num &gt; 0) is true when num is 9 or 15, but it's false when num is -18 or, say, 0.</p>
5 <p>Conditions can be either true or false. For example, (num &gt; 0) is true when num is 9 or 15, but it's false when num is -18 or, say, 0.</p>
6 <p>Things that answer TRUE or FALSE are called<strong>predicates</strong>.</p>
6 <p>Things that answer TRUE or FALSE are called<strong>predicates</strong>.</p>
7 <p>Other math predicates in JavaScript include:</p>
7 <p>Other math predicates in JavaScript include:</p>
8 <p>=== !== &gt; &lt; &gt;= &lt;=</p>
8 <p>=== !== &gt; &lt; &gt;= &lt;=</p>
9 <p>Examples:</p>
9 <p>Examples:</p>
10 <p>512 === 512; // true 512 === 988; // false 512 !== 378; // true 512 !== 512; // false 512 &gt; 500; // true 512 &gt; 689; // false 512 &lt; 900; // true 512 &lt; -30; // false 512 &gt;= 512; // true 512 &gt;= 777; // false 512 &lt;= 512; // true 512 &lt;= 600; // true 512 &lt;= 5; // false</p>
10 <p>512 === 512; // true 512 === 988; // false 512 !== 378; // true 512 !== 512; // false 512 &gt; 500; // true 512 &gt; 689; // false 512 &lt; 900; // true 512 &lt; -30; // false 512 &gt;= 512; // true 512 &gt;= 777; // false 512 &lt;= 512; // true 512 &lt;= 600; // true 512 &lt;= 5; // false</p>
11 <p>AND (&amp;&amp;):</p>
11 <p>AND (&amp;&amp;):</p>
12 <p>OR (||):</p>
12 <p>OR (||):</p>
13 <p>NOT (!):</p>
13 <p>NOT (!):</p>
14 <p>Alternative way of implementing abs function:</p>
14 <p>Alternative way of implementing abs function:</p>
15 <p>Think of another way of implementing the same function using &gt;= sign.</p>
15 <p>Think of another way of implementing the same function using &gt;= sign.</p>
16 <p><em>Same function</em>means "the function behaves the same way from the outside, but the insides (the implementation) can be different".</p>
16 <p><em>Same function</em>means "the function behaves the same way from the outside, but the insides (the implementation) can be different".</p>
17 <h2>Recommended watching</h2>
17 <h2>Recommended watching</h2>
18 <ul><li><a>AND OR NOT - Logic Gates Explained - Computerphile</a></li>
18 <ul><li><a>AND OR NOT - Logic Gates Explained - Computerphile</a></li>
19 <li><a>See How Computers Add Numbers In One Lesson</a></li>
19 <li><a>See How Computers Add Numbers In One Lesson</a></li>
20 </ul><h2>Optional</h2>
20 </ul><h2>Optional</h2>
21 <p>JavaScript and many other languages also have a short-hand version of the if statement: it's called<strong>conditional</strong>or<strong>ternary</strong>operator:</p>
21 <p>JavaScript and many other languages also have a short-hand version of the if statement: it's called<strong>conditional</strong>or<strong>ternary</strong>operator:</p>
22 <p>Here there is only one condition and two options: one for true and one for false.</p>
22 <p>Here there is only one condition and two options: one for true and one for false.</p>
23 <p>We create an absValue and assign it a value. This value depends on the condition: if condition is true, then num is used, otherwise -num is used.</p>
23 <p>We create an absValue and assign it a value. This value depends on the condition: if condition is true, then num is used, otherwise -num is used.</p>
24 <h2>Lesson transcript</h2>
24 <h2>Lesson transcript</h2>
25 <p>So far we've been using our programs as glorified calculators. Of course, they can do more, and the next big idea is to make computers make decisions based on some information.</p>
25 <p>So far we've been using our programs as glorified calculators. Of course, they can do more, and the next big idea is to make computers make decisions based on some information.</p>
26 <p>Check this out: in mathematics there is a concept of absolute value. This is how it's defined:</p>
26 <p>Check this out: in mathematics there is a concept of absolute value. This is how it's defined:</p>
27 <p>Don't worry, it's simple: if number is positive, then its absolute value is the same as the number; if number is negative, then its absolute value is the negation of the number. Basically, drop the minus if it's there and that's it.</p>
27 <p>Don't worry, it's simple: if number is positive, then its absolute value is the same as the number; if number is negative, then its absolute value is the negation of the number. Basically, drop the minus if it's there and that's it.</p>
28 <p>One way to think of it is: it's the distance from zero.</p>
28 <p>One way to think of it is: it's the distance from zero.</p>
29 <p>Imagine you want to describe a black box - a function - that accepts a number and returns its absolute value. You'll need to make a rule inside the box, something like this:</p>
29 <p>Imagine you want to describe a black box - a function - that accepts a number and returns its absolute value. You'll need to make a rule inside the box, something like this:</p>
30 <p>IF number is greater than 0 THEN return number IF number is less than 0 THEN return -number IF number is 0 THEN return 0</p>
30 <p>IF number is greater than 0 THEN return number IF number is less than 0 THEN return -number IF number is 0 THEN return 0</p>
31 <p>This is a<strong>condition</strong>, and formally it looks like this:</p>
31 <p>This is a<strong>condition</strong>, and formally it looks like this:</p>
32 <p>IF (condition) THEN do something IF (other_condition) THEN do some other thing IF (none of those conditions) THEN do something else</p>
32 <p>IF (condition) THEN do something IF (other_condition) THEN do some other thing IF (none of those conditions) THEN do something else</p>
33 <p>Let's make our function for real now:</p>
33 <p>Let's make our function for real now:</p>
34 <p>This function has one<strong>argument</strong>- it accepts one thing from the outside. Then we have the if keyword, then goes the condition in brackets, then - a block of instructions that will be running if that condition is met. Next is another condition with else if. else if means "if previous condition is not met but this new condition IS met, then do this next block of instructions".</p>
34 <p>This function has one<strong>argument</strong>- it accepts one thing from the outside. Then we have the if keyword, then goes the condition in brackets, then - a block of instructions that will be running if that condition is met. Next is another condition with else if. else if means "if previous condition is not met but this new condition IS met, then do this next block of instructions".</p>
35 <p>There can be multiple "else if" blocks, sometimes you have many options.</p>
35 <p>There can be multiple "else if" blocks, sometimes you have many options.</p>
36 <p>So now, after we've taken care of positive and negative numbers, there is one more option left: what if number is zero. Note that we don't check for zero explicitly, we just say else. It means "if none of the conditions above are met, then do this next block of instructions". You can safely think that if number is not positive and not negative, it can only be zero. But sometimes we make mistakes when thinking about conditions and options, and many problems in programming come from incorrect conditions.</p>
36 <p>So now, after we've taken care of positive and negative numbers, there is one more option left: what if number is zero. Note that we don't check for zero explicitly, we just say else. It means "if none of the conditions above are met, then do this next block of instructions". You can safely think that if number is not positive and not negative, it can only be zero. But sometimes we make mistakes when thinking about conditions and options, and many problems in programming come from incorrect conditions.</p>
37 <p>These conditions in brackets are things that can be either true or false. For example, (num &gt; 0) is true when num is 9 or 15, for example, but it's false when num is -18 or, say, 0.</p>
37 <p>These conditions in brackets are things that can be either true or false. For example, (num &gt; 0) is true when num is 9 or 15, for example, but it's false when num is -18 or, say, 0.</p>
38 <p>As you see, greater than and less than math symbols kind of give answers - YES or NO, TRUE or FALSE. There are other things that can give TRUE or FALSE answers:</p>
38 <p>As you see, greater than and less than math symbols kind of give answers - YES or NO, TRUE or FALSE. There are other things that can give TRUE or FALSE answers:</p>
39 <p>=== !== &gt; &lt; &gt;= &lt;=</p>
39 <p>=== !== &gt; &lt; &gt;= &lt;=</p>
40 <p>And here are few more examples:</p>
40 <p>And here are few more examples:</p>
41 <p>512 === 512; // true 512 === 988; // false 512 !== 378; // true 512 !== 512; // false 512 &gt; 500; // true 512 &gt; 689; // false 512 &lt; 900; // true 512 &lt; -30; // false 512 &gt;= 512; // true 512 &gt;= 777; // false 512 &lt;= 512; // true 512 &lt;= 600; // true 512 &lt;= 5; // false</p>
41 <p>512 === 512; // true 512 === 988; // false 512 !== 378; // true 512 !== 512; // false 512 &gt; 500; // true 512 &gt; 689; // false 512 &lt; 900; // true 512 &lt; -30; // false 512 &gt;= 512; // true 512 &gt;= 777; // false 512 &lt;= 512; // true 512 &lt;= 600; // true 512 &lt;= 5; // false</p>
42 <p>The branch of math that studies these true and false statements is called<strong>Boolean algebra</strong>. In general, statements of any nature, not just number systems, can be true or false. For example, "I am a human being" is<strong>true</strong>, while "China is located in South America" is<strong>false</strong>.</p>
42 <p>The branch of math that studies these true and false statements is called<strong>Boolean algebra</strong>. In general, statements of any nature, not just number systems, can be true or false. For example, "I am a human being" is<strong>true</strong>, while "China is located in South America" is<strong>false</strong>.</p>
43 <p>JavaScript has true and false values, and you can actually use them in conditions. For example, you can say if true, and this condition will always be met - because true is always true.</p>
43 <p>JavaScript has true and false values, and you can actually use them in conditions. For example, you can say if true, and this condition will always be met - because true is always true.</p>
44 <p>There are many aspects and details in Boolean algebra, but in programming we are mostly concerned about three basic operations: AND, OR, NOT.</p>
44 <p>There are many aspects and details in Boolean algebra, but in programming we are mostly concerned about three basic operations: AND, OR, NOT.</p>
45 <p>AND is when you want two conditions to be true. "I am a human being AND horses eat grass" is true, because both statements are true. "I am a human being AND pigs can fly" is false, becase one statement is true, but other one is false, so together, this whole thing joined by AND is false.</p>
45 <p>AND is when you want two conditions to be true. "I am a human being AND horses eat grass" is true, because both statements are true. "I am a human being AND pigs can fly" is false, becase one statement is true, but other one is false, so together, this whole thing joined by AND is false.</p>
46 <p>The symbol for AND is double ampersand &amp;&amp;. And this is the so called truth table for AND, it's like a cheat sheet:</p>
46 <p>The symbol for AND is double ampersand &amp;&amp;. And this is the so called truth table for AND, it's like a cheat sheet:</p>
47 <p>So, only TRUE AND TRUE give TRUE, and all other combinations include FALSE, so they result is FALSE.</p>
47 <p>So, only TRUE AND TRUE give TRUE, and all other combinations include FALSE, so they result is FALSE.</p>
48 <p>OR is when you want at least one condition to be true. Following the same examples, "I am a human being OR horses eat grass" is true. "I am a human being OR pigs can fly" is also true. Even though pigs don't fly, I AM a human being, so one of the two statements is true, which makes this whole thing joined by OR to be true.</p>
48 <p>OR is when you want at least one condition to be true. Following the same examples, "I am a human being OR horses eat grass" is true. "I am a human being OR pigs can fly" is also true. Even though pigs don't fly, I AM a human being, so one of the two statements is true, which makes this whole thing joined by OR to be true.</p>
49 <p>The symbol for OR is two vertical bars ||.</p>
49 <p>The symbol for OR is two vertical bars ||.</p>
50 <p>If there is true, then the result is true.</p>
50 <p>If there is true, then the result is true.</p>
51 <p>NOT is simple - it's the opposite. NOT true is false, NOT false is true. "NOT pigs can fly" is true, because "pigs can fly" is false, and NOT false is true.</p>
51 <p>NOT is simple - it's the opposite. NOT true is false, NOT false is true. "NOT pigs can fly" is true, because "pigs can fly" is false, and NOT false is true.</p>
52 <p>The symbol for NOT is an exclamation mark !.</p>
52 <p>The symbol for NOT is an exclamation mark !.</p>
53 <p>In programming there are usually multiple ways to do the same thing. We can re-write our absolute value function differently and get the same result. Let's try to do it using these new ideas:</p>
53 <p>In programming there are usually multiple ways to do the same thing. We can re-write our absolute value function differently and get the same result. Let's try to do it using these new ideas:</p>
54 <p>So now we have only two conditions:</p>
54 <p>So now we have only two conditions:</p>
55 <ol><li>If number is zero OR number is greater than 0, then return the number.</li>
55 <ol><li>If number is zero OR number is greater than 0, then return the number.</li>
56 <li>In any other case return -number.</li>
56 <li>In any other case return -number.</li>
57 </ol><p>You can think of yet another way of writing the same function using "greater than or equal" sign, for example. When I say "same function", I mean that the function behaves the same way from the outside, but the insides - the implementation, can be different.</p>
57 </ol><p>You can think of yet another way of writing the same function using "greater than or equal" sign, for example. When I say "same function", I mean that the function behaves the same way from the outside, but the insides - the implementation, can be different.</p>