JavaScript: IP converter

Last update: 06 Jun 23:20
1
Student

solution.js

Implement and export the ipToInt() and intToIp() functions, which convert the representation of an IP address from an 8-bit format to a 32-bit decimal number and vice versa.

The ipToInt() function takes a string as input and returns a number. The function intToIp() works the opposite way: it takes a number as input, and returns a string.

Examples

ipToInt('128.32.10.1'); // 2149583361
ipToInt('0.0.0.0'); // 0
ipToInt('255.255.255.255'); // 4294967295

intToIp(2149583361); // '128.32.10.1'
intToIp(0); // '0.0.0.0'
intToIp(4294967295); // '255.255.255.255'

Tips

  • IPv4
  • Use the parseInt() function to convert the string to the required number system
  • Explore the possibilities of the toString() method for a number, look at examples
  • Additionally, you can use the padStart() method

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests