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;
false
if (sunny) {
    umbrella = false; 
} else if (rainy) {
    umbrella = true; 
} else {
    umbrella = false; 
}

console.log(umbrella);
false

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

function convert(num) {
    if (num >= 1) {
        convert(num) % 2;
      } 
    console.log(num % 2, end = ' ')
    }

convert(4);
evalmachine.<anonymous>:1
function convert(num) {
                ^

RangeError: Maximum call stack size exceeded
    at convert (evalmachine.<anonymous>:1:17)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
    at convert (evalmachine.<anonymous>:3:9)
num = int(input("Enter a decimal number to be converted into binary."))
def convert(num):
	
	if num >= 1:
		convert(num // 2)  # double slash : divide the first number by the second, rounds to the nearest integer
	print(num % 2, end = '') # end = '' signifies what will come between each printed num
 
# Driver Code
print("Binary of num " + str(num) + " is:", end=" ")
convert(num)