【问题标题】:JavaScript function not working for 0 value in arrayJavaScript函数不适用于数组中的0值
【发布时间】:2011-06-07 05:49:14
【问题描述】:

此函数旨在查找变量列表中最高的变量,这些变量有五个 id 字母和一个数字。它适用于所有其他插槽,2、3、4、5、6、7、8、9、10,但不是 1。我需要另一双眼睛。

getVer 函数从 id 中获取数字;所以使用 getVer 的 ImpHt1 将为 1,而 getShtNm 得到 ImpHt。

function find_max_feat(array_input,ShtNm) {
    if (String(array_input[0]).length == 1) {
        var max = 0;
    }
    else {
        var max = getVer(array_input[0]);
    }
    var maxver = 0
    var len = array_input.length;
    for (var i = 0; i < len; i++) {
        if (String(array_input[i]).length > 1) {
            if (getShtNm(String(array_input[i])) == ShtNm) {
                if (getVer(String(array_input[i])) > maxver) {
                    var max = array_input[i];
                    var maxver = getVer(String(array_input[i]));
                }
            }
        }
    }
    return max;
}

0,DmnHt1_0,AltFm1_,0,0,0,0,0,0,0

数组的一个例子,这就是为什么需要getVer。

这是一个表格生成器,需要明确的是,但我已经在整个事情上工作了至少几天,甚至可能是一个星期或几周的上下班时间。

上面的数组是在任何时候选择一个feat时生成的,find_max_feat数组用于查找组中最高的版本;它在无限循环中运行,因为我所做的任何其他事情都无法让它按照我想要的方式工作。

function checkFeats() {
    updateFeatsel();
    t=setTimeout("checkFeats()",1000);
}

function updateFeatsel() {
    curselarray = new Array();
    var selinc = 1;
    while (selinc <= 10) {
        var selincar = selinc - 1;
        var selid = document.getElementById(String('ftlst' + selinc));
        if (getVer(selid.options[selid.selectedIndex].title)) {
                curselarray[selincar] = selid.options[selid.selectedIndex].title;
        }
        else {
            curselarray[selincar] = 0;
        }
        selinc++;
    }
    document.getElementById('debug1').innerHTML = curselarray.valueOf();
    featSelch('hlthm','ImpHt',healthom);
    featSelch('strdmgm','ImpPd',Strpdom);
    featSelch('strwhtm','ImpLi',Strwhtom);
    featSelch('strsltm','EnhIt',StrSltom);
    featSelch('endsurm','ImpEn',EndSurom);
    featSelch('endsokm','ImpDf',EndSokom);
    featSelch('intelmpm','ImpMg',Intelmom);
    featSelch('willsokm','ImpMs',Willsokom);
    featSelch('luckrllm','ImpLu',Lukrllom);
    featSelch('luckpntm','EnhLu',Lukpntom);
    featSelch('hlthbn','DmnHt',0);
    featSelch('strbn','SupSt',0);
    featSelch('luckbn','DmnLu',0);
    featSelch('endbn','Armor',0)
    document.getElementById('debug2').innerHTML = find_max_feat(curselarray,'DmnHt');
    updateAmounts();
}

function featSelch(sid,fshtnm,defval) {
    return document.getElementById(sid).innerHTML = getFeatvalue(fshtnm,defval);
}

【问题讨论】:

    标签: javascript arrays function menu indexing


    【解决方案1】:

    那是因为您使用getVer(array_input[0]) 而不是array_input[0] 来初始化max。如果第一项最高且版本号为零,则使用初始值。

    改变这个:

    var max = getVer(array_input[0]);
    

    进入:

    var max = array_input[0];
    

    【讨论】:

    • 不幸的是,这实际上似乎破坏了其他一切;现在返回的值代替了每个字段值,而不仅仅是它应该修改的值。
    • @Daedalus:我不明白你的意思。什么是回声,在哪里?修改在哪里?代码没有修改任何东西......
    • 函数在另一个中被调用;让我现在得到它;
    • 我希望这能让事情变得明朗起来。让我知道是否需要添加更多内容。
    • @Daedalus:向我们扔越来越多的代码只会让事情变得复杂。 制作一个测试用例.
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2016-01-13
    • 2020-08-31
    • 1970-01-01
    • 2013-05-24
    • 2017-11-22
    相关资源
    最近更新 更多