Funny pieces of code

S

scriptguru

I've found some funny pieces of code in one project... I don't know: to
laught or to cry =)

function toHex(decimal) {
switch(decimal) {
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
decimal = "" + decimal;
}
return decimal;
}

function decToHex(num) {
return(toHex(Math.floor(num / 16)) + toHex(num % 16));
}

//------------------------------------------------------------------------

function isNumber(s) { for (var i = 0; i < s.length; i++) { var c =
s.charAt(i); if (!isDigit(c)) return false; } return true; }

function isDigit (c){return ((c >= "0") && (c <= "9")) }

function isLetter (c){return ( ((c >= "a") && (c <= "z")) || ((c >=
"A") && (c <= "Z")) ) }

var whitespace = " \t\n\r";
function isWhitespace (s){
if((s == null) || (s.length == 0)) return true;
for(var i = 0; i < s.length; i++){
if(whitespace.indexOf(s.charAt(i)) == -1) return false;
}
return true;
}
 
C

cwdjrxyz

I've found some funny pieces of code in one project... I don't know: to
laught or to cry =)

function toHex(decimal) {
switch(decimal) {
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
decimal = "" + decimal;
}
return decimal;
}

function decToHex(num) {
return(toHex(Math.floor(num / 16)) + toHex(num % 16));
}

//------------------------------------------------------------------------

function isNumber(s) { for (var i = 0; i < s.length; i++) { var c =
s.charAt(i); if (!isDigit(c)) return false; } return true; }

function isDigit (c){return ((c >= "0") && (c <= "9")) }

function isLetter (c){return ( ((c >= "a") && (c <= "z")) || ((c >=
"A") && (c <= "Z")) ) }

var whitespace = " \t\n\r";
function isWhitespace (s){
if((s == null) || (s.length == 0)) return true;
for(var i = 0; i < s.length; i++){
if(whitespace.indexOf(s.charAt(i)) == -1) return false;
}
return true;
}

Did you want someone to answer a question concerning the script you
quote? It has something to do with a fairly common procedure:
hex/decimal number system conversions. Does it not work, do you want
someone to explain it, or what? Or perhaps I have not had enough coffee
this morning and missed something.
 
L

Lee

cwdjrxyz said:
Did you want someone to answer a question concerning the script you
quote? It has something to do with a fairly common procedure:
hex/decimal number system conversions. Does it not work, do you want
someone to explain it, or what? Or perhaps I have not had enough coffee
this morning and missed something.

It's all done the hard way. It may be old enough that RegExp
couldn't be trusted to exist/work on all platforms, but
Number.toString(16) has been around for a long time.


--
 
S

scriptguru

Lee said:
It's all done the hard way. It may be old enough that RegExp
couldn't be trusted to exist/work on all platforms, but
Number.toString(16) has been around for a long time.

YES! Its all done possibly the worst way. I've never saw SO DUMB code.
 
R

Randy Webb

(e-mail address removed) said the following on 10/30/2006 12:54 PM:
YES! Its all done possibly the worst way. I've never saw SO DUMB code.

Never coded for the old generation browsers then, eh? Just think, in 5
years you could look back at your code now and think "I've never saw SO
DUMB code"!
 
S

scriptguru

Randy said:
Never coded for the old generation browsers then, eh? Just think, in 5
years you could look back at your code now and think "I've never saw SO
DUMB code"!
Yes, Randy, you're right. But is is a piece of code from _current time
project_.
It is not a code for old generation browsers.
I think it is just coded by C programmer without JS knowledge.

BTW when I looking at old code (C/C++/...) sometimes I think "It is
beautiful!", but this code looks ugly (but funny).
 
R

Randy Webb

(e-mail address removed) said the following on 10/30/2006 1:46 PM:
Yes, Randy, you're right. But is is a piece of code from _current time
project_.

And it is typical of "current time project" code if you spend any time
on the web viewing source code. I am not sure who said it originally (I
think Jim Ley but not positive) that if you want to see the best JS code
in the world, comp.lang.javascript is the place to see it. The reason is
that code posted here is open to constructive criticism (and any code
posted here gets it, whether verbal or not) whereas code written in a
boiler room to make a profit isn't subjected to that review - it is only
subjected to non-IE users bitching at the screen when the page doesn't
work properly because the idiot that wrote it thought IE was the only
browser on the web.
It is not a code for old generation browsers.
I think it is just coded by C programmer without JS knowledge.

Or any other programmer who either didn't know better or didn't care.
BTW when I looking at old code (C/C++/...) sometimes I think "It is
beautiful!", but this code looks ugly (but funny).

It *is* ugly and funny!
 
C

cwdjrxyz

YES! Its all done possibly the worst way. I've never saw SO DUMB code.

Things do change rapidly on the web. Let us start with a script from
1996.

1996 original

// Michael P. Scholtis ([email protected]) // All rights
reserved. January 15, 1996 // You may use this JavaScript example as
you see fit, as long as the // information within this comment above is
included in your script. function MakeArray(n){ this.length=n; for(var
i=1; n>=i; i++) this=i-1; return this } hex=new MakeArray(16);
hex[11]="A"; hex[12]="B"; hex[13]="C"; hex[14]="D"; hex[15]="E";
hex[16]="F"; function ToHex(x){ // Changes a int to hex (in the range 0
to 255) var high=x/16; var s=high+""; //1 s=s.substring(0,2); //2 the
combination of these are the same as the trunc function
high=parseInt(s,10); //3 var left=hex[high+1]; // left part of the
hex-value var low=x-high*16; // calculate the rest of the values
s=low+""; //1 s=s.substring(0,2); //2 the combination of these are the
same as the trunc function low=parseInt(s,10); //3 var
right=hex[low+1]; // right part of the hex-value var
string=left+""+right; // add the high and low together return string; }
function rainbow(text){ text=text.substring(3,text.length-4); // gets
rid of the HTML-comment-tags color_d1=255; // any value in 'begin' 0 to
255 mul=color_d1/text.length; for(i=0;text.length > i;i++){
color_d1=255*Math.sin(i/(text.length/3)); // some other things you can
try >> "=255-mul*i" to fade out, "=mul*i" to fade in, or try
"255*Math.sin(i/(text.length/3))" color_h1=ToHex(color_d1);
color_d2=mul*i; color_h2=ToHex(color_d2);
document.write(""+text.substring(i,i+1)+"<\/font>"); } } // --End
Hiding Here --> This is a rainbow text effect which you may upload to
your own site and use as you wish. Unfortunately you can only add about
10 lines of text here and then you have to use the code over again in
the next paragraph. You only need to use the code that's in the body
tag over again, not the part in the head tag. Let's see what happens
here if I continue to add text and see if it continues to change the
color of the text. And I'll just ramble on here a little more so you
can see the effect here and how it changes the text color. Cool effect
Huh!

Sorry the above has had white space removed. Note that it has hex
conversion. Now, if we consider the head script, we find that if we use
r,g,b colors instead of hex, we can avoid the hex conversion
completely, and the head script can be written:

js/rt.

/* Written by (e-mail address removed) in Feb. 2003 */
function paint(blah){
var x; var i; var c1=255; var x=blah.length; var f=c1/x; var c0=255;
for (i=0;x>i;i++){
c1=255*Math.round(Math.sin(i/(x/3))); var c2=Math.round(f*i);
document.write('<span style="color:
rgb('+c0+','+c1+','+c2+')">'+blah.substring(i,i+1)+'<\/span>');
}
}

The revised page using the above head script is at:
http://www.cwdjr.net/text/RainbowText.html . Today you can thus save a
huge amount of code.

This page shows age. All we need is a midi of the Beatle's "Lucy In The
Sky" and a few dancing cow animated gifs to make the page true to that
long gone era.
 
C

cwdjrxyz

cwdjrxyz said:
The revised page using the above head script is at:
http://www.cwdjr.net/text/RainbowText.html .

The page I gave you has a slight flaw in the effect. The correct one is
at http://www.cwdjr.net/text/rainbow_text.html . This page gives the
head script on the main page rather than as an external script. I
served this page as text/html to avoid the trouble of serving as
application/xhtml+xml for browsers that can accept it and for browsers
that require html 4.01 strict. In any event, I doubt if many would want
to use this old dated effect these days :). I really am going to have
to clean out the closets on the computer and server someday to get rid
of junk such as this that I do not use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top