473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need some JavaScript puzzles

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.
Sep 23 '08 #1
25 5415
Oltmans wrote:
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?
You will not become what you aspire to be by solving trivial puzzles. Find
a *real-world problem* that you want to solve. Try to solve it using an
ECMAScript implementation like JavaScript. There is no better exercise,
even if the scripted solution turns out not to be the best approach to solve
that particular problem.
b) Any programs that you can suggest that can make me learn JavaScript
internals in greatest depth.
Programs?
Please recommend anything. I know some very best programmers lurk around
here so any help will be appreciated.
I do not think they just *lurk* *here*.
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 can recommend Mozilla Thunderbird to subscribe to, read, and post to
comp.lang.javascript using your real name.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 23 '08 #2

You will not become what you aspire to be by solving trivial puzzles. *Find
a *real-world problem* that you want to solve. *Try to solve it using an
ECMAScript implementation like JavaScript. *There is no better exercise,
even if the scripted solution turns out not to be the best approach to solve
that particular problem.
Point taken. On a serious note, can you recommend some projects that I
should be working on in my spare time? Any ideas that you have. Thanks
in advance.
Sep 23 '08 #3
Oltmans wrote:
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.
Well, one recommendation would be to lurk around here. You'll get a
never-ending supply of "why doesn't this work?" questions. Since you'll
be facing some of these (of your own making), skill in working out why
something doesn't work can be quite handy. I probably spend more time on
the one thing that doesn't work than I do on the few things coded by me
that do work.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
Sep 23 '08 #4
Oltmans wrote:
>You will not become what you aspire to be by solving trivial puzzles. Find
a *real-world problem* that you want to solve. Try to solve it using an
ECMAScript implementation like JavaScript. There is no better exercise,
even if the scripted solution turns out not to be the best approach to solve
that particular problem.

Point taken. On a serious note, can you recommend some projects that I
should be working on in my spare time? Any ideas that you have. Thanks
in advance.
There are several open-source projects you could generally contribute to,
however I am afraid that without more-than-pedestrian knowledge you could
neither appreciate your participation in those projects as you would like
to, nor would it be likely that you could make a considerable contribution
to them.

IMHO, programming languages can be learned best like natural languages: by
using them in everyday life. Therefore, I (seriously) suggested you try to
use scripting to solve *your* problems first, those which bug *you* the
most. Start with the ones that look simple on the outset. Find them to be
more complex than you thought. Do not let yourself be discouraged, and take
your time to solve them eventually. Then use the experience gained to solve
even more complex problems.

When (not: if) you get stuck in the process, read *again* everything about
the topic that you can get your hands on (electronic material is cheapest
and readily available, but not always best; books are more expensive and
tend to be bad). Read it again. What is most important: Do not fall for
self-proclaimed gurus; compare your sources, verify what they are saying.
Look into the message, not at the messenger. If anything still remains
unclear, ask smart questions[1] about it.

This is how I started learning the languages a decade ago and it is still
serving me well.
HTH

PointedEars

P.S.
Please shorten, but do not remove attribution lines for quotations you leave in.
___________
[1] <http://catb.org/~esr/faqs/smart-questions.html>
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 23 '08 #5

"Oltmans" <ro**********@gmail.comwrote in message
news:8c**********************************@l64g2000 hse.googlegroups.com...
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.
OK -
Create a Bingo Game
or a Checkers game
or a Calculator like the windows accessories calculator
or a number-to-words (like writing a check) program without getting help
or all the above.
or if you like recursion, look at the "towers of Hanoi" puzzle.(google it)
Sep 24 '08 #6
Oltmans wrote:
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

Only the best lurkers here!
O_o
:-D
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.

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".
Sep 24 '08 #7
On Sep 24, 3:14*am, dhtml <dhtmlkitc...@gmail.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".
http://preview.tinyurl.com/3unmha

--
Jorge.
Sep 24 '08 #8
On Sep 24, 3:14*am, dhtml <dhtmlkitc...@gmail.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".
[x] done: http://preview.tinyurl.com/3unmha

--
Jorge.
Sep 24 '08 #9
On Sep 23, 9:01*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Oltmans wrote:
You will not become what you aspire to be by solving trivial puzzles. *Find
a *real-world problem* that you want to solve.
NO. Real-world problems are generally either trivial or complex, and
neither is appropriate for practice. OP, choose instead problems
which, at your current state of knowledge, are on the verge of
difficulty. When you approach the ability to do a real-world non-
trivial problem that you want to solve, set it aside; the risks of
including someting bad and not realising it or not bothering to fix it
are too great. Instead, choose a problem of sufficient fifficulty
which you do not need to solve, so that you can throw away the
solution. Don't assume premature competence.

Tackle problems that you really need to solve only when you know
enough to be reasonably sure of making a good job of them.
Please shorten, but do not remove attribution lines for quotations you leave in.
Ignore that : people give attributions for a purpose, and full
attributions are useful in various ways that Thomas Lahn does not
understand. Remember, be is not psychologically normal, as is obvious
from reading a selection of his replies. Perhaps he is the Kaiser
reincarnated; perhaps something worse.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
Sep 24 '08 #10
On Sep 23, 8:50*am, Oltmans <rolf.oltm...@gmail.comwrote:
Hi guys,
I'm learning JavaScript and I need some puzzles that can make me a
better JavaScript programmer. [...]

[...] please recommend programs
that you wish you had done earlier to understand internals in a better
way. Thanks in advance.
One of the first Javascript programs that I wrote lives here:

http://webstervanrobot.com/ (only tested on Firefox)

The program implements a small programming language on top of
Javascript that allows a robot to move around a grid of streets and
avenues.

It got me acquainted with the following concepts:

creating objects
manipulating DOM
parsing strings
implementing data structures
responding to events
setting up callbacks and responding to them
variable scoping (this, var, etc.)
cross-browser pain (which I have punted on so far)

If you're learning Javascript, this might be a fun program to hack on,
because it's all self-contained and not tied to any domain, and I'm
hoping to make it a useful program. It really is a "Javascript
program," as opposed to code that lives within a larger system, for
better or worse.

Some challenges:

1) Make it work on a browser other than Firefox.
2) Change the world to be rendered on a canvas instead of tables.
3) Add scrolling to the world.
4) Add new capabilities for the robot.
5) Add new syntax to the programming language.
6) Simply play around with the code, try to restructure it to your
liking.
7) Get the program to have a mode where you can click on it to see
the internal data structures that define the world.

Yes, I'm subversively asking for help with my program, but the program
was written with the intention of helping people learn Javascript (and
programming in general). It's free software.

Cheers,

Steve
http://webstervanrobot.com/

Sep 24 '08 #11
On Sep 23, 5:50*pm, Oltmans <rolf.oltm...@gmail.comwrote:
>
Please recommend anything.
Have you seen Douglas Crockford's videos already ?

http://developer.yahoo.com/yui/theater/

--
Jorge.
Sep 25 '08 #12
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.
Sep 27 '08 #13
On Sep 27, 3:08*am, Douglas Crockford <nos...@sbcglobal.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.
javascript:alert((function add (p) { var a=p; return function (p)
{ return a+p } })(3)(4))

--
Jorge
Sep 27 '08 #14
On 2008-09-27 03:18, Jorge wrote:
>Define function add such that>
add(3)(4)
returns 7.

javascript:alert((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.

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
Sep 27 '08 #15
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:alert((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.
Sep 27 '08 #16
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:alert((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
Sep 28 '08 #17
On Sep 27, 6:08*am, Douglas Crockford <nos...@sbcglobal.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.
Sep 28 '08 #18
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:
http://dow.ngra.de/2008/02/28/javascript-puzzlers/

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
Sep 28 '08 #19
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?
Sep 28 '08 #20
Jorge wrote:
On Sep 24, 3:14 am, dhtml <dhtmlkitc...@gmail.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".

http://preview.tinyurl.com/3unmha
You could have used toString(radix).

var n = 101111;

n.toString(2).replace(/1/g, "ON ").
replace(/0/g,"OFF ").replace(/ $/,'');
Garrett
--
Jorge.
Sep 28 '08 #21
On Sep 23, 8:50*am, Oltmans <rolf.oltm...@gmail.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:

http://groups.google.com/group/comp....559592272d4e54

The whole thread is

http://groups.google.com/group/comp....559592272d4e54

Peter
Sep 28 '08 #22
On Sep 28, 9:04 pm, dhtml <dhtmlkitc...@gmail.comwrote:
Jorge wrote:
On Sep 24, 3:14 am, dhtml <dhtmlkitc...@gmail.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".
http://jorgechamorro.com/cljs/016/

You could have used toString(radix).

var n = 101111;

n.toString(2).replace(/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).toString(2) ->
11.00100100001111111110010111001001000111010001010 0111

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:alert((9007199254740991).toString(2)) ->
11111111111111111111111111111111111111111111111111 111
javascript:alert((9007199254740991.4).toString(2))->
11111111111111111111111111111111111111111111111111 111
javascript:alert((9007199254740991.5).toString(2))->
10000000000000000000000000000000000000000000000000 0000
javascript:alert((9007199254740992).toString(2)) ->
10000000000000000000000000000000000000000000000000 0000
javascript:alert((9007199254740993).toString(2)) ->
10000000000000000000000000000000000000000000000000 0000
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:alert(9007199254740991 & Math.pow(2,31)) ===
-10000000000000000000000000000000
javascript:alert(9007199254740991 & Math.pow(2,32)) === 0

--
Jorge.
Sep 29 '08 #23
On Sep 29, 9:02*pm, Jorge <jo...@jorgechamorro.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.demon.co.uk/js-misc0.htm#TC>

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
Sep 29 '08 #24
On Sep 29, 11:49*pm, Dr J R Stockton <J.R.Stock...@physics.orgwrote:
On Sep 29, 9:02*pm, Jorge <jo...@jorgechamorro.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.demon.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.
Sep 30 '08 #25
On Sep 27, 3:08*am, Douglas Crockford <nos...@sbcglobal.netwrote:
>
Dmitri showed me this one:
http://dmitry.baranovskiy.com/
http://raphaeljs.com/
http://vimeo.com/1815045

--
Jorge.
Oct 9 '08 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roberto Gallo | last post by:
Hi, I have an applet running inside an html document and I need to read and to set some of html form's textfield and textarea input from my applet. How can I do that? Do I need a javascript? ...
8
by: Victor | last post by:
I need a JavaScript timer - I have five events I need to time, that can be triggered by a mouseclick event, or a keypress event. Each event is separated by only one to two seconds. The first...
9
by: Mickey Segal | last post by:
The long-simmering Eolas patent dispute: http://www.microsoft.com/presspass/press/2003/oct03/10-06EOLASPR.mspx has led to an optional Microsoft Update: http://support.microsoft.com/kb/912945/en-us...
22
by: the_grove_man | last post by:
I purchased a book titled "Pro ASP.NET 2.0" to get up to speed on web stuff because I ususally do Windows Form Applications.. But in the first chapters I was reading this week it brought to mind...
3
by: Jevgeni Kabanov | last post by:
Just a couple of language puzzles I compiled from a course I took recently, quite entertaining IMHO: http://dow.ngra.de/2008/02/28/javascript-puzzlers/ What do you think? Jevgeni Kabanov
1
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric...
10
by: Dutchmarshalls | last post by:
Hi All, I'm using a Pay Pal form script, but locally tested in Dreamweaver with a browser it will do exactly what I'm aspect it to do. Only when I'm uploaded the file on the server it will give...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.