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


Groups > comp.lang.javascript > #27249 > unrolled thread

Javascript classes. Please take a look at my question.

Started byRahul Reddy <rahulreddy160@gmail.com>
First post2015-04-08 10:44 -0700
Last post2015-04-09 09:24 +0100
Articles 8 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#27249 — Javascript classes. Please take a look at my question.

FromRahul Reddy <rahulreddy160@gmail.com>
Date2015-04-08 10:44 -0700
SubjectJavascript classes. Please take a look at my question.
Message-ID<e7563f81-a11d-4d56-a61a-c0c84f966c3e@googlegroups.com>
Exemplary data (assuming all votes are valid):

Total seats: 15
Votes: A => 15000, B => 5400, C => 5500, D => 5550

Result should be
Seats: A => 7, B => 2, C = 3, D = > 3

Write a class that calculates  for a variable number of parties and seats and include the needed two lines how to call your class.

[toc] | [next] | [standalone]


#27251

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-04-08 22:49 +0200
Message-ID<XnsA476E8295D2B8eejj99@194.109.133.246>
In reply to#27249
Rahul Reddy <rahulreddy160@gmail.com> wrote on 08 apr 2015 in
comp.lang.javascript: 

> Write a class that calculates  for a variable number of parties and
> seats and include the needed two lines how to call your class. 

You want us to ddo your homework? 

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#27252

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-04-08 21:05 +0000
Message-ID<mg456a$2sr$4@dont-email.me>
In reply to#27249
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

[toc] | [prev] | [next] | [standalone]


#27253

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-04-08 23:52 +0200
Message-ID<mg47u5$9q8$1@solani.org>
In reply to#27252
Denis McMahon wrote:

> 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.

<snip>

> 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].

It appears that the function can be used as constructor function and
called with `new election([...])`, so all is well. ;)

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#27254

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-04-09 01:31 +0200
Message-ID<2436927.QxtA20KIfX@PointedEars.de>
In reply to#27253
Christoph M. Becker wrote:

> Denis McMahon wrote:
>> 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.
> 
> <snip>
> 
>> 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].
> 
> It appears that the function can be used as constructor function

No, it cannot.  It is syntactically invalid to begin with.

> and called with `new election([...])`, so all is well. ;)

It is a fundamental misconception that OOP requires classes.  You do _not_ 
emulate OOP using functions as constructors in most ECMAScript 
implementations; you *do* OOP, just a special kind of prototype-based OOP.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27270

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-04-09 16:31 +0200
Message-ID<mg62gg$teu$1@solani.org>
In reply to#27254
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>> Denis McMahon wrote:
>>> 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.
>>
>> <snip>
>>
>>> 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].
>>
>> It appears that the function can be used as constructor function
> 
> No, it cannot.  It is syntactically invalid to begin with.

Indeed.  I had to replace instanceOf with instanceof (and a string
literal that was split due to Usenet posting) to be able to run the
function.  I did not go further.

>> and called with `new election([...])`, so all is well. ;)
> 
> It is a fundamental misconception that OOP requires classes.  You do _not_ 
> emulate OOP using functions as constructors in most ECMAScript 
> implementations; you *do* OOP, just a special kind of prototype-based OOP.

ACK.  However, neither the OP nor Denis seem to be really insterested in
such (and other) "subtleties", which caused me to post my reply which
was meant ironically.

Thanks, however, for clarifying the issue for other readers. :)

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#27255

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-04-09 01:48 +0000
Message-ID<mg4lqa$2sr$6@dont-email.me>
In reply to#27253
On Wed, 08 Apr 2015 23:52:10 +0200, Christoph M. Becker wrote:

> Denis McMahon wrote:
> 
>> 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.
> 
> <snip>
> 
>> 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].
> 
> It appears that the function can be used as constructor function and
> called with `new election([...])`, so all is well. ;)

Also the OP will need to deduce the form of the object supplied to the 
function by inspection of the code.

That's probably going to be almost as much work as writing his own code, 
or more by the time he looks everything up to understand what's happening 
at the level that his teacher will expect him to be able to explain it.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#27256

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-04-09 09:24 +0100
Message-ID<cmdciapukre68o7dpjc2mrli6pcevdbmo0@4ax.com>
In reply to#27249
On Wed, 8 Apr 2015 10:44:21 -0700 (PDT), Rahul Reddy
<rahulreddy160@gmail.com> wrote:

  <snip>
>Write a class that calculates  for a variable number of parties and seats and include the needed two lines how to call your class.

Classes don't calculate. It's objects belonging to classes that
calculate.

A class is simply a collection of very similar objects or potential
objects.

  John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web