function returns NaN when the index of arrays are defined through a loop
I'm doing a project, and this function returns NaN
. The last time I had this problem, I fixed it by defining a variable that was equal to 0. However it doesn't work.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
javascript arrays function
|
show 2 more comments
I'm doing a project, and this function returns NaN
. The last time I had this problem, I fixed it by defining a variable that was equal to 0. However it doesn't work.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
javascript arrays function
reiserute.length
is an empty array.what s the expected output?
– brk
Nov 21 '18 at 10:27
@brk for examplevar reiserute = [0, 1];
then the expected output would be 62.3
– Shinji
Nov 21 '18 at 10:28
1
Your function currently returns 0, becausereiserute
is empty.
– JLRishe
Nov 21 '18 at 10:29
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
2
While you can't see the issue whenreiserute
is empty - because the loop won't even run - you are going to getNaN
whenever it isn't empty. This is because you are accessingreiserute[i + 1]
even wheni
isreiserute.length - 1
, which is going to beundefined
. Looks like you should stop your loop one interation earlier.
– Robin Zigmond
Nov 21 '18 at 10:34
|
show 2 more comments
I'm doing a project, and this function returns NaN
. The last time I had this problem, I fixed it by defining a variable that was equal to 0. However it doesn't work.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
javascript arrays function
I'm doing a project, and this function returns NaN
. The last time I had this problem, I fixed it by defining a variable that was equal to 0. However it doesn't work.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0, 1];
function ruteFunction() {
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z += avstandArray[reiserute[i]][reiserute[i + 1]];
}
return z;
}
console.log(ruteFunction());
javascript arrays function
javascript arrays function
edited Nov 21 '18 at 10:35
JLRishe
75.5k1077116
75.5k1077116
asked Nov 21 '18 at 10:25
Shinji
295
295
reiserute.length
is an empty array.what s the expected output?
– brk
Nov 21 '18 at 10:27
@brk for examplevar reiserute = [0, 1];
then the expected output would be 62.3
– Shinji
Nov 21 '18 at 10:28
1
Your function currently returns 0, becausereiserute
is empty.
– JLRishe
Nov 21 '18 at 10:29
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
2
While you can't see the issue whenreiserute
is empty - because the loop won't even run - you are going to getNaN
whenever it isn't empty. This is because you are accessingreiserute[i + 1]
even wheni
isreiserute.length - 1
, which is going to beundefined
. Looks like you should stop your loop one interation earlier.
– Robin Zigmond
Nov 21 '18 at 10:34
|
show 2 more comments
reiserute.length
is an empty array.what s the expected output?
– brk
Nov 21 '18 at 10:27
@brk for examplevar reiserute = [0, 1];
then the expected output would be 62.3
– Shinji
Nov 21 '18 at 10:28
1
Your function currently returns 0, becausereiserute
is empty.
– JLRishe
Nov 21 '18 at 10:29
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
2
While you can't see the issue whenreiserute
is empty - because the loop won't even run - you are going to getNaN
whenever it isn't empty. This is because you are accessingreiserute[i + 1]
even wheni
isreiserute.length - 1
, which is going to beundefined
. Looks like you should stop your loop one interation earlier.
– Robin Zigmond
Nov 21 '18 at 10:34
reiserute.length
is an empty array.what s the expected output?– brk
Nov 21 '18 at 10:27
reiserute.length
is an empty array.what s the expected output?– brk
Nov 21 '18 at 10:27
@brk for example
var reiserute = [0, 1];
then the expected output would be 62.3– Shinji
Nov 21 '18 at 10:28
@brk for example
var reiserute = [0, 1];
then the expected output would be 62.3– Shinji
Nov 21 '18 at 10:28
1
1
Your function currently returns 0, because
reiserute
is empty.– JLRishe
Nov 21 '18 at 10:29
Your function currently returns 0, because
reiserute
is empty.– JLRishe
Nov 21 '18 at 10:29
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
2
2
While you can't see the issue when
reiserute
is empty - because the loop won't even run - you are going to get NaN
whenever it isn't empty. This is because you are accessing reiserute[i + 1]
even when i
is reiserute.length - 1
, which is going to be undefined
. Looks like you should stop your loop one interation earlier.– Robin Zigmond
Nov 21 '18 at 10:34
While you can't see the issue when
reiserute
is empty - because the loop won't even run - you are going to get NaN
whenever it isn't empty. This is because you are accessing reiserute[i + 1]
even when i
is reiserute.length - 1
, which is going to be undefined
. Looks like you should stop your loop one interation earlier.– Robin Zigmond
Nov 21 '18 at 10:34
|
show 2 more comments
4 Answers
4
active
oldest
votes
The head of your loop is as follows:
for (var i = 0; i < reiserute.length; i++)
This means that i
will vary from 0
up to reiserute.length - 1
, inclusive.
Then, in your loop body, you do this:
z += avstandArray[reiserute[i]][reiserute[i + 1]];
Note in particular the reiserute[i + 1]
. Since i
is running from 0
to reiserute.length - 1
, i + 1
will range from 1
to reiserute.length
. So on the last iteration you are trying to access reiserute[reiserute.length]
. And this will be undefined
- you are trying to access an index "one beyond" the end of the array.
So you are then evaluating
avstandArray[reiserute[i]][undefined]
which is itself undefined
. Then +=
forcibly coerces this to a Number
, which is where your NaN
comes from.
While I'm not sure what the exact intention behind your code is, the easiest fix would be to change the header of your loop to not access elements beyond the last in the array. That is, do this instead:
for (var i = 0; i < reiserute.length - 1; i++)
add a comment |
The problem is that
z+= avstandArray[reiserute[i]][reiserute[i+1]];
the above code will try to get the value from reiserute array at n+1th element which is undefined and adding the undefined element to number will throw NaN exception. So Change the logic accordingly.
for (var i = 0; i < reiserute.length; i++) {
if(i!=reiserute.length)
z+= avstandArray[reiserute[i]][reiserute[i+1]];
else
z+= avstandArray[reiserute[i]];
}
add a comment |
Not sure what you are trying to achieve here, although, this is the issue:
your "reiserute" variable has no value in it, it is an empty array and you are trying to access value at different indexes, since there is no data available, javascript will return "undefined".
reiserute[i] ----> undefined
now you are trying to find a value passing reiserute[i], which essentially is undefined.
when you try to access a value of an array using an index, javascript expects your index to be an integer, since your index was evaluated to undefined, it threw NaN error - Nan stands for "NotANumber".
now, you are getting your output as "0" because, length of the array is zero and condition is failing in the first check, hence the loop is not executed.
If your loop executes, you will receive NaN as an error.
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
add a comment |
This is returning 0 without any issues. As you defined var reiserute = ;
the for loop not even running for once.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409994%2ffunction-returns-nan-when-the-index-of-arrays-are-defined-through-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The head of your loop is as follows:
for (var i = 0; i < reiserute.length; i++)
This means that i
will vary from 0
up to reiserute.length - 1
, inclusive.
Then, in your loop body, you do this:
z += avstandArray[reiserute[i]][reiserute[i + 1]];
Note in particular the reiserute[i + 1]
. Since i
is running from 0
to reiserute.length - 1
, i + 1
will range from 1
to reiserute.length
. So on the last iteration you are trying to access reiserute[reiserute.length]
. And this will be undefined
- you are trying to access an index "one beyond" the end of the array.
So you are then evaluating
avstandArray[reiserute[i]][undefined]
which is itself undefined
. Then +=
forcibly coerces this to a Number
, which is where your NaN
comes from.
While I'm not sure what the exact intention behind your code is, the easiest fix would be to change the header of your loop to not access elements beyond the last in the array. That is, do this instead:
for (var i = 0; i < reiserute.length - 1; i++)
add a comment |
The head of your loop is as follows:
for (var i = 0; i < reiserute.length; i++)
This means that i
will vary from 0
up to reiserute.length - 1
, inclusive.
Then, in your loop body, you do this:
z += avstandArray[reiserute[i]][reiserute[i + 1]];
Note in particular the reiserute[i + 1]
. Since i
is running from 0
to reiserute.length - 1
, i + 1
will range from 1
to reiserute.length
. So on the last iteration you are trying to access reiserute[reiserute.length]
. And this will be undefined
- you are trying to access an index "one beyond" the end of the array.
So you are then evaluating
avstandArray[reiserute[i]][undefined]
which is itself undefined
. Then +=
forcibly coerces this to a Number
, which is where your NaN
comes from.
While I'm not sure what the exact intention behind your code is, the easiest fix would be to change the header of your loop to not access elements beyond the last in the array. That is, do this instead:
for (var i = 0; i < reiserute.length - 1; i++)
add a comment |
The head of your loop is as follows:
for (var i = 0; i < reiserute.length; i++)
This means that i
will vary from 0
up to reiserute.length - 1
, inclusive.
Then, in your loop body, you do this:
z += avstandArray[reiserute[i]][reiserute[i + 1]];
Note in particular the reiserute[i + 1]
. Since i
is running from 0
to reiserute.length - 1
, i + 1
will range from 1
to reiserute.length
. So on the last iteration you are trying to access reiserute[reiserute.length]
. And this will be undefined
- you are trying to access an index "one beyond" the end of the array.
So you are then evaluating
avstandArray[reiserute[i]][undefined]
which is itself undefined
. Then +=
forcibly coerces this to a Number
, which is where your NaN
comes from.
While I'm not sure what the exact intention behind your code is, the easiest fix would be to change the header of your loop to not access elements beyond the last in the array. That is, do this instead:
for (var i = 0; i < reiserute.length - 1; i++)
The head of your loop is as follows:
for (var i = 0; i < reiserute.length; i++)
This means that i
will vary from 0
up to reiserute.length - 1
, inclusive.
Then, in your loop body, you do this:
z += avstandArray[reiserute[i]][reiserute[i + 1]];
Note in particular the reiserute[i + 1]
. Since i
is running from 0
to reiserute.length - 1
, i + 1
will range from 1
to reiserute.length
. So on the last iteration you are trying to access reiserute[reiserute.length]
. And this will be undefined
- you are trying to access an index "one beyond" the end of the array.
So you are then evaluating
avstandArray[reiserute[i]][undefined]
which is itself undefined
. Then +=
forcibly coerces this to a Number
, which is where your NaN
comes from.
While I'm not sure what the exact intention behind your code is, the easiest fix would be to change the header of your loop to not access elements beyond the last in the array. That is, do this instead:
for (var i = 0; i < reiserute.length - 1; i++)
answered Nov 21 '18 at 10:42
Robin Zigmond
1,7691410
1,7691410
add a comment |
add a comment |
The problem is that
z+= avstandArray[reiserute[i]][reiserute[i+1]];
the above code will try to get the value from reiserute array at n+1th element which is undefined and adding the undefined element to number will throw NaN exception. So Change the logic accordingly.
for (var i = 0; i < reiserute.length; i++) {
if(i!=reiserute.length)
z+= avstandArray[reiserute[i]][reiserute[i+1]];
else
z+= avstandArray[reiserute[i]];
}
add a comment |
The problem is that
z+= avstandArray[reiserute[i]][reiserute[i+1]];
the above code will try to get the value from reiserute array at n+1th element which is undefined and adding the undefined element to number will throw NaN exception. So Change the logic accordingly.
for (var i = 0; i < reiserute.length; i++) {
if(i!=reiserute.length)
z+= avstandArray[reiserute[i]][reiserute[i+1]];
else
z+= avstandArray[reiserute[i]];
}
add a comment |
The problem is that
z+= avstandArray[reiserute[i]][reiserute[i+1]];
the above code will try to get the value from reiserute array at n+1th element which is undefined and adding the undefined element to number will throw NaN exception. So Change the logic accordingly.
for (var i = 0; i < reiserute.length; i++) {
if(i!=reiserute.length)
z+= avstandArray[reiserute[i]][reiserute[i+1]];
else
z+= avstandArray[reiserute[i]];
}
The problem is that
z+= avstandArray[reiserute[i]][reiserute[i+1]];
the above code will try to get the value from reiserute array at n+1th element which is undefined and adding the undefined element to number will throw NaN exception. So Change the logic accordingly.
for (var i = 0; i < reiserute.length; i++) {
if(i!=reiserute.length)
z+= avstandArray[reiserute[i]][reiserute[i+1]];
else
z+= avstandArray[reiserute[i]];
}
answered Nov 21 '18 at 10:42
Prabhakaran
368113
368113
add a comment |
add a comment |
Not sure what you are trying to achieve here, although, this is the issue:
your "reiserute" variable has no value in it, it is an empty array and you are trying to access value at different indexes, since there is no data available, javascript will return "undefined".
reiserute[i] ----> undefined
now you are trying to find a value passing reiserute[i], which essentially is undefined.
when you try to access a value of an array using an index, javascript expects your index to be an integer, since your index was evaluated to undefined, it threw NaN error - Nan stands for "NotANumber".
now, you are getting your output as "0" because, length of the array is zero and condition is failing in the first check, hence the loop is not executed.
If your loop executes, you will receive NaN as an error.
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
add a comment |
Not sure what you are trying to achieve here, although, this is the issue:
your "reiserute" variable has no value in it, it is an empty array and you are trying to access value at different indexes, since there is no data available, javascript will return "undefined".
reiserute[i] ----> undefined
now you are trying to find a value passing reiserute[i], which essentially is undefined.
when you try to access a value of an array using an index, javascript expects your index to be an integer, since your index was evaluated to undefined, it threw NaN error - Nan stands for "NotANumber".
now, you are getting your output as "0" because, length of the array is zero and condition is failing in the first check, hence the loop is not executed.
If your loop executes, you will receive NaN as an error.
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
add a comment |
Not sure what you are trying to achieve here, although, this is the issue:
your "reiserute" variable has no value in it, it is an empty array and you are trying to access value at different indexes, since there is no data available, javascript will return "undefined".
reiserute[i] ----> undefined
now you are trying to find a value passing reiserute[i], which essentially is undefined.
when you try to access a value of an array using an index, javascript expects your index to be an integer, since your index was evaluated to undefined, it threw NaN error - Nan stands for "NotANumber".
now, you are getting your output as "0" because, length of the array is zero and condition is failing in the first check, hence the loop is not executed.
If your loop executes, you will receive NaN as an error.
Not sure what you are trying to achieve here, although, this is the issue:
your "reiserute" variable has no value in it, it is an empty array and you are trying to access value at different indexes, since there is no data available, javascript will return "undefined".
reiserute[i] ----> undefined
now you are trying to find a value passing reiserute[i], which essentially is undefined.
when you try to access a value of an array using an index, javascript expects your index to be an integer, since your index was evaluated to undefined, it threw NaN error - Nan stands for "NotANumber".
now, you are getting your output as "0" because, length of the array is zero and condition is failing in the first check, hence the loop is not executed.
If your loop executes, you will receive NaN as an error.
edited Nov 21 '18 at 10:36
answered Nov 21 '18 at 10:32
Suhas NM
1175
1175
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
add a comment |
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
the thing is i already defined earlier from some codes i didn't add here since i didn't think it would be necessary.
– Shinji
Nov 21 '18 at 10:34
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
but in this code it is reset to an empty array, all your previous values are lost
– Suhas NM
Nov 21 '18 at 10:37
add a comment |
This is returning 0 without any issues. As you defined var reiserute = ;
the for loop not even running for once.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
add a comment |
This is returning 0 without any issues. As you defined var reiserute = ;
the for loop not even running for once.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
add a comment |
This is returning 0 without any issues. As you defined var reiserute = ;
the for loop not even running for once.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
This is returning 0 without any issues. As you defined var reiserute = ;
the for loop not even running for once.
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
var avstandArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var reiserute = [0,1];
function ruteFunction(){
var z = 0;
for (var i = 0; i < reiserute.length; i++) {
z+= avstandArray[reiserute[i]][reiserute[i+1]];
console.log(z);
}
return z;
}
console.log(ruteFunction());
edited Nov 21 '18 at 10:38
answered Nov 21 '18 at 10:29
OneJeet
952310
952310
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
add a comment |
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
i have defined it earlier. but let's assume the array consists of 0 and 1
– Shinji
Nov 21 '18 at 10:31
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
Edited the code. check... your i+1 causing NAN coz at the second iteration there is no index available.
– OneJeet
Nov 21 '18 at 10:39
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
WHat exactly you want the function to return. please explain and I'll update the solution.
– OneJeet
Nov 21 '18 at 10:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409994%2ffunction-returns-nan-when-the-index-of-arrays-are-defined-through-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
reiserute.length
is an empty array.what s the expected output?– brk
Nov 21 '18 at 10:27
@brk for example
var reiserute = [0, 1];
then the expected output would be 62.3– Shinji
Nov 21 '18 at 10:28
1
Your function currently returns 0, because
reiserute
is empty.– JLRishe
Nov 21 '18 at 10:29
@JLRishe actually i've pushed in some values. but i haven't added the codes to this question since i thought it is not important. i would like to assume the array is for example [0, 1]
– Shinji
Nov 21 '18 at 10:31
2
While you can't see the issue when
reiserute
is empty - because the loop won't even run - you are going to getNaN
whenever it isn't empty. This is because you are accessingreiserute[i + 1]
even wheni
isreiserute.length - 1
, which is going to beundefined
. Looks like you should stop your loop one interation earlier.– Robin Zigmond
Nov 21 '18 at 10:34