/    Sign up×
Community /Pin to ProfileBookmark

Smallest Value in an Array

Hello,

I am new to the forum and JavaScript. I am really hoping that someone on here will be able to help me. I am trying to code the following:

Find the smallest value in an array that is greater than a given number. So I need to be able to choose the number (lets say it 5) and the numbers in the array were [1, 6, 12, 14]. The lowest number would be 6. Anyone know what the code would be for something like this?

Regards,

Fred Williams

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@007JulienDec 14.2011 — This could be the response for arrays of numbers..
[CODE]<script type="text/javascript">
Array.prototype.minGreaterThan=function(a){var t=this,r=Number.POSITIVE_INFINITY,i;
for (i=0;i<t.length;i++) if (a<t[i] && t[i]<r) r=t[i];
return r;
}

var arr=[1,6,12,14],n=5;
alert(arr.minGreaterThan(n)); // give 6

</script>[/CODE]
Copy linkTweet thisAlerts:
@bybonlineauthorDec 14.2011 — Thank you so much! That was exactly what I was looking for.

Thanks,

Fred Williams
Copy linkTweet thisAlerts:
@007JulienDec 14.2011 — More exactly, it would be advisable to return r, if r isFinite, and null otherwise...
[CODE]Array.prototype.minGreaterThan=function(a){var t=this,r=Number.POSITIVE_INFINITY,i;
for (i=0;i<t.length;i++) if (a<t[i] && t[i]<r) r=t[i];
if (isFinite(r)) return r;
return null;
}
[/CODE]
Copy linkTweet thisAlerts:
@rnd_meDec 14.2011 — Find the smallest value in an array that is greater than a given number. So I need to be able to choose the number (lets say it 5) and the numbers in the array were [1, 6, 12, 14]. The lowest number would be 6. Anyone know what the code would be for something like this?
[/QUOTE]


[CODE]
var min=5;
var r=[1, 6, 12, 14];
function gt(a){return a>this; }

alert(
Math.min.apply(0,
r.filter(gt, min)
)
); [/CODE]
Copy linkTweet thisAlerts:
@JunkMaleDec 15.2011 — [CODE]
var min=5;
var r=[1, 6, 12, 14];
function gt(a){return a>this; }

alert(
Math.min.apply(0,
r.filter(gt, min)
)
); [/CODE]
[/QUOTE]


Wooo neat!

I just prototyped it in case anyone wants to play with that

[CODE]Array.prototype.getLowestAbove = function(a){
return Math.min.apply(0,this.filter( function (a){ return a>this;}, a)) || 0;
}[/CODE]
Copy linkTweet thisAlerts:
@bybonlineauthorDec 15.2011 — Would I want to write this code differently if I am wanting to use it as method within a library? Also, what does this "prototype" due exactly?

Thanks!

Fred Williams
Copy linkTweet thisAlerts:
@JMRKERDec 15.2011 — Wooo neat!

I just prototyped it in case anyone wants to play with that

[CODE]Array.prototype.getLowestAbove = function(a){
return Math.min.apply(0,this.filter( function (a){ return a>this;}, a)) || 0;
}[/CODE]
[/QUOTE]


Works pretty good until value is larger than array limit.

I don't know if there is a valid number here,

sort of like the square root of -1,

but I would think it should not return 'Infinity"
<i>
</i>&lt;script type="text/javascript"&gt;
Array.prototype.getLowestAbove = function(a){
return Math.min.apply(0,this.filter( function (a){ return a&gt;this;}, a)) || 0;
}

var r=[1, 6, 12, 14];
alert(r.getLowestAbove(-15));
alert(r.getLowestAbove(0));
alert(r.getLowestAbove(5));
alert(r.getLowestAbove(6));

alert(r.getLowestAbove(15)); // works pretty good unless above highest number

&lt;/script&gt;
Copy linkTweet thisAlerts:
@bybonlineauthorDec 15.2011 — How would you code this if you were to lay it into a function like this:

var array = function () {};

Thanks,

Fred Williams
Copy linkTweet thisAlerts:
@007JulienDec 15.2011 — Array.filter do not works with IE8. It is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script.

[CODE]if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
[/CODE]
×

Success!

Help @bybonline spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...