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
| Address | Purpose |
|---|---|
127.0.0.1 | Loopback (localhost) |
0.0.0.0 | Unspecified / default route |
255.255.255.255 | Broadcast |
10.x.x.x | Private (Class A) |
192.168.x.x | Private (Class C) |
Validation
A valid IPv4 address must:
- Have exactly 4 octets separated by dots
- Each octet must be a number between 0 and 255
- No leading zeros (e.g.,
01.02.03.04is invalid)
Your Task
Implement parseIPv4(str) that takes a string and returns an object with:
valid: boolean indicating if the IP is validoctets: 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.