Hints for the Challenge:

Below, you can find another hint for the Challenge.

You should only get here after reading, digesting, and applying the .

Hint

We had already decided on the following shape for the program:

var S = 'var S = fragment_2'fragment_2

It is a matter of putting all the pieces from the preceding hints together. If you are lazy, you write a program to assemble this program:

With a little bit of effort (noting that + concatenates strings, see JavaScript Basics), the calculation of the value  can also be incoporated (this makes the construction less error prone, and easier to adapt):

That way, it is also clear how the program was constructed. Here is the output of the preceding construction program ( and ):

That output should itself be a program, and not just any program, but in fact, a program that produces itself as output. So, finally, we obtained the following program that should work:

It is a program on one long line of characters. In fact, the line is too long to be acceptable, and we still need to do something about that. But this program produces the following output, which indeed equals its own program code (of course, you must ):

Can you refine this into a real solution without too long lines?

Try your program in the machine, and see if you can extend it to a complete solution.

Exercise

As an exercise, you can try to follow the other approach to writing Hello World! twice, based on a user-defined function (see Hint 6).

The general shape of the program now is

function tr(s) { /* ... use s to write output ... */ } tr('SSS');

This is again a string literal delimited by quotes, surrounded by two fragments. In this case, the second fragment is already completely determined, and the first fragment must be tweaked to use string parameter s to produce appropriate output. Note that, again, we still can choose the value SSS of s. The answer is in the next hint.

Apply the Hint

It is your turn ...

Need Another Hint?

If you tried this hint, or do not see how it helps, then you are ready for the .


Valid HTML 4.01 Transitional

Copyright ©2009, Tom Verhoeff, T.Verhoeff@TUE.NL