admin管理员组文章数量:1406943
I'm trying to get the value for row and col with the following function:
function find_winning_position(value, row, col) {
var i, j, chcount, emptycol, emptyrow;
chcount = 0;
emptycol = -1;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (document.getElementById(elementArray[i][j]).innerHTML == value) {
chcount++;
}
else if (document.getElementById(elementArray[i][j]).innerHTML.length == 0) {
emptycol = j;
}
}
if (chcount == 2 && emptycol != -1) {
row = i;
col = emptycol;
return row;
return col;
}
}
}
how to get it? what am doing wrong here... can anyone explain?
var row = -1,
col = -1;
find_winning_position("O", row, col);
alert("row value" + row);
alert("col value" + col);
if (row != -1 && col != -1) {
//do some thing here
}
What am I doing wrong here?
I'm trying to get the value for row and col with the following function:
function find_winning_position(value, row, col) {
var i, j, chcount, emptycol, emptyrow;
chcount = 0;
emptycol = -1;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (document.getElementById(elementArray[i][j]).innerHTML == value) {
chcount++;
}
else if (document.getElementById(elementArray[i][j]).innerHTML.length == 0) {
emptycol = j;
}
}
if (chcount == 2 && emptycol != -1) {
row = i;
col = emptycol;
return row;
return col;
}
}
}
how to get it? what am doing wrong here... can anyone explain?
var row = -1,
col = -1;
find_winning_position("O", row, col);
alert("row value" + row);
alert("col value" + col);
if (row != -1 && col != -1) {
//do some thing here
}
What am I doing wrong here?
Share Improve this question edited Mar 29, 2012 at 12:52 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Mar 29, 2012 at 12:50 ssss05ssss05 7177 gold badges14 silver badges27 bronze badges 6- 2 Can you spend more time formatting your question next time please? – Matt Commented Mar 29, 2012 at 12:52
- a function can return only one value. – Pranav Commented Mar 29, 2012 at 12:53
- @pranav : yeah i tried with only return; too – ssss05 Commented Mar 29, 2012 at 12:54
-
Is
if (chcount == 2 && emptycol != -1) {
true? Put a breakpoint in and check. You don't have any returns except inside this block. – asawyer Commented Mar 29, 2012 at 12:57 - @asawyer : i checked.. then only i posted this question here... – ssss05 Commented Mar 29, 2012 at 13:08
4 Answers
Reset to default 3The first problem is that you are trying to return
when you have already return
ed.
The second problem is that you don't assign the return value of the function to anything.
return { "row": row, "col": col };
and
var position = find_winning_position("O", row, col);
row = position.row;
col = position.col;
You also need to cope with the situation where the function doesn't return anything (since all your returns are inside an if statement).
you can only return one value... this does not make sense:
return row; return col;
your usage example actually does not expect returned values, it expects the function to change the values it created in the global scope.
the function actually does not change global values because it is being passed variables with same names and works with the local ones.
what you can do:
a. do not pass row
and col
to the function, and instead it will work with global variables. and then no need to return anything from it.
b. return an object with two properties...
return { row: row, col: col };
Execution ends when you reach a return, so the second return is unreachable.
return row;
return col; // will never hit this line
To return both, I'd probably wrap them together like this:
return {
Row: row,
Col: col
};
You also don't have a return statement to return anything if that if(...)
block doesn't evaluate to true, that could be why your not seeing anything back at all.
edit - your call site ignores returned values, and you are depending on mutating the arguments. While it may or may not work, it's a practice I try very hard to avoid without explicitly saying in the function name that the function will be mutating the state at the call site.
I would change the function to return an object like this:
return {
Row: i,
Col: emptycol
};
And change the call site to this:
var row = -1,
col = -1;
var result = find_winning_position("O", row, col);
alert("row value" + result.Row);
alert("col value" + result.Col);
if (result.Row != -1 && result.Col != -1) {
//do some thing here
}
SA function can return only one value :-
if you want to return row and col then you can simply concat both and return and on the other
side you can split that and get both values.
return row+"-"+col;
other end split it :-
var obj=find_winning_position("O", row, col);
var spl=obj.Split('-');
var newrow=spl[0];
var newcol=obj[1];
本文标签: Javascript function not returning valuesStack Overflow
版权声明:本文标题:Javascript function not returning values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744967961a2635064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论