Review

if-else statements

Syntax:

if (condition) {

} else if (condition 2) {

} else {

}

for loops

for (let i = number; i < number; i++) {
    // code
}

Conditionals vs Booleans

Conditionals and booleans can be equivalent.

For example, let's say there are two booleans: rainy and sunny.

Let's look at the following code:

sunny = true; 
rainy = false;
if (sunny) {
    umbrella = false; 
} else if (rainy) {
    umbrella = true; 
} else {
    umbrella = false; 
}

console.log(umbrella);
Failed to start the Kernel. 

node:internal/modules/cjs/loader:1203

  return process.dlopen(module, path.toNamespacedPath(filename));

                 ^



Error: The module '/home/colinmills/anaconda3/lib/node_modules/ijavascript/node_modules/zeromq/build/Release/zmq.node'

was compiled against a different Node.js version using

NODE_MODULE_VERSION 48. This version of Node.js requires

NODE_MODULE_VERSION 108. Please try re-compiling or re-installing

the module (for instance, using `npm rebuild` or `npm install`).

    at Module._extensions..node (node:internal/modules/cjs/loader:1203:18)

    at Module.load (node:internal/modules/cjs/loader:997:32)

    at Module._load (node:internal/modules/cjs/loader:838:12)

    at Module.require (node:internal/modules/cjs/loader:1021:19)

    at require (node:internal/modules/cjs/helpers:103:18)

    at load (/home/colinmills/anaconda3/lib/node_modules/ijavascript/node_modules/node-gyp-build/index.js:22:10)

    at Object.<anonymous> (/home/colinmills/anaconda3/lib/node_modules/ijavascript/node_modules/zeromq/binding.js:1:43)

    at Module._compile (node:internal/modules/cjs/loader:1119:14)

    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)

    at Module.load (node:internal/modules/cjs/loader:997:32) {

  code: 'ERR_DLOPEN_FAILED'

}



Node.js v18.9.0. 

View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.

The code above is the same as below:

umbrella = !sunny && rainy;

console.log(umbrella);
false

To determine if two conditionals and booleans are the same, you can substitute the four possibilities that the two booleans (sunny and rainy) can be (listed below) into the conditional and boolean and see if both cases match:

sunny = true, rainy = true

sunny = true, rainy = false

sunny = false, rainy = true

sunny = false, rainy = false

Challenge

Using JavaScript, create an algorithm that takes in an IP address and a subnet mask and computes the network address.

Overview

As we've seen in Unit 4.1, an IP address is a 32 bit number that uniquely identifies each device. (See this for a recap). Something extra is that an IP address also comes with a subnet mask. A subnet mask is also a 32 bit number that identifies what network an IP address in in through a process that uses the bitwise AND.

In ANDing:

0 + 0 = 0

0 + 1 = 0

1 + 0 = 0

1 + 1 = 1


The following are the steps to determine the network that an IP address is in given the subnet mask:

Example: IP address: 192.168.0.1

Subnet mask: 255.255.255.0

  1. Convert the IP address into binary: 192.168.0.1 -> 11000000.10101000.00000000.00000001
  2. Convert the subnet mask into binary: 255.255.255.0 -> 11111111.11111111.11111111.00000000
  3. Do a bitwise AND operation on the binary IP address and subnet mask:
 11000000.10101000.00000000.00000001
+11111111.11111111.11111111.00000000
=11000000.10101000.00000000.00000000
  1. Convert the result back to decimal: 11000000.10101000.00000000.00000000 -> 192.168.0.0

The network address is 192.168.0.0