Lesson 1 of 15

IP Addresses

IP Addresses

An IP address (Internet Protocol address) is a unique numerical label assigned to each device on a network. IPv4 addresses consist of four octets — numbers from 0 to 255 — separated by dots.

Structure

192.168.1.1
 │    │  │ │
 │    │  │ └─ Host (octet 4)
 │    │  └─── Host (octet 3)
 │    └────── Network (octet 2)
 └─────────── Network (octet 1)

Each octet is an 8-bit number (0–255), so an IPv4 address is 32 bits total.

Special Addresses

AddressPurpose
127.0.0.1Loopback (localhost)
0.0.0.0Unspecified / default route
255.255.255.255Broadcast
10.x.x.xPrivate (Class A)
192.168.x.xPrivate (Class C)

Validation

A valid IPv4 address must:

  1. Have exactly 4 octets separated by dots
  2. Each octet must be a number between 0 and 255
  3. No leading zeros (e.g., 01.02.03.04 is invalid)

Your Task

Implement parseIPv4(str) that takes a string and returns an object with:

  • valid: boolean indicating if the IP is valid
  • octets: array of 4 numbers (or empty array if invalid)
  • type: one of "loopback", "private", "broadcast", or "public" (or "invalid" if not valid)
Node.js loading...
Loading...
Click "Run" to execute your code.