• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Is their a way to find the closest number to 0 in an Array ?

Contributor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

I've got an array like that

 

var tideArray = new Array(); 
tideArray
.push({tide:"pmm", difference: -2});
tideArray
.push({tide:"pms", difference: 5});
tideArray
.push({tide:"bmm", difference: -7});
tideArray
.push({tide:"bms", difference: 8}); 

tideArray
.sortOn("difference", Array.NUMERIC);
trace
(tideArray[0].tide);

 

For now, it's choosing the minimum number (-7) but I'd like to choose the closest number to 0.

 

Is there a way to do that ?

TOPICS
ActionScript

Views

2.3K

Translate

Translate

Report

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

Enthusiast , Jan 22, 2018 Jan 22, 2018

I don't think sort or sortOn will do what you want. Here's a little method that will return the index of the array element closes tto zero:

var tideArray = new Array();

tideArray.push({tide:"pmm", difference: -2});

tideArray.push({tide:"pms", difference: 5});

tideArray.push({tide:"bmm", difference: -7});

tideArray.push({tide:"bms", difference: 8});

trace(closestToZero(tideArray));

function closestToZero(a:Array):int

{

var curDelta:int = Math.abs(0 - a[0].difference);

var curIndex:int = 0;

for(var i:in

...

Votes

Translate

Translate
Enthusiast ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

I don't think sort or sortOn will do what you want. Here's a little method that will return the index of the array element closes tto zero:

var tideArray = new Array();

tideArray.push({tide:"pmm", difference: -2});

tideArray.push({tide:"pms", difference: 5});

tideArray.push({tide:"bmm", difference: -7});

tideArray.push({tide:"bms", difference: 8});

trace(closestToZero(tideArray));

function closestToZero(a:Array):int

{

var curDelta:int = Math.abs(0 - a[0].difference);

var curIndex:int = 0;

for(var i:int = 1; i < a.length; i++){

var thisDelta:int = Math.abs(0 - a.difference);

if(thisDelta < curDelta){

curIndex = i;

}

}

return curIndex;

}

Votes

Translate

Translate

Report

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
Contributor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

Hmm not sure that is working...

I've tested and it returns "1"

If I change  the values, for example to :

tideArray.push({tide:"pmm", difference: 3});

tideArray.push({tide:"pms", difference: -4});

tideArray.push({tide:"bmm", difference: 10});

tideArray.push({tide:"bms", difference: 8});

trace(closestToZero(tideArray)); //// output 0 (it should output 3)

Also, is it possible to output the name of the array ? (in this example, I'd like to output "pmm" because it's the closest to 0)

Votes

Translate

Translate

Report

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 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

The output is correct - it's the array index not the value...

If you want the value:

var i:int = closestToZero(tideArray);

trace(tideArray.difference);

And if you want name too:

trace(tideArray.name, tideArray.difference);

Votes

Translate

Translate

Report

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
Contributor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

Oh sorry , my bad. I read to quickly ! Thanks !!!

Votes

Translate

Translate

Report

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
Contributor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

Hmm, in fact, I don't know why but sometimes the results are wrong.

I've tried this  :

var tideArray = new Array();
tideArray.push({tide:"haute", difference: "-14"});
tideArray.push({tide:"haute", difference: "-3"});
tideArray.push({tide:"basse", difference: "-9"});
tideArray.push({tide:"basse", difference: "4"});


trace(closestToZero(tideArray));

function closestToZero(a:Array):int  { 
var curDelta:int = Math.abs(0 - a[0].difference); 
var curIndex:int = 0; 

for(var i:int = 1; i < a.length; i++){ 
var thisDelta:int = Math.abs(0 - a.difference); 
if(thisDelta < curDelta){ 
curIndex = i; 



return curIndex; 
}

The output is 3 (so the 4th array that is equal to 4). But, the closest to 0 is not the fourth one, but the second one (-3)..

Do you know why the code make this mistake ?

Votes

Translate

Translate

Report

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 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

LATEST

Yes... not sure how but this line is missing:

curDelta = thisDelta;

that goes inside the if statement where curIndex is set to i - so:

  1. if(thisDelta < curDelta){   
  2. curIndex = i;   
  3. curDelta = thisDelta;
  4. }

Votes

Translate

Translate

Report

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