基于其属性在数组中搜索对象的问题。 (在JavaScript中)

时间:2015-11-08 05:28:23

标签: javascript debugging

我一直在设计一个程序,以帮助使子网网络的任务变得更加容易。现在,支持要做的就是搜索数组并拉出与用户定义的属性相匹配的对象。目前,我遇到的问题是,无论用户选择什么,都只选择数组中的第一个对象。

注意:此外,每当我为“子网”输入大于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;
}

1 个答案:

答案 0 :(得分:0)

我会重申我的评论,在外面定义函数并在切换案例中调用它们。

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);
}