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


Groups > comp.lang.javascript > #13654

Re: Javascript Math problem

From Tom <thart@vicon.net>
Newsgroups comp.lang.javascript
Subject Re: Javascript Math problem
Date 2012-04-27 18:02 -0700
Organization Wieslauf BBS
Message-ID <jnffjm$bcd$1@wieslauf.sub.de> (permalink)
References <jnf7b8$7d0$1@wieslauf.sub.de> <jnfabu$g0r$1@news.albasani.net>

Show all headers | View raw


On 4/27/2012 4:33 PM, Stefan Weiss wrote:
> On 2012-04-28 00:41, Tom wrote:
>> Hi, I was wondering if someone could help me with math puzzle.  I need a
>> function that will give me the number of times an integer is divisible
>> by 3, but I can't use the round function.   In other words,
>> if I sent the value of 6, it would return 2 since 6/3 = 2.  9/3 = 3 etc.
>> For numbers that are not evenly divisible,  5/3 would need to return 1,
>> 7/3 would return 2 etc, basically scrap the decimal numbers.  But like I
>> said it can't use round.
>>
>> I was thinking that possibly it would use some combination of the
>> division and modulus operators (/,%), but I haven't been able to figure
>> it out yet.
>
> Math.round() would have been the wrong tool anyway: Math.round(5/3) is
> 2, not 1. You probably meant Math.floor(): Math.floor(5/3) is 1. Note
> that floor() will round in the wrong direction if the quotient is negative.
>
> There are other methods, too:
>
>    parseInt(5/3)
>    ~~(5/3)
>    (5/3)>>  0
>    (5/3) | 0
>
> By the way, what you are describing is called "integer division".
>
> Out of curiosity, why can't you use round()? Was that part of the
> assignment?
>
> - stefan


Actually its for something I'm doing at work.

I'm using a tool that automatically generates scripts (in html and js) 
for taking orders for products.  You don't have to be a coder to use it 
and I don't normally do this, but it does most of the work for you. You 
just basically use templates of screens and tack them together in the 
desired flow.  For this particular script, the client was giving 1 free 
widget with every 3 purchased.

There is a screen in this tool where you can build and test functions
by clicking a button, but for some reason it didn't like round or floor. 
  So I found myself trying division, modulus and trying to subtract out 
the decimal part.

I did come up with a work around, but your solution is much cleaner. 
Thanks for the help.

Tom

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


Thread

Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 15:41 -0700
  Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 01:33 +0200
    Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 18:02 -0700
    Re: Javascript Math problem Dr J R Stockton <reply1217@merlyn.demon.co.uk.not.invalid> - 2012-04-29 21:07 +0100
      Re: Javascript Math problem "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-30 08:06 +0000
        Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-30 01:33 -0700
        Re: Javascript Math problem Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-05-01 19:30 +0100
  Re: Javascript Math problem "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-04-27 17:30 -0700
    Re: Javascript Math problem Tom <thart@vicon.net> - 2012-04-27 18:05 -0700
    Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 04:00 +0200
      Re: Javascript Math problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-28 13:04 +0200
        Re: Javascript Math problem Stefan Weiss <krewecherl@gmail.com> - 2012-04-28 21:10 +0200
          Re: Javascript Math problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-28 22:23 +0200
            Re: Javascript Math problem "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-04-28 23:39 +0300
            JS/Terminology (was: Javascript Math problem) Stefan Weiss <krewecherl@gmail.com> - 2012-04-29 05:59 +0200
              Re: JS/Terminology Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-29 16:15 +0200
                Re: JS/Terminology Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-29 16:20 +0200
                Re: JS/Terminology Puzzled <scratching.head@example.com> - 2012-05-07 16:22 -0400
                Re: JS/Terminology Gene Wirchenko <genew@ocis.net> - 2012-05-07 14:36 -0700
                Re: JS/Terminology "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-05-08 08:40 +0000
                Re: JS/Terminology Gene Wirchenko <genew@ocis.net> - 2012-05-08 09:29 -0700
            Re: Javascript Math problem John G Harris <john@nospam.demon.co.uk> - 2012-04-29 17:30 +0100
          Re: Javascript Math problem Scott Sauyet <scott.sauyet@gmail.com> - 2012-04-30 06:19 -0700
            Re: Javascript Math problem Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-05-01 19:48 +0100
              Re: Javascript Math problem Gene Wirchenko <genew@ocis.net> - 2012-05-01 19:30 -0700
      Re: Javascript Math problem "Mel Smith" <med_cutout_syntel@aol.com> - 2012-04-28 09:00 -0600
    Re: Javascript Math problem "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-28 09:02 +0000

csiph-web