Arvorezinha 2.0 em JavaScript
_..._
Tired of using .-' '-. Are you still using
different cli / _ _\ that incoherent, bug
ent and serv /':. (o) /__) ridden, insecure mess
er languages /':. .,_ | | that is PHP?
on all your |': ; / \ /_/
web / ; `"`" } Would you like to use a
projects? ; ':., { modern, general-purpose,
/ ; } web-scale, optimized,
+-----------------------+ { object-oriented, scalable,
| TESTEMONIALS |`\ dependency-free, modular,
| "I installed nodeJS. | ; agnostic, baked-in, light-
| Five seconds of tink- | | weight, component framework,
| ering later I solved | ; responsive, event-driven,
| world hunger, effectu | | non-blocking i/o, efficent,
| ated peace in the mid | | perfect for data-intensive
| dle east and started | / an,--.........,-time appli-
| the first manned miss | ; .' -='.sy building
| ion to jupiter while |/ .\ 'eautiful
| holding a cigar in one| \ .'pplications
| hand and a death-ray |\~"`_ \ .'gram like its
| in the other." || ` \ ..'rock out with
+-----------------------+| =="''\.==''your cock out
~ /; ;/="" |`| |`| `
~..==` \\ |`| / /=="` Now You Can With Node.JS!
~` ~ /,\ / /= )") The Cancer of Javascript
~ ~~ _')") has just spread to servers!
~ ~ _,=~";`
~ =~"|; ;| Node.JSbird
~ ~ | ; | ===========
~ ~ |;|\ | Available via NPM
|/ \|
Não consigo deixar de nadar contra a corrente do blol/#c, que por norma odeia JavaScript e derivados. Trago-vos uma Arvorezinha 2.0 feita em JavaScript, que à boa moda deste blog, não acrescenta rigorosamente nada de interessante ou novo em relação às implementações anteriores. Podem corre-la na consola de qualquer browser, ou se quiserem crescer alguns centimetros de barba hipster, em node.js.
Futuramente farei algo com as funcionalidades do node, como por exemplo, um servidor de arvorezinhas à moda do phalic 100% RESTful.
var LittleTree = function(altura) {
this.altura = altura;
}
LittleTree.prototype.makeTree = function() {
var result = "";
for (var i = 1; i <= this.altura; i++) {
for (var j = 0; j < this.altura - i; j++) {
result += " ";
}
for (var j = 1; j <= 2*i - 1; j++) {
result += "*";
}
result += "\n";
}
return result;
}
LittleTree.prototype.makeTrunk = function() {
var result = "";
var lastLineWidth = this.altura * 2 - 1;
if (this.altura % 2 > 0) {
for (var i = 0; i <= (this.altura / 2); i++) {
for (var j = 0; j <= lastLineWidth / 4 - 1; j++) {
result += " ";
}
for (var j = 0; j <= lastLineWidth / 2; j++) {
result += "#";
}
result += "\n";
}
} else {
for (var i = 0; i <= (this.altura / 2); i++) {
for (var j = 0; j <= lastLineWidth / 4; j++) {
result += " ";
}
for (var j = 0; j <= lastLineWidth / 2 - 1; j++) {
result += "x";
}
result += "\n";
}
}
return result;
}
LittleTree.prototype.toString = function() {
return this.altura + ":\n" + this.makeTree() + this.makeTrunk() + "\n";
}
var sizes = {
1 : null,
2 : null,
3 : null,
4 : null,
5 : null,
6 : null,
10 : null,
20 : null,
40 : null,
};
for (size in sizes) {
console.log(String(new LittleTree(size)));
}
Output:
1:
*
#
2:
*
***
x
x
3:
*
***
*****
###
###
4:
*
***
*****
*******
xxx
xxx
xxx
5:
*
***
*****
*******
*********
#####
#####
#####
6:
*
***
*****
*******
*********
***********
xxxxx
xxxxx
xxxxx
xxxxx
10:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
...
Update: Conforme prometido, fiz um serviço RESTful baseado neste script powered by node.js + express.io. Demorou 5 minutos a fazer, só com uma mão e enquanto mandava uma cagada, conforme prometido pelo passaroco ali acima.
<3 amo. Agora faz em tcl/tk.
#NOJO