File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes playing with loops Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "playing with loops" Watch "playing with loops" New topic
Author

playing with loops

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hi guys,
can anyone help me with a small problem.
I am playing with loops, they are just the same as java loops, but with java I can see my main probs. with JavaScript I can't.
Now my problem is output: here is my code, it is loops to make up the hexadecimal variables for colours so that i can have a page full of all the colours that hex can show, so that when i make up web pages i can pick and choose colours.

I had an idea of document.write("<FONT COLOR="a+b+c+d+e+f">"+a+b+c+d+e+f+"<BR>"); but this did not work
any ideas?
Davy


How simple does it have to be???
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
first off.....eewwwww....lol
secoond off you missed the number sign for the font color, plus I would use a style tag instead of the font tag
theStr = '<div style="color:#' + a + b + c + d + e + f + '">' + a + b + c + d + e + f +'</div>';
I also help you have A and F declared...
See if that works for you....
plus what are you going to so about the A-F in the colors??

Eric
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey Eric,
I got this, but I can't see what is going on with thte letters now. it gets to A then stops?? why

Cheers Davy
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Play with this code, it shows you how to change a number to hex so you do not have to have 5 billion if statements
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Thanks eric,
It worked, but I am not sure I understand the code too well.

ok document.write is displayed on the screen, i get this so you made a table at the start, then closed the table rows and then closed the table.
for loops, getting the red green blue colours, but why 256 and why +=15???
all table data is: style ="cursor: pointer;font-size:1px;width:5px;height:5px;background-color:rgb('+r+','+g+','+b+')" onklick=\'alert("#'+r.toString(16)+g.toString(16)+b.toString(16)+'")\'> </td> ');
I don't understand what is bold, god I hope my editing worked.
I know an alert is like a pop up, I can see you want to output the rgb colours so r.toString() i get but why 16? and could you explain ' and " what is difference and why did you use them the way you did? what is \ for???
onklick changed because jr would not accept it with a c
Why in the web page, when click on a colour i get some #1133ff and some #10f, so why 3 not 6?

I am on a distance learning program, but has not got into it in great detail.
have done: fundamentals, loops and conditions, functions & objects, core objects so far, but these were merely showing us what it can do how to do basics.
Davy
[ March 10, 2004: Message edited by: Davy Kelly ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
By your questions I can say you are definitely beggining programming, but that is okay. Asking questions is a great way to learn...
This is CSS and is just for looks
cursor: pointer; <-- Change mouse to hand or arrow depending on browser
font-size:1px; <-- Need to do this to make boxes small in certain bowsers
red green blue colours, but why 256
Basic idea with out many details....
From your early education days you know that all colors are made up of red green and blue. Now they have the intensity rating from 0 to 256
0 is black, 256 is the full color
If you want to do the gray scale you would have to go up evenly
RED RGB(256,0,0)
GREEN RGB(0,256,0)
BLUE RGB(0,0,256)
000000 <--Black RGB(0,0,0)
111111
222222
333333
3A3A3A
3B3B3B
to
FEFEFE
FFFFFF <--White RGB(256,256,256)
and why +=15
I wanted to count by 15 since if you try doing it by 1 your browser will freeze
i += 15 is the same thing as i = i + 15
Why in the web page, when click on a colour i get some #1133ff and some #10f, so why 3 not 6?
Why 3 and 6 you can also have 4 and 5
The reason is that you really do not need to show that zero which is a holding place so that #10f is actually #01000f

r.toString(16)
HEX is base 16 and is what the color naming convention follows and our number system is base 10
base 16 is 1 thru 10 and A thru F
Therefor by using a built in Javascript function, it does the hard work that you were tyring to do if those 6 if statements
So when r is the number 11 and changes it to A
could you explain ' and " what is difference and why did you use them the way you did? what is \ for???
With Javascript you can define a string by using quotes or apostrophes
var theString1 = "This is the same"
var theString2 = 'This is the same'
now the \ is to escape a character so the complier of the browser does not see it.
If you were to try to use this line with the apostrophe it would be fine
var theString3 = "Eric's Car"
But if you try doing it with the aphostrophes
var theString4 = 'Eric's Car'
there would be an error
The compiler would see the string as 'Eric' and would try to figure out what s Car' is and would throw an error.
So to avoid the error you would have to do this
var theString5 = 'Eric\'s Car'
Not sure if I answered all of your questios, but I tried....
The reason I did it this way is 3 loops instead of you 6, it is more efficient and cleaner in my mind.
Eric
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
thanks eric,
I must admit java is a hell of a lot easier,
I forgot all about the bases:
toString(2) binary
toString(8) octal
toString(10) decimal
toString(16) hexidecimal
so now I can see why you did the RGB but I did not know about the loss of the pointless 0.
I knew about the +=, but i did not understand why you used 15 but not 1.
The pointer thing, I have never seen before, I just touched on CSS a few weeks ago, but it just went on about <div>
so if i want to use " in a string i could do it like:
str=(' "Help!" he shouted');
and
str("Davy's getting to grips with javascript, hopefully");
or do I have to use the \' or \" ???
I have made a few basic websites up but only with HTML, check out my clubs web page
cheers again Eric,
Davy
[ March 10, 2004: Message edited by: Davy Kelly ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
It is okay to have strings with
"This is a 'fun' thing" because the compiler is looking for " and not '
You do not have to escape it...
There are alot of escaped characters that you will learn on your way throught this \n\c\r are a couple of common ones used with textareas
If you want a good online reference: http://echoecho.com
If you want a great book on JavaScript, check out Scott Duffy's book "How to do Everything with JavaScript" I reviewed it in the bunkhouse on this site.
Also I know only a small amount of Java and I see JavaScript as being real easy, there is alot you can do with it, just takes time to figure stuff out.
Eric
 
 
subject: playing with loops
 
Threads others viewed
how to encrypt the text
Help with displaying data properly
basic javscript question
Changing the param of an applet with XSL or JS ...
new to JavaScript but got Probs
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture