[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Javascript classes. Please take a look at my question.

Rahul Reddy

4/8/2015 5:44:00 PM

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

Evertjan.

4/8/2015 8:49:00 PM

0

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)

Denis McMahon

4/8/2015 9:05:00 PM

0

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

Christoph M. Becker

4/8/2015 9:52:00 PM

0

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

Thomas 'PointedEars' Lahn

4/8/2015 11:31:00 PM

0

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Denis McMahon

4/9/2015 1:49:00 AM

0

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

John Harris

4/9/2015 8:24:00 AM

0

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

Christoph M. Becker

4/9/2015 2:32:00 PM

0

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