Advertisement
Guest User

q4 answer

a guest
Mar 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validateIPv4Address(address) {
  2.   const arrayedAddress = address.split('.');
  3.   if (arrayedAddress.length !== 4) {
  4.     return false;
  5.   }
  6.  
  7.   const invalidOctet = arrayedAddress.filter(octet => parseInt(octet, 10) < 0 || parseInt(octet, 10) > 255);
  8.  
  9.   return invalidOctet.length === 0;
  10. }
  11.  
  12. function clasifyIPv4Address(address) {
  13.   const subnetClasses = {
  14.     A: {
  15.       lowest: 0,
  16.       highest: 127,
  17.     },
  18.     B: {
  19.       lowest: 128,
  20.       highest: 191,
  21.     },
  22.     C: {
  23.       lowest: 192,
  24.       highest: 223,
  25.     },
  26.     D: {
  27.       lowest: 224,
  28.       highest: 239,
  29.     },
  30.     E: {
  31.       lowest: 240,
  32.       highest: 255,
  33.     },
  34.   };
  35.  
  36.   if (validateIPv4Address(address)) {
  37.     const addressPointer = parseInt(address.split('.')[0]);
  38.     for (let subnetClass in subnetClasses) {
  39.       const lowest = subnetClasses[subnetClass].lowest;
  40.       const highest = subnetClasses[subnetClass].highest;
  41.       if (addressPointer >= lowest && addressPointer <= highest) {
  42.         return subnetClass;
  43.       }
  44.     }
  45.   }
  46.  
  47.   return new Error('Invalid IPv4 address');
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement