Need some JavaScript puzzles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jorge

    #16
    Re: Need some JavaScript puzzles

    On Sep 27, 4:07 am, Conrad Lender <crlen...@yahoo .comwrote:
    On 2008-09-27 03:18, Jorge wrote:
    >
    Define function add such that>
         add(3)(4)
    returns 7.
    >
    javascript:aler t((function add (p) { var a=p; return function (p)
    { return a+p } })(3)(4))
    >
    You don't need to create an extra variable.
    >
    function add (first) {
        return function (second) {
            return first + second;
        }
    >
    }
    Damn it!
    (but that took you an hour hehe)

    --
    Jorge.

    Comment

    • Oltmans

      #17
      Re: Need some JavaScript puzzles

      On Sep 27, 7:07 am, Conrad Lender <crlen...@yahoo .comwrote:
      On 2008-09-27 03:18, Jorge wrote:
      >
      Define function add such that>
           add(3)(4)
      returns 7.
      >
      javascript:aler t((function add (p) { var a=p; return function (p)
      { return a+p } })(3)(4))
      >
      You don't need to create an extra variable.
      >
      function add (first) {
          return function (second) {
              return first + second;
          }
      >
      }
      >
      But hey!
      Don't spoil the fun for the OP by posting the solution!
      >
      @Douglas:
      I'd also appreciate more puzzles if you know any. This kind of (simple
      but interesting) problem is a godsend when you're teaching kids.
      I absolutely agree with you. We need some puzzles like that. I really
      liked that puzzle. This is exactly the kind of puzzle that I wanted to
      solve. I mean a puzzle that would force me hard to think about the
      language constructs and this one just did that.

      Also, I thank others who posted their invaluable comments.
      >
      The only thing I've seen recently that comes close to a puzzle is this
      website:
      >
       http://parentnode.org/static/challange2.html
      >
      You need to enter the correct password, that's all. No rewards :-) It's
      not pretty, rather the opposite, but if you like obfuscation, it might
      be interesting. Syntax highlighting helps.
      >
        - Conrad

      Comment

      • Oltmans

        #18
        Re: Need some JavaScript puzzles

        On Sep 27, 6:08 am, Douglas Crockford <nos...@sbcglob al.netwrote:
        Oltmans wrote:
        Hi guys,
        I'm learning JavaScript and I need some puzzles that can make me a
        better JavaScript programmer.
        >
        Dmitri showed me this one:
        >
        Define function add such that
        >
             add(3)(4)
        >
        returns 7.

        Douglas,
        I really appreciate you jumping in the discussion. I really was
        looking for such a puzzle that can force me think hard about language
        constructs and that one just did that. There are many puzzles that you
        can solve to improve your algorithmic skills but I was not looking for
        those. And I guess your puzzle was dead on. I agree with other people
        i.e. if you know more such puzzles like that then please let us know.
        Any puzzles that will make me think hard about how to use the
        JavaScript constructs. Please post more such puzzles. It will be a lot
        of fun. Thanks again.

        P.S: You're my JavaScript hero :) I'm even on the verge of finishing
        "Little Schemer" just because you recommended it.

        Comment

        • Conrad Lender

          #19
          Re: Need some JavaScript puzzles

          On 2008-09-28 02:08, Oltmans wrote:
          I absolutely agree with you. We need some puzzles like that. I really
          liked that puzzle. This is exactly the kind of puzzle that I wanted to
          solve. I mean a puzzle that would force me hard to think about the
          language constructs and this one just did that.
          This page has a number of short brain teasers:


          A few caveats:
          - The examples assume that a print() function has been defined, which
          is usually not the case in a browser environment (Rhino does have a
          print() function though).
          - The later examples build on the results of the previous ones, meaning
          that they are not self-contained.

          IIRC that page has even been posted here before.


          - Conrad

          Comment

          • =?ISO-8859-1?Q?Karl_Tikj=F8b_Krukow?=

            #20
            Re: Need some JavaScript puzzles

            Oltmans wrote:
            Hi guys,
            I'm learning JavaScript and I need some puzzles that can make me a
            better JavaScript programmer.
            I think the best way for you to learn JavaScript depends on your
            background. What other programming languages do you already know well?

            Comment

            • dhtml

              #21
              Re: Need some JavaScript puzzles

              Jorge wrote:
              On Sep 24, 3:14 am, dhtml <dhtmlkitc...@g mail.comwrote:
              >1. write a function return the binary representation of a given number
              >in "ON" and "OFF". For example, if the input is 47 (101111 in binary),
              >it should return "ON OFF ON ON ON ON".
              >

              >
              You could have used toString(radix) .

              var n = 101111;

              n.toString(2).r eplace(/1/g, "ON ").
              replace(/0/g,"OFF ").replace(/ $/,'');


              Garrett
              --
              Jorge.

              Comment

              • Peter Michaux

                #22
                Re: Need some JavaScript puzzles

                On Sep 23, 8:50 am, Oltmans <rolf.oltm...@g mail.comwrote:
                Hi guys,
                I'm learning JavaScript and I need some puzzles that can make me a
                better JavaScript programmer. I mean I'm looking out for programming
                puzzles (e.g. Project Euler or TopCoder) but I'm looking out for
                language specific puzzles that can make me a top-notch JavaScript
                programmer. a) Any puzzles you can recommend? b) Any programs that you
                can suggest that can make me learn JavaScript internals in greatest
                depth.
                >
                Please recommend anything. I know some very best programmers lurk
                around here so any help will be appreciated. Moreover, to people
                who've been using JavaScript for sometime, please recommend programs
                that you wish you had done earlier to understand internals in a better
                way. Thanks in advance.
                I highly recommend the following thread starting with this post:



                The whole thread is



                Peter

                Comment

                • Jorge

                  #23
                  Re: Need some JavaScript puzzles

                  On Sep 28, 9:04 pm, dhtml <dhtmlkitc...@g mail.comwrote:
                  Jorge wrote:
                  On Sep 24, 3:14 am, dhtml <dhtmlkitc...@g mail.comwrote:
                  1. write a function return the binary representation of a given number
                  in "ON" and "OFF". For example, if the input is 47 (101111 in binary),
                  it should return "ON OFF ON ON ON ON".
                  >>
                  You could have used toString(radix) .
                  >
                  var n = 101111;
                  >
                  n.toString(2).r eplace(/1/g, "ON ").
                  replace(/0/g,"OFF ").replace(/ $/,'');
                  >
                  Now that you say it... yes. I completely forgot its existence :-)

                  Thanks for posting that because I have played with it a little and I
                  have learnt that it converts reals as well:

                  (3.1416).toStri ng(2) ->
                  11.001001000011 111111100101110 010010001110100 010100111

                  and that it will fail as miserably as mine and without warning (for
                  those numbers that a 'number' primitive value can't represent
                  accurately):

                  javascript:aler t((900719925474 0991).toString( 2)) ->
                  111111111111111 111111111111111 111111111111111 11111111
                  javascript:aler t((900719925474 0991.4).toStrin g(2))->
                  111111111111111 111111111111111 111111111111111 11111111
                  javascript:aler t((900719925474 0991.5).toStrin g(2))->
                  100000000000000 000000000000000 000000000000000 000000000
                  javascript:aler t((900719925474 0992).toString( 2)) ->
                  100000000000000 000000000000000 000000000000000 000000000
                  javascript:aler t((900719925474 0993).toString( 2)) ->
                  100000000000000 000000000000000 000000000000000 000000000


                  While writing 'my version', I was going to 'logically' test the bits
                  with an '&', but I have discovered that the & operator operates on
                  just 32 bits, not on the 53 available in the mantissa of a 'number'
                  primitive value:

                  javascript:aler t(9007199254740 991 & Math.pow(2,31)) ===
                  -100000000000000 000000000000000 00
                  javascript:aler t(9007199254740 991 & Math.pow(2,32)) === 0

                  --
                  Jorge.

                  Comment

                  • Dr J R Stockton

                    #24
                    Re: Need some JavaScript puzzles

                    On Sep 29, 9:02 pm, Jorge <jo...@jorgecha morro.comwrote:
                    While writing 'my version', I was going to 'logically' test the bits
                    with an '&', but I have discovered that the & operator operates on
                    just 32 bits, not on the 53 available in the mantissa of a 'number'
                    primitive value:
                    Agreed, but one can access all bits of a Double for logical operations
                    by using arithmetic. Working example :
                    <URL:http://www.merlyn.demo n.co.uk/js-misc0.htm#TC>

                    --
                    (c) John Stockton, near London, UK. Posting with Google.
                    Mail: J.R.""""""""@ph ysics.org or (better) via Home Page at
                    Web: <URL:http://www.merlyn.demo n.co.uk/>
                    FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|

                    Comment

                    • Jorge

                      #25
                      Re: Need some JavaScript puzzles

                      On Sep 29, 11:49 pm, Dr J R Stockton <J.R.Stock...@p hysics.orgwrote :
                      On Sep 29, 9:02 pm, Jorge <jo...@jorgecha morro.comwrote:
                      >
                      While writing 'my version', I was going to 'logically' test the bits
                      with an '&', but I have discovered that the & operator operates on
                      just 32 bits, not on the 53 available in the mantissa of a 'number'
                      primitive value:
                      >
                      Agreed, but one can access all bits of a Double for logical operations
                      by using arithmetic.  Working example :
                      <URL:http://www.merlyn.demo n.co.uk/js-misc0.htm#TC>
                      Yes, that's what I did, see:

                      function toBinaryString (number) {
                      var weight= Math.pow(2, 52), binStr= '', decimalPoint;
                      do {
                      if (number >= weight) {
                      number-= weight;
                      binStr+= '1';
                      } else { binStr+= binStr ? '0' : ''; }

                      if (((weight/= 2) < 1) && !decimalPoint) {
                      decimalPoint= 1;
                      if (number) {
                      binStr+= binStr ? '.' : '0.';
                      } else { binStr+= binStr ? '' : '0'; }
                      }
                      } while (!decimalPoint || (number && weight))
                      return binStr;
                      };

                      --
                      Jorge.

                      Comment

                      • Jorge

                        #26
                        Re: Need some JavaScript puzzles

                        On Sep 27, 3:08 am, Douglas Crockford <nos...@sbcglob al.netwrote:
                        >
                        Dmitri showed me this one:
                        >

                        The most popular coding language for the web is javascript; so much so that since the advent of HTML5, it has now been officially accepted as the default standard. Javascript has moved beyond a smaller client-side browser-based language to become integrated not just for front-end design, but also for back-end server-side development. As a result […]

                        Dmitry Baranovskiy shows his mastery of TextMate and Raphael at WebJam. Here he live-codes a script for logo reflection that responds to mouse movement and rotates.&hellip;


                        --
                        Jorge.

                        Comment

                        Working...