Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] How do you correctly use the array find method?

Author
Message
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 2nd May 2020 19:51 Edited at: 2nd May 2020 19:53
For some reason, the Array.find() method does not always find a value that exists in the array. Seems success depends on the values searched for and the length of the array. Tested with 2019.12.16 and 2020.04.30 versions


Should show
[65,46,44,25,]
Find 65: 0
Find 46: 1
Find 44: 2
Find 25: 3

but I get
[65,46,44,25,]
Find 65: -1
Find 46: 1
Find 44: -1
Find 25: -1

The author of this post has marked a post as an answer.

Go to answer

Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 2nd May 2020 20:08
This post has been marked by the post author as the answer.
Its not really a bug. The .find() operator only works on sorted arrays. Your array isnt sorted so it doesnt work as expected.

AGK documentation wrote: ".sort() will always sort in ascending order and .find(item) will only work on arrays that are in ascending order. "

JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 2nd May 2020 20:18 Edited at: 2nd May 2020 20:20
As noted by @Bengismo, according to the guides " .find(item) will only work on arrays that are in ascending order ", so you must sort them first to put in ascending order and then use .find.

This works:

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 2nd May 2020 20:47
I've renamed the thread because this is not a bug.
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 2nd May 2020 21:14
Thanks. I totally forgot the array needed to be sorted.

Login to post a reply

Server time is: 2024-11-22 04:41:17
Your offset time is: 2024-11-22 04:41:17