Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27252

Re: Javascript classes. Please take a look at my question.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Javascript classes. Please take a look at my question.
Date Wed, 8 Apr 2015 21:05:14 +0000 (UTC)
Organization A noiseless patient Spider
Lines 53
Message-ID <mg456a$2sr$4@dont-email.me> (permalink)
References <e7563f81-a11d-4d56-a61a-c0c84f966c3e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Injection-Date Wed, 8 Apr 2015 21:05:14 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="66ffcfa4470a58bcddbdcd1913f98ab4"; logging-data="2971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fkep/f6Q/4XI96ES9DL1AGP7itj5sjB8="
User-Agent Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2)
Cancel-Lock sha1:ShEXVl4FLdDtgu6NN/0ajw5T1g0=
Xref csiph.com comp.lang.javascript:27252

Show key headers only | View raw


On Wed, 08 Apr 2015 10:44:21 -0700, Rahul Reddy wrote:

1) Please use a subject line that describes your javascript programming 
problem.

2) Please do not ask us to write your code from scratch.

3) We don't do homework and school programming assignments.

4) We don't do java either.

I haven't tested the following code, but it might return a result object 
that either contains the results you want, garbage, or a single error 
attribute.

function election(data) {
    var totalVotes;
    var votesPerSeat;
    var seats;
    if (!data instanceOf Object)
        return {error: 'data was not an object'};
    if (!data.seats)
        return {error: 'data had no attribute seats'};
    if (!data.seats instanceOf Number &&
        !data.seats instanceOf String)
        return {error: 'data attribute seats was not a valid type'};
    seats = parseInt(data.seats);
    if (!data.votes)
        return {error: 'data had no attribute votes'};
    if (!data.votes instanceOf Object)
        return {error: 'data attribute votes was not a valid type'};
    for (attr in data.votes)
        if (!data.votes[attr] instanceOf Number &&
            !data.votes[attr] instanceOf String)
                return {error: 'data attribute votes.' + atrr + ' was not 
a valid type'};
    for (attr in data.votes)
        tvotes += parseInt(data.votes[attr]);
    votesPerSeat = totalVotes / seats;
    data.result = {};
    for (attr in data.votes)
        data.result[attr] = Math.round(parseInt(data.votes[attr]) / 
votesPerSeat);
    return data;
}

Sorry it's not a class object, but I've never really grokked the whole 
emulate OOP with javascript using functions as classes thing[1].

As far as OP is concerned, that's my story and I'm sticking to it! ;)

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Javascript classes. Please take a look at my question. Rahul Reddy <rahulreddy160@gmail.com> - 2015-04-08 10:44 -0700
  Re: Javascript classes. Please take a look at my question. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-08 22:49 +0200
  Re: Javascript classes. Please take a look at my question. Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-08 21:05 +0000
    Re: Javascript classes. Please take a look at my question. "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-08 23:52 +0200
      Re: Javascript classes. Please take a look at my question. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-09 01:31 +0200
        Re: Javascript classes. Please take a look at my question. "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-09 16:31 +0200
      Re: Javascript classes. Please take a look at my question. Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-09 01:48 +0000
  Re: Javascript classes. Please take a look at my question. John Harris <niam@jghnorth.org.uk.invalid> - 2015-04-09 09:24 +0100

csiph-web