function returns NaN when the index of arrays are defined through a loop












-1















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());












share|improve this question

























  • 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 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


















-1















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());












share|improve this question

























  • 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 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
















-1












-1








-1








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());












share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:35









JLRishe

75.9k1077117




75.9k1077117










asked Nov 21 '18 at 10:25









ShinjiShinji

335




335













  • 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 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





















  • 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 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



















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














4 Answers
4






active

oldest

votes


















2














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++)






share|improve this answer































    1














    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]];
    }





    share|improve this answer































      0














      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.






      share|improve this answer


























      • 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



















      0














      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());








      share|improve this answer


























      • 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











      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      2














      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++)






      share|improve this answer




























        2














        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++)






        share|improve this answer


























          2












          2








          2







          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++)






          share|improve this answer













          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++)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 10:42









          Robin ZigmondRobin Zigmond

          1,8241611




          1,8241611

























              1














              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]];
              }





              share|improve this answer




























                1














                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]];
                }





                share|improve this answer


























                  1












                  1








                  1







                  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]];
                  }





                  share|improve this answer













                  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]];
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 10:42









                  PrabhakaranPrabhakaran

                  390214




                  390214























                      0














                      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.






                      share|improve this answer


























                      • 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
















                      0














                      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.






                      share|improve this answer


























                      • 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














                      0












                      0








                      0







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 21 '18 at 10:36

























                      answered Nov 21 '18 at 10:32









                      Suhas NMSuhas 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



















                      • 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











                      0














                      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());








                      share|improve this answer


























                      • 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
















                      0














                      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());








                      share|improve this answer


























                      • 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














                      0












                      0








                      0







                      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());








                      share|improve this answer















                      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());






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 21 '18 at 10:38

























                      answered Nov 21 '18 at 10:29









                      OneJeetOneJeet

                      962310




                      962310













                      • 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











                      • 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


















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?