Create a square of increasing size by replicating the initial code
Pyth, 2 bytes
5
Try it online! Also Try it doubled, tripled!
How does that work?
\n
is the command that prints its argument with a trailing newline, while returning it simultaneously. So, each time you make an insertion, you turn the integer literal 5 into a number containing N copies of 5 concatenated, and the leading newlines basically make sure it's printed the appropriate number of times, thus keeping it square.
JavaScript (ES6), 42 32 30 bytes
s=[this.s]+0; console.log(s);
Second iteration:
s=[this.s]+0; s=[this.s]+0; console.log(s);console.log(s);
This works by appending a 0
to s
each time the first half of the code is run, and printing s
itself each time the second half is run. Takes advantage of four quirks of JavaScript:
- The current environment can be referred to with
this
. This allows us to dothis.s
in place ofs
. - When accessing a property that has not been defined on an object, instead of throwing an error, JavaScript returns
undefined
. - An array plus a number returns a string.
[1,2,3] + 4 === "1,2,34"
- When stringifying an array,
undefined
is converted to the empty string, which means that[undefined] + 0 === "0"
.
Put together, this means that we can express the first half (generating a string of zeroes) in just 13 bytes. If using alert
instead of console.log
is allowed, we can save 4 more bytes by shortening the second half.
05AB1E, 2 bytes
5=
Try it online!
Port of my Pyth answer.