2015-11-08 54 views
0

我一直在设计一个程序来帮助使网络划分网络的任务变得特别容易。现在,所有被SUPPOSED做的是通过一个数组搜索并提取出匹配用户定义的属性的对象。目前,我遇到了一个问题,只选择数组中的第一个对象,无论用户选择什么。根据数组的属性在数组中搜索对象时出现问题。 (在JavaScript中)

注意:另外,每当我为“子网”输入一个大于1的值时,它给了我我的错误280. A类是唯一一个通用的工具。因此,如果有人知道如何正确搜索我想要的对象(不仅是“子网”,而是其他3个选项),我该怎么做?

我很抱歉,这段代码很残酷,有很多缺陷!我无法将自己的头围绕在我做错了的事情上,也无法解决它。我只是编程了几个月,这是迄今为止我最大的项目。

请测试一下,看看我做错了什么。我使用的主要测试环境是Codecademy的JavaScript解释器。

谢谢大家的任何建议或解决方案,你可能有!

//This program was developed by N1njaMDN. 

//This program's purpose is to help assist with subnetting. 

/*This section of the code is designated to holding all the subnet information. 
All of the data is put into an object in the format of 
{subNumA:#, 
subNumB:#, 
subNumC:#, 
hostNum:#, 
BlockSize:#, 
customSubMask:"#.#.#.#"} 
*/ 

/*To find block size, we subtract (the first portion of the custom subnet mask from the left that isnt a 255) 
from 256. Example: in 255.192.168.1, we would do 256-192. This number is our block size.*/ 

var objects = [ 
{ 
    cidr:8, 
    subNumA:1, 
    subNumB:0, 
    subNumC:0, 
    hostNum:16777214, 
    blockSize:256, 
    customSubMask:"255.0.0.0" 
}, 

{ 
    cidr:9, 
    subNumA:2, 
    subNumB:0, 
    subNumC:0, 
    hostNum:8388608, 
    blockSize:128, 
    customSubMask:"255.128.0.0" 
}, 

{ 
    cidr:10, 
    subNumA:4, 
    subNumB:0, 
    subNumC:0, 
    hostNum:4194304, 
    blockSize:64, 
    customSubMask:"255.192.0.0" 
}, 

{ 
    cidr:11, 
    subNumA:8, 
    subNumB:0, 
    subNumC:0, 
    hostNum:2097152, 
    blockSize:32, 
    customSubMask:"255.224.0.0" 
}, 

{ 
    cidr:12, 
    subNumA:16, 
    subNumB:0, 
    subNumC:0, 
    hostNum:1048576, 
    blockSize:16, 
    customSubMask:"255.240.0.0" 
}, 

{ 
    cidr:13, 
    subNumA:32, 
    subNumB:0, 
    subNumC:0, 
    hostNum:524288, 
    blockSize:8, 
    customSubMask:"255.248.0.0" 
}, 

{ 
    cidr:14, 
    subNumA:64, 
    subNumB:0, 
    subNumC:0, 
    hostNum:262144, 
    blockSize:4, 
    customSubMask:"255.252.0.0" 
}, 

{ 
    cidr:15, 
    subNumA:128, 
    subNumB:0, 
    subNumC:0, 
    hostNum:131072, 
    blockSize:2, 
    customSubMask:"255.254.0.0" 
}, 

{ 
    cidr:16, 
    subNumA:256, 
    subNumB:1, 
    subNumC:0, 
    hostNum:65536, 
    blockSize:256, 
    customSubMask:"255.255.0.0" 
}, 

{ 
    cidr:17, 
    subNumA:512, 
    subNumB:2, 
    subNumC:0, 
    hostNum:32768, 
    blockSize:128, 
    customSubMask:"255.255.128.0" 
}, 

{ 
    cidr:18, 
    subNumA:1024, 
    subNumB:4, 
    subNumC:0, 
    hostNum:16384, 
    blockSize:64, 
    customSubMask:"255.255.192.0" 
}, 

{ 
    cidr:19, 
    subNumA:2048, 
    subNumB:8, 
    subNumC:0, 
    hostNum:8192, 
    blockSize:32, 
    customSubMask:"255.255.224.0" 
}, 

{ 
    cidr:20, 
    subNumA:4096, 
    subNumB:16, 
    subNumC:0, 
    hostNum:4096, 
    blockSize:16, 
    customSubMask:"255.255.240.0" 
}, 

{ 
    cidr:21, 
    subNumA:8192, 
    subNumB:32, 
    subNumC:0, 
    hostNum:2048, 
    blockSize:8, 
    customSubMask:"255.255.248.0" 
}, 

{ 
    cidr:22, 
    subNumA:16384, 
    subNumB:64, 
    subNumC:0, 
    hostNum:1024, 
    blockSize:4, 
    customSubMask:"255.255.252.0" 
}, 

{ 
    cidr:23, 
    subNumA:32768, 
    subNumB:128, 
    subNumC:0, 
    hostNum:512, 
    blockSize:2, 
    customSubMask:"255.255.254.0" 
}, 

{ 
    cidr:24, 
    subNumA:65536, 
    subNumB:256, 
    subNumC:1, 
    hostNum:256, 
    blockSize:256, 
    customSubMask:"255.255.255.0" 
}, 

{ 
    cidr:25, 
    subNumA:131072, 
    subNumB:512, 
    subNumC:2, 
    hostNum:128, 
    blockSize:128, 
    customSubMask:"255.255.255.128" 
}, 

{ 
    cidr:26, 
    subNumA:262144, 
    subNumB:1024, 
    subNumC:4, 
    hostNum:64, 
    blockSize:64, 
    customSubMask:"255.255.255.192" 
}, 

{ 
    cidr:27, 
    subNumA:524288, 
    subNumB:2048, 
    subNumC:8, 
    hostNum:32, 
    blockSize:32, 
    customSubMask:"255.255.255.224" 
}, 

{ 
    cidr:28, 
    subNumA:1048576, 
    subNumB:4096, 
    subNumC:16, 
    hostNum:16, 
    blockSize:16, 
    customSubMask:"255.255.255.240" 
}, 

{ 
    cidr:29, 
    subNumA:2097152, 
    subNumB:8192, 
    subNumC:32, 
    hostNum:8, 
    blockSize:8, 
    customSubMask:"255.255.255.248" 
}, 

{ 
    cidr:30, 
    subNumA:4194304, 
    subNumB:16384, 
    subNumC:64, 
    hostNum:4, 
    blockSize:4, 
    customSubMask:"255.255.255.252" 
}, 

{ 
    cidr:31, 
    subNumA:8388608, 
    subNumB:32768, 
    subNumC:32, 
    hostNum:2, 
    blockSize:2, 
    customSubMask:"255.255.255.254" 
} 
]; 
var classGiven = prompt("What is the class of your given network address?").toLowerCase(); 
var addressGiven = prompt("What is your given network address?"); 
var userChoice = prompt("What is your given information? ('subnets', 'hosts', 'subnets & hosts', or just a 'network address'?) Type your answer based on what is between the quotes."); 
switch(userChoice) { 
//This section is for showing info based on number of subnets only. 
case 'subnets': 
    var subnetsNeeded = prompt("How many subnets do you need?"); 
    switch(classGiven) { 
    //This is only for class A subnets. 
     case 'a': 
      function getObjectsAsub(subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if (objects[i].subNumA >= subnetsNeeded) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error280"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsAsub(subnetsNeeded); 
      break; 
    //This is only for class B subnets. 
     case 'b': 
      function getObjectsBsub(subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if (objects[i].subNumB >= subnetsNeeded) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error298"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsBsub(subnetsNeeded); 
      break; 
    //This is only for class C subnets. 
     case 'c': 
      function getObjectsCsub(subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if (objects[i].subNumC >= subnetsNeeded) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error316"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsCsub(subnetsNeeded); 
      break; 
    //This is the default case. 
     default: 
      console.log("Something went wrong! error327"); 
      break; 
    } 
    break; 
//This section is for showing info based on number of hosts only. 
case 'hosts': 
    var hostsNeeded = parseInt(prompt("How many hosts do you need?")); 
    var exactHostsNeeded = hostsNeeded + 2; 
    function getObjectsHost(exactHostsNeeded) { 
     var obj, i, il; 
     for (i = 0, il = objects.length; i < il; i++) { 
      if (objects[i].hostNum >= exactHostsNeeded) { 
       obj = objects[i]; 
       break; 
      } else { 
       console.log("Something went wrong! error342"); 
       break; 
      } 
     } 
     var objectArrayTwo = obj; 
     console.log(obj); 
    } 
    getObjectsHost(exactHostsNeeded); 
    break; 
//This section is for showing info based on number of subnets and hosts only. 
case 'subnets & hosts': 
    var subnetsNeeded = parseInt(prompt("How many subnets do you need?")); 
    var hostsNeeded = parseInt(prompt("How many hosts do you need?")); 
    var exactHostsNeeded = hostsNeeded + 2; 
    switch(classGiven) { 
    //This is only for class A subnets. 
     case 'a': 
      function getObjectsA(exactHostsNeeded, subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if ((objects[i].hostNum >= exactHostsNeeded) && (objects[i].subNumA >= subnetsNeeded)) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error366"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsA(exactHostsNeeded, subnetsNeeded); 
      break; 
    //This is only for class B subnets. 
     case 'b': 
      function getObjectsB(exactHostsNeeded, subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if ((objects[i].hostNum >= exactHostsNeeded) && (objects[i].subNumB >= subnetsNeeded)) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error384"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsB(exactHostsNeeded, subnetsNeeded); 
      break; 
    //This is only for class C subnets. 
     case 'c': 
      function getObjectsC(hexactHostsNeeded, subnetsNeeded) { 
       var obj, i, il; 
       for (i = 0, il = objects.length; i < il; i++) { 
        if ((objects[i].hostNum >= exactHostsNeeded) && (objects[i].subNumC >= subnetsNeeded)) { 
         obj = objects[i]; 
         break; 
        } else { 
         console.log("Something went wrong! error402"); 
         break; 
        } 
       } 
       var objectArrayTwo = obj; 
       console.log(obj); 
      } 
      getObjectsC(exactHostsNeeded, subnetsNeeded); 
      break; 
    //This is the default case. 
     default: 
      console.log("Something went wrong! error413"); 
      break; 
    } 
    break; 
//This section is the default for the main switch! 
default: 
    console.log("Something went wrong! error419"); 
    break; 
} 
+0

第一指针,永远无法定义内部开关罩的功能。定义外部并在交换机中调用它。我还看到像getObjectsAsub,getObjectsBsub这样的函数。它们可以像'getObjectsSub(className)'一样处理。同样,对于'getObjectsA'也是如此。 – Rajesh

+0

您能否为我们提供一些输入,然后为这些输入预期输出? –

+0

@GlenKeane“子网和主机”的一个输入场景是C类。我需要大约4个主机和32个子网。它应该返回来自CIDR29的所有信息,因为这是最合适的。 (我们至少需要4台主机,但可用主机的数量是hosts-2,所以总共8台主机 - 2个会给我们6个,这还是足够的,所以它可以工作。) –

回答

0

我会重申我的意见,定义外面的函数,并在switch case中调用它们。

case 'b': 
    function getObjectsBsub(subnetsNeeded) { 
     var obj, i, il; 
     for (i = 0, il = objects.length; i < il; i++) { 
      if (objects[i].subNumB >= subnetsNeeded) { 
       obj = objects[i]; 
       break; 
      } 
      else { 
       console.log("Something went wrong! error298"); 
       break; 
      } 
     } 
     var objectArrayTwo = obj; 
     console.log(obj); 
     } 
     getObjectsBsub(subnetsNeeded); 
    break; 

其次,如果你看你条件下,

 for (i = 0, il = objects.length; i < il; i++) { 
      if (objects[i].subNumB >= subnetsNeeded) { 
       obj = objects[i]; 
       break; 
      } 
      else { 
       console.log("Something went wrong! error298"); 
       break; // This is why only first object is selected 
      } 
     } 

此外,我冒昧地修改函数按我的理解,

function getObjectsSub(classType, subnetsNeeded) { 
    var obj; 

    objects.forEach(function(row){ 
     if(row["subNum" + classType] >= subnetsNeeded){ 
      obj = row; 
      return; 
     } 
    }); 

    if(!obj){ 
     console.log("Something went wrong! error280"); 
    } 

    var objectArrayTwo = obj; 
    console.log(obj); 
}