Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. 99 Bottles of Beer

99 Bottles of Beer

Scheduled Pinned Locked Moved JavaScript
javascriptalgorithmshelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simewu
    wrote on last edited by
    #1

    In JavaScript I need the shortest possible code to generate the 99 bottles of beer on the wall song lyrics into a string (using "\n" as the line separator). The problem is making it grammatically correct while still keeping the code compact. For a refresher: 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall. 97 bottles of beer on the wall, 97 bottles of beer. Take one down and pass it around, 96 bottles of beer on the wall. ... 2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall. 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall. I'm searching for the cleverest, most compact approach. Any ideas?

    P S 2 Replies Last reply
    0
    • S Simewu

      In JavaScript I need the shortest possible code to generate the 99 bottles of beer on the wall song lyrics into a string (using "\n" as the line separator). The problem is making it grammatically correct while still keeping the code compact. For a refresher: 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall. 97 bottles of beer on the wall, 97 bottles of beer. Take one down and pass it around, 96 bottles of beer on the wall. ... 2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall. 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall. I'm searching for the cleverest, most compact approach. Any ideas?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      It's your homework; you do it. Or maybe you could search for a website with that problem already written in many languages...

      S 1 Reply Last reply
      0
      • P PIEBALDconsult

        It's your homework; you do it. Or maybe you could search for a website with that problem already written in many languages...

        S Offline
        S Offline
        Simewu
        wrote on last edited by
        #3

        Course I can do it, I just want to see some new, clever approaches. What, does nobody code for fun anymore? Here, i'll post mine.

        1 Reply Last reply
        0
        • S Simewu

          In JavaScript I need the shortest possible code to generate the 99 bottles of beer on the wall song lyrics into a string (using "\n" as the line separator). The problem is making it grammatically correct while still keeping the code compact. For a refresher: 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall. 97 bottles of beer on the wall, 97 bottles of beer. Take one down and pass it around, 96 bottles of beer on the wall. ... 2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall. 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall. I'm searching for the cleverest, most compact approach. Any ideas?

          S Offline
          S Offline
          Simewu
          wrote on last edited by
          #4

          Here's the one I made. 194 characters long. This is my first time code golfing, but I think i'm getting the hang of it. :suss:

          for(var $=b=c=d='',_=99,a=' on the wall.';b=' bottle'+(~(_-2)?'s':d)+' of beer',_>~0;c=_--&&'\n\n') 99-_&&($+='.\nTake one down and pass it around, '+(_||'no more')+b+a),$+=_?c+_+b+a+', '+_+b:d;

          console.log($);

          P 1 Reply Last reply
          0
          • S Simewu

            Here's the one I made. 194 characters long. This is my first time code golfing, but I think i'm getting the hang of it. :suss:

            for(var $=b=c=d='',_=99,a=' on the wall.';b=' bottle'+(~(_-2)?'s':d)+' of beer',_>~0;c=_--&&'\n\n') 99-_&&($+='.\nTake one down and pass it around, '+(_||'no more')+b+a),$+=_?c+_+b+a+', '+_+b:d;

            console.log($);

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Golfscript? http://99-bottles-of-beer.net/language-golfscript-2515.html[^]

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              Golfscript? http://99-bottles-of-beer.net/language-golfscript-2515.html[^]

              S Offline
              S Offline
              Simewu
              wrote on last edited by
              #6

              PIEBALDconsult wrote:

              http://99-bottles-of-beer.net/language-golfscript-2515.html

              Its a cool website but the JavaScript one was pretty disappointing, its badly written, then encrypted with an escape sequence to hide the actual code.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups