在javascript函数

时间:2016-11-01 09:09:30

标签: javascript arrays

我试图将测试分数与级别数组中包含的值进行匹配,并在输出数组中返回选定的值,但是,当在Firebug控制台中运行以下脚本时,我收到错误消息" TypeError:level未定义"。我不明白这一点,这个级别对我来说似乎很明确,也许错误在于其他地方。

var level =  [[905,990,"91% - 100%","International Professional Proficiency","Able to communicate effectively in any situation"],
    [785,900,"79% - 90%","Working Proficiency Plus","Able to satisfy most work requirements with language that is often, but not always, acceptable and effective"],
    [605,780,"61% - 78%","Limited Working Proficiency","Able to satisfy most social demands and limited work requirements "],
    [405,600,"41% - 60%","Elementary Proficiency Plus","Can initiate and maintain predictable face-to-face conversations and satisfy limited social demands"],
    [255,400,"26% - 40%","Elementary Proficiency","Speaker has functional, but limited proficiency. Able to maintain very simple face-to-face conversations on familiar topics"],
    [10,50, "0 - 25%","Basic Proficiency","Able to satisfy immediate survival needs"]];

function between(x, min, max) {                     
  return x >= min && x <= max;
}

var newProfic =  function profLevel(x, level) {
    var prof1, prof2, prof3;
    for(var i=0; i<level.length; i++) {
      if( between(x, level[i][0], level[i][1])) {
        prof1 = levell[i][2];
        prof2 = level[i][3];
        prof3 = level[i][4];
      }     
    }
    return  [ prof1,   prof2,  prof3 ];
  }

  var profic = newProfic();
  var prof1 = profic[0];
  var prof2 = profic[1]; 
  var prof3 = profic[2];  
  newProfic( 300,  level);

任何评论或帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:0)

错误信息实际上是:

ReferenceError: levell is not defined

您输入了一个拼写错误,并在其中一个变量中添加了额外的l

您还犯了另一个错误:

var profic = newProfic();

在这里你调用newProfic,第二个参数未指定,因此它是undefined

var newProfic =  function profLevel(x, level) {

第二个参数是level因此levelundefined

答案 1 :(得分:0)

我建议使用Array#filter然后Array#map来获取想要的元素。

此提案需要零个,一个或多个结果集。

var level = [[905, 990, "91% - 100%", "International Professional Proficiency", "Able to communicate effectively in any situation"], [785, 900, "79% - 90%", "Working Proficiency Plus", "Able to satisfy most work requirements with language that is often, but not always, acceptable and effective"], [605, 780, "61% - 78%", "Limited Working Proficiency", "Able to satisfy most social demands and limited work requirements "], [405, 600, "41% - 60%", "Elementary Proficiency Plus", "Can initiate and maintain predictable face-to-face conversations and satisfy limited social demands"], [255, 400, "26% - 40%", "Elementary Proficiency", "Speaker has functional, but limited proficiency. Able to maintain very simple face-to-face conversations on familiar topics"], [10, 50, "0 - 25%", "Basic Proficiency", "Able to satisfy immediate survival needs"]];

function between(x, min, max) {
    return x >= min && x <= max;
}

function profLevel(x, level) {
    return level.filter(function (a) {
        return between(x, a[0], a[1]);
    }).map(function (a) {
        return [a[2], a[3], a[4]];
    });
}

console.log(profLevel(300, level));
.as-console-wrapper { max-height: 100% !important; top: 0; }

此提案只需要一个结果集

var level = [[905, 990, "91% - 100%", "International Professional Proficiency", "Able to communicate effectively in any situation"], [785, 900, "79% - 90%", "Working Proficiency Plus", "Able to satisfy most work requirements with language that is often, but not always, acceptable and effective"], [605, 780, "61% - 78%", "Limited Working Proficiency", "Able to satisfy most social demands and limited work requirements "], [405, 600, "41% - 60%", "Elementary Proficiency Plus", "Can initiate and maintain predictable face-to-face conversations and satisfy limited social demands"], [255, 400, "26% - 40%", "Elementary Proficiency", "Speaker has functional, but limited proficiency. Able to maintain very simple face-to-face conversations on familiar topics"], [10, 50, "0 - 25%", "Basic Proficiency", "Able to satisfy immediate survival needs"]];

function between(x, min, max) {
    return x >= min && x <= max;
}

function profLevel(x, level) {
    var index = -1;
    level.some(function (a, i) {
        if (between(x, a[0], a[1])) {
            index = i;
            return true;
        }
    });
    if (index >= 0) {
        return [level[index][2], level[index][3], level[index][4]];
    }
}

console.log(profLevel(300, level));
.as-console-wrapper { max-height: 100% !important; top: 0; }