Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: function to get frequencies of a char array Date: Sat, 24 Jan 2015 17:49:40 +0000 Organization: A noiseless patient Spider Lines: 27 Message-ID: <87lhkskr0r.fsf@bsb.me.uk> References: <26839541.MRbOrngyXZ@PointedEars.de> <87twzgl0aq.fsf@bsb.me.uk> <2697158.aGRKFDBAEn@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="27818"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18W3UaSr8HV07y++BD+P9CLjgcPI3nCofo=" Cancel-Lock: sha1:Esb5kZpfTaB6/ie7DwSPA4SWwfU= sha1:ZafrB/o7GBf/azfVtrC675wLHMw= X-BSB-Auth: 1.8f6dcc04072488e7148a.20150124174940GMT.87lhkskr0r.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:26694 Thomas 'PointedEars' Lahn writes: > Ben Bacarisse wrote: > >> Thomas 'PointedEars' Lahn writes: >>> var counts = {}; >>> >>> for (var i = 0, len = a.length; i < len; ++i) >>> { >>> /* NOTE: hasOwnProperty() may be required */ >>> var e = a[i]; >>> e ? ++counts[e] : (counts[e] = 1); >>> } >> >> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and >> immediately below): […] > > No, but > > if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); } Why? And did you mean to add the if (e) test to the forEach examples, too? -- Ben.