/    Sign up×
Community /Pin to ProfileBookmark

Max value of array with unknown Array length

I can’t seem to find a way to get the maximum value of an array with an unknown quantity of variables in it. generally you’d write something like this:

tempmax=Math.max(ptotal[0],ptotal[1],ptotal[2],ptotal[3])

but that array ‘ptotal’ can range from having an array length of 1 to 6.

Is anyone able to help me out here. thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@GollumOct 31.2003 — You might consider implementing your own max() function that takes an array as an argument, uses the .length property and inspects each element looking for the max.
Copy linkTweet thisAlerts:
@CharlesOct 31.2003 — [font=monospace]<script type="text/javascript">

<!--

Array.prototype.max = function () {

if (this.length == 0) return undefined;

var n = Number(this[0]);

for (var i=1; i<this.length; i++) {n = Math.max(n, this[i])};

return n;

}



alert ([1, 2, 3, 2, 1].max())

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@Khalid_AliOct 31.2003 — Did I miss something here???

how come you are not able to use

array.length???

either its too early for me.?. or yoru question does not make any sense
Copy linkTweet thisAlerts:
@hyperstyleauthorNov 01.2003 — thanks charles, i didn't think to do it that way
×

Success!

Help @hyperstyle 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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