Jump to content

Unicode,Font Color,Variable Type Test,Prompt Box,Code Error


violagirl

Recommended Posts

1. This one's just out of curiosity. What is the purpose, practically speaking, of getting the unicode value(s) out of a string?2. I can't get the fontcolor() method to work with rgb values. I was typing document.write(stringobj.fontcolor('#,#,#'));, but it wasn't working at all. I tried tons of colors. Does anyone know why that might be?3. Is there any method to retrieve the type of a variable? Such as a way to test if var q = "5" is a string or a number or whatnot. I know that I can use toString() to make anything into a string, but I was wondering if there was any way to test this, as I haven't come across it so far.4. When a user presses cancel on a prompt box, doesn't that set the value of the variable to null? So why won't this work? (there very well be an error in my code, so I apologize if that is it :) )

<html><head><script type="text/javascript">function name()  {  do	{	var str = prompt("What is your name?","");	};  while(str == "");  if (str.toLowerCase ().match("megan") == "megan")		{		alert("We have the same name!!!");		};else if (str == null)  {  document.write("Aw, why don't you wanna play?");   };};</script></head><body onload="name();"></body></html>

5. Lastly, could someone tell me why this won't work? I looked and looked, but I can't seem to find my error!!!

<html><head><script type = "text/javascript"><!--function message()  {  try	 {	 do	   {	   var sky = prompt("Hey, what's the color of the sky?","Red");	   };	 while (sky != null && sky.toLowerCase () != "blue");	 };  catch(err)	 {	 var conf = confirm("Do you want to go to a better place?");	 if (conf == true)	   {	   document.location.href=" http://www.google.com/"	 };  };//--></script></head><body><input type="button" value="Will ya?" onclick="message();" /></body></html>

(Yes, I know it's pointless. I'm just trying to practice so I use the first random sentences that pop into my mind.)

Link to comment
Share on other sites

1: As an exercise, I once made a function which would display the date (and any other message I wanted) using an LCD image that I created. It used a single image that had all of the characters in a single row in a very specific order. I then would parse the string, one character at a time, to decide what the unicode value was for that character and then use that data to determine the exact location of the letter in my image so that I could use it as the background image for a span element. For example, if each letter in my image was 16 pixels wide, and the ascii code was 97 ("a"), I would then know that I needed to display the image that began at 16 * (97 - 32) pixels from the left. (I started with the space character " ").2: I don't think those are part of any W3C standard. I would avoid those methods. Use the HTML DOM instead.3: Check out the "typeof" function.4: There are a couple problems there. First, you have a semi-colon after the closing curly brace in the do-while loop. It should be:

do{}while(test)

Also, you declare the str variable inside the do-while loop. Once that loop ends, that variable ceases to exist so your else if (str == null) fails.5: Same problem as above. You have semi-colons after your closing curly braces:

if (conf == true){  document.location.href=" http://www.google.com/"};

Link to comment
Share on other sites

4: There are a couple problems there. First, you have a semi-colon after the closing curly brace in the do-while loop. It should be:
do{}while(test)

Also, you declare the str variable inside the do-while loop. Once that loop ends, that variable ceases to exist so your else if (str == null) fails.5: Same problem as above. You have semi-colons after your closing curly braces:

if (conf == true){  document.location.href=" http://www.google.com/"};

Is there something wrong with putting semicolons after either of those? I have written tons of examples already that I did that and I worked fine! Is it not good after all? Anyway, so I took your advice but this still won't work!
<html><head><script type="text/javascript">function name()  {  var str;  do	{	str = prompt("What is your name?","");	}  while(str == "");  if (str.toLowerCase ().match("megan") == "megan")		{		alert("We have the same name!!!");		}else if (str == null)  {  document.write("Aw, why don't you wanna play?");   };};</script></head><body onload="name();"></body></html>

And the same with this!!!

<html><head><script type = "text/javascript"><!--function message()  {  try	 {	 var sky;	 do	   {	   sky = prompt("Hey, what's the color of the sky?","Red");	   }	 while (sky != null && sky.toLowerCase() != "blue");	 };  catch(err)	 {	 var conf = confirm("Do you want to go to a better place?");	 if (conf == true)	   {	   document.location.href=" http://www.google.com/"	 }  };//--></script></head><body><input type="button" value="Will ya?" onclick="message();" /></body></html>

So I guess I still don't quite understand what the problem is!

Link to comment
Share on other sites

Is there something wrong with putting semicolons after either of those?
Yes. You should not use semi-colons after blocks of code that are demarked with the curly braces.
try{	// something}catch(e){   // something}

not

try{	// something};catch(e){	// somthing};

Link to comment
Share on other sites

Yes. You should not use semi-colons after blocks of code that are demarked with the curly braces.
try{	// something}catch(e){   // something}

not

try{	// something};catch(e){	// somthing};

Now, usually when something is bad it is because it will not work. But in this case, because it does, I would like to know (out of curiosity) WHY it's bad to use semicolons after blocks of code demarked with a bracket. For example, this code is perfectly valid (I just tested it):
<script type = "text/javascript"><!--var txt = prompt("Pick a number!","");if (txt && !isNaN(txt))  {  document.write(txt);  };else if (!txt)  {  document.write("Boring!");  };else  {  document.write("Pick a real number, buddy!");  };//--></script>

I have no problem with not doing it; I would just like to know the reason. Now I WILL note that things like

if (txt && !isNaN(txt));  {  ...  }

will not work because you can't put a semicolon after the closing parenthesis in an if statement (and while statements, etc.), which makes sense because the if statement isn't done yet. But, as putting semicolons after ending curly brackets doesn't seem to invalidate the code, could someone give me a reason for why it's bad? Just wondering.Megan

Link to comment
Share on other sites

Hi there,Think about the way it compiles the code if you wrote it in a single line:if(something){ do_something() } if(something){ do_something() }var x var y var x--------------notice in the first one, you can add the semicolons if you really wanted to, but it makes no difference - it sees the if statement, reads between the parenthesis then moves to the next if statement. With the second line, you need the semicolons to distinguish that you're creating a variable x, then moving to the next statement and declaring the variable y .. and so on.It's not exactly bad to add the semicolons, but they're not a necessity, so why bother? :)Hope that helps

Link to comment
Share on other sites

Hi there,Think about the way it compiles the code if you wrote it in a single line:if(something){ do_something() } if(something){ do_something() }var x var y var x--------------notice in the first one, you can add the semicolons if you really wanted to, but it makes no difference - it sees the if statement, reads between the parenthesis then moves to the next if statement. With the second line, you need the semicolons to distinguish that you're creating a variable x, then moving to the next statement and declaring the variable y .. and so on.It's not exactly bad to add the semicolons, but they're not a necessity, so why bother? :)Hope that helps
Plus, if you are in the habit of using semi-colons after the braces, then you'll inadvertantly do something like this:do{};while()Which serves to separate the do command from the while command and there is no such thing, as far as I know, in javascript called do {}; (It's do-while).
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...