Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 11:50 PM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by 007julien View Post
There is never two dominant numbers !
Well, true, if the definition of dominant is "more than half the length of the array". But I was just following the lead of your original code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-01-2012, 11:53 PM   PM User | #17
evaaa
New to the CF scene

 
Join Date: Dec 2012
Location: London
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
evaaa is an unknown quantity at this point
Thank you, exactly this. Much better way than mine.
evaaa is offline   Reply With Quote
Old 12-02-2012, 03:32 AM   PM User | #18
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
why do you keep talking about more than half?
as i pointed out earlier the Mode isnt always as common as half the elements

Dominant number is not actually a known mathematical term so i assume he ment mode?

Last edited by donna1; 12-02-2012 at 03:34 AM..
donna1 is offline   Reply With Quote
Old 12-02-2012, 12:37 PM   PM User | #19
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by donna1 View Post
why do you keep talking about more than half?
as i pointed out earlier the Mode isnt always as common as half the elements

Dominant number is not actually a known mathematical term so i assume he ment mode?
Not so.

http://stackoverflow.com/questions/9...er-in-an-array

A real number in the array is called a decimal dominant if it occurs more than n/10 times in the array.

But I am not sure what the practical use of this.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-02-2012 at 12:43 PM..
Philip M is offline   Reply With Quote
Old 12-02-2012, 02:59 PM   PM User | #20
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
This kind of script can be useful to study relative frequencies of letters in the English language... Here is an example.

The issue is: The frequency variations can they give an indication of the author's speech?
007julien is offline   Reply With Quote
Old 12-03-2012, 07:24 AM   PM User | #21
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
is there anything map/filter can't do?

Code:
r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]
  .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0]

r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 12-03-2012, 07:43 AM   PM User | #22
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by rnd me View Post
is there anything map/filter can't do?

Code:
r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]
  .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0]

r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191
Yes - report the mode when two values occur the same number of times. Obviously you can have more than one mode.
Having two modes is called "bimodal". More than 2 modes is called "multi-modal".

felgall's and Old Pedant's scripts do this.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-03-2012 at 07:55 AM..
Philip M is offline   Reply With Quote
Old 12-03-2012, 08:43 AM   PM User | #23
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
Yes - report the mode when two values occur the same number of times. Obviously you can have more than one mode.
Having two modes is called "bimodal". More than 2 modes is called "multi-modal".

felgall's and Old Pedant's scripts do this.
my bad, i didn't understand that was required. i've never been good at math...

that does make a bit more work.
what a good opportunity to expand my functional example!

to compare a collection, i needed a variable (counts), and a way get the unique values, unique. i went ahead and named count() and pulled it out of the body to reduce clutter.

isn't it neat how those new pieces bolt right on to the existing solution? that's functional programming for ya.
Code:
function unique(a){return this[a]?0:(this[a]=1)}
function count(a){ return this[a]?(this[a]+=1):(this[a]=1),this;}

r=[ 77, 101, 141, 91, 91,  191, 191,343 ]

counts=r.map( count, [])[0];

r.filter(function(a,b){ 
   return counts[a]==this;
  },   
   Math.max.apply(0, counts.filter(Number))
).filter( unique, {});
this should output 91 and 191, right? it does.

and given[ 77, 101, 141, 91, 91, 191, 191,343, 44,44,65 ], it gives [91, 191, 44], so it's appears to be a scale-able solution.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 12-03-2012 at 08:47 AM..
rnd me is offline   Reply With Quote
Old 12-03-2012, 09:43 AM   PM User | #24
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by rnd me View Post
this should output 91 and 191, right? it does.

and given[ 77, 101, 141, 91, 91, 191, 191, 343, 44, 44, 65 ], it gives [91, 191, 44], so it's appears to be a scale-able solution.
Yep, it does!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-03-2012, 01:56 PM   PM User | #25
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Thanks ! is it very necessary to return many values with map ?

Code:
var r=[ 77, 101, 141, 91, 91, 191, 191, 343, 44, 44, 65 ],lttFrq=[];// an array or an object
   function count(a){lttFrq[a]?lttFrq[a]+=1:(lttFrq[a]=1);}
   r.map(count);
   alert(lttFrq);
Then (for IE only9 web users) filter give the «dominant» value, or all others frequencies...
007julien is offline   Reply With Quote
Old 12-03-2012, 06:28 PM   PM User | #26
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by 007julien View Post
Thanks ! is it very necessary to return many values with map ?
no, i returned an array to avoid a variable.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 12-04-2012, 01:06 AM   PM User | #27
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Of course, the original poster *did* make the requirement that, *FOR HIM*, "dominant" implied that the number had to appear more than half the time.

Qoute from post #3:
Quote:
Yes exactly. I mean more times than the half
So all the stuff I did, and RndMe did, to produce multiple answers in the case of duplicates is fun but not, per his statement, needed.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 AM.


Advertisement
Log in to turn off these ads.