[英]Returning multiple values in javascript function
I'm trying to match test scores with values contained in the level array and to return selected values in an output array, But, when running the below script in the Firebug console I get the error message "TypeError: level is undefined". 我试图将考试成绩与级别数组中包含的值进行匹配,并在输出数组中返回选定的值,但是,在Firebug控制台中运行以下脚本时,出现错误消息“ TypeError:级别未定义”。 I don't understand this, the level seems well defined to me, maybe the error lies elsewhere.
我不明白这一点,对我来说,水平似乎定义不错,也许错误出在其他地方。
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);
Any comments or help would be much appreciated. 任何意见或帮助将不胜感激。 Thanks
谢谢
The error message is actually: 错误消息实际上是:
ReferenceError: levell is not defined
You made a typo and added an extra l
to one of your variables. 您输入了错字,并向其中一个变量添加了额外的
l
。
You also made another error: 您还犯了另一个错误:
var profic = newProfic();
Here you call newProfic
and the second argument is unspecified so it is undefined
. 在这里,您调用
newProfic
,第二个参数未指定,因此undefined
。
var newProfic = function profLevel(x, level) {
The second argument is level
therefore level
is undefined
. 第二个参数是
level
因此level
undefined
。
I suggest to use Array#filter
and then Array#map
to get the wanted elements. 我建议使用
Array#filter
,然后使用Array#map
来获取所需的元素。
This proposal expects zero, one or more than one result set. 该提案预期结果为零,一个或多个。
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; }
This proposal expects only one result set 该提案仅预期一个结果集
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.