Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Searching array question

Guest
Jan 02, 2013 Jan 02, 2013

Hi

I'm using the following to search for the index of a nested array within the array 'folders', that contains an object whose id matches a certain value.  The objects being searched are at position 0 of each nested array.

function find(id):int

    for (var i:int = 0; i < folders.length; i++)

    {       

        if(folders[0].id == id) return i;

    }

}

find("my_value");

...gives me the error: function does not return a value

But if I get rid of the :int...

function find(id)

...it works.

It's definitely an integer returning, as I'm able to use it mathemtaically later.

Why does this happen?

Thanks

TOPICS
ActionScript
817
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 03, 2013 Jan 03, 2013

Since the return of the i value is conditional it is possible the function will fail to return a value.  You should probably rearrange things so that the return happens outside of the loop, where in the case of no value matching you return a -1 or some other value that could not be related to an array index.

Translate
LEGEND ,
Jan 03, 2013 Jan 03, 2013

Since the return of the i value is conditional it is possible the function will fail to return a value.  You should probably rearrange things so that the return happens outside of the loop, where in the case of no value matching you return a -1 or some other value that could not be related to an array index.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 03, 2013 Jan 03, 2013

if  a function return a value then the function ever need return a value with condition or not

function find(id):int

var idx:int=-1;

    for (var i:int = 0; i < folders.length; i++)

    {       

        if(folders[0].id == id) idx=i;

    }

     return idx;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 04, 2013 Jan 04, 2013

Thank you guys - these answers both helped!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 04, 2013 Jan 04, 2013
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines