Jump to content

violagirl

Members
  • Posts

    26
  • Joined

  • Last visited

Everything posted by violagirl

  1. Well, first of all, I didn't know that match() DID return an an array, so thank you for your tidbit. However, I tried putting in both of those and neither of them worked! There must be another problem with the code.
  2. Hm.... so why do !name and name == false not seem to work the same way, then?Like I said, if I type var name = prompt("What is your name?","");while(name == false || name.toLowerCase() != "megan"){ name = prompt("Come on, say 'Megan'.","");}alert("Hello, " + name); it will not work, allowing the user to cancel.However, if I type var name = prompt("What is your name?","");while(name != true || name.toLowerCase() != "megan"){ name = prompt("Come on, say 'Megan'.","");}alert("Hello, " + name); it will, as I mentioned, put me into a loop which I can't break! It only works if I put name != null or !name. So does !name equal either name != null OR name != true, while tying out true or false explicitly doesn't work since this variable doesn't have a true or false attribute?
  3. 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
  4. 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!
  5. Does anyone else have any idea for number one, about the function returning true? I'm really curious now!
  6. Oh! I had thought that it was exactly the same as != true! So there is a difference, then? Well, obviously so, because I took your code and replaced the !name with name != true and just got myself stuck into a loop. :grumbles: So now I can't close the gosh darn box because it won't quit the loop even if I type in "Megan"!!!
  7. So why does this work <html><head><script type="text/javascript"><!--function increase(a,b) { return ++a * --b; };//--></script></head><body><script type="text/javascript"><!--document.write(increase(2,5));//--></script></body></html> (it outputs 12), but return a++ * b-- doesn't in that in outputs 10?If what you said is true, wouldn't the first one first increment a and then multiply the incremented b? That makes sense, because b would be incremented BEFORE it was multiplied but... why doesn't a++ * b-- output 15? Because the ++ IS before the * sign, wouldn't a be incremented, then multiplied by b, and then b would be incremented, thus resulting in 3 * 5? But it seems like it is multiplying before incrementing ANYTHING, even though that ++ IS before the * sign! Why is that! I'm sort of confused here!
  8. 1. There is a global search for the replace() method. Is there anything resembling this for the search() method? What I want to do is have it search through a document and give me the value for EVERY TIME it comes up. Since there could probably be a lot, it would probably be easiest to do through an array, I suppose. But either way, I couldn't figure out how to have it give me anything except the position of the FIRST occurance of the word/whatever I'm searching for. Is there any way to do this?2. Is there any way to use the split() method (or perhaps there is another method existing which would do this task) so it will split at a character but not erase the character itself? For example, let's say I had var txt = "How were you feeling yesterday?" and I wanted to split it up so it divided every time AFTER an e, so txt[0] would equal "How we", txt[1] would equal "re", txt[2] would equal " you fe", txt[3] would equal "e", txt[4] would equal "ling ye", txt[5] would equal "ste", and txt[6] would equal "rday?". Is there any way to do this? Right now I have it so every time e occurs the e dissapears and it splits by it. But I don't want the e to actually dissapear, just split before/after it. Does anyone know how to do this?3. How are slice() and substring() different, save for substring() being considerably older? I noticed the defintions differed slightly, as slice() said: The slice() method extracts a part of a string and returns the extracted part in a new string. and substring() said: The substring() method extracts the characters in a string between two specified indices. Which led me to believe that MAYBE the difference is that you couldn't store obj.substring() in a variable, but the following code worked perfectly well and the same way whether I used slice() or substring()! So what is the difference between the two, if there IS one (and if there isn't, why are they two seperate methods!!!) <script type="text/javascript">var txt = "Howdy partner!";var txt2 = txt.substring(6,13); //or var txt2 = txt.slice(6,13);document.write(txt2);</script> 4. Speaking of those methods... for slice(), substr(), and substring(), it was mentioned that you can extract characters from the end of a string by using a negative start number. I couldn't figure out a: what they meant by this and b: how to do it. For example, if I extracted from the end of the string "Monkeys!", would it mean spitting out something like "!sy" or the order would still be "ys!"? This is what I attempted (not exactly sure what I wanted, but it didn't work either way ): <script type="text/javascript">var txt = "Howdy partner!";var txt2 = txt.substring(-2,13);document.write(txt2);</script> As it treated it exactly as if I had typed txt.substring(0,13), I realized I must have done it incorrectly, but I'm not sure the correct way of doing this. So could somebody explain it to me?5. In this piece of code from the tutorial, why did they set the new property to null? I couldn't figure out the purpose of it!!! function employee(name,jobtitle,born){this.name=namethis.jobtitle=jobtitlethis.born=born}var fred=new employee("Fred Flintstone","Caveman",1970)employee.prototype.salary=nullfred.salary=20000document.write(fred.salary) 6. Why does this code work? I would have thought that as I am using perameters that are actually undefined in the function, it would come up as undefined. <html><head><script type = "text/javascript"><!--function Pets(one,two,three) { this.one=one; this.two=two; this.three=three; };//--></script></head><body><script type = "text/javascript"><!--var One = new Pets("Melvin","Pumpkin","Pineapple");One.four = "Silly!";document.write(One.four);//--></script></body></html> 7. Why won't this work!!! :is frustrated: <html><head><script type = "test/javascript"><!--function what (x) { if (x.toLowerCase().match("silly") == "silly") { return ("That's right!<br />"); } else { return ("Wrong!<br />"); } }//--></script></head><body><script type = "text/javascript"><!--document.write(what("Sillymonkeys!"));document.write(what("Shush!"));//--></script></body></html>
  9. So how would you DO that? If I wanted to have the first time have that prompt box pop up, and then if they still didn't do it correctly, to have another prompt box pop up with something like "Can't you guess?" until they get it right. Well, this is what I TRIED, but it doesn't work, so how would I do it? I tried a number of things and none of them worked. This actually applies to another situation with a similar thing, which I couldn't figure out how to do, so if someone could tell me how to do this, it will help me out with the other thing too, so it would be MUCH appreciated!script type = "text/javascript"><!--var name = prompt ("What is your name?",""); while (name.toLowerCase() != "megan"); { name = prompt("What, can't you guess?",""); };//--></script> Oh, wait, wait, I think I figured it out. Is this a good way to do it? <script type = "text/javascript"><!--var whee, name;name = prompt ("What is your name?","");do { if (name.toLowerCase() != "megan") { whee = prompt("What, can't you guess?",""); if (whee.toLowerCase() == "megan") {break;}; }; };while (name.toLowerCase() != "megan"); //--></script> I guess sometimes you DO figure things out if you think hard enough! Is that the usual way of doing such things? My only problem with it is I am trying to make it so the user can't press cancel. :-p I had assumed that so long as I wrote if (name.toLowerCase() != "megan"); it wouldn't break out of it unless this is true, but it allowed the user to press cancel unless I explictitly typed if (name.toLowerCase() != "megan" || name == null) and if I typed the order reversed, as if (name.toLowerCase() != "megan" || name == null), it would still let the user press cancel!!! Can someone tell me why this is! I remember you saying that for ||, so long as the first part of the test is true, the rest of it needn't be tested. So does Javascript just automatically have something built in where so long as you press cancel, it automatically lets them cancel, even if your loop doesn't explicitly say so? Also, the prevention of cancel only works for the first prompt, they can still press cancel on the whee prompt. How can I fix that? I tried if (whee.toLowerCase() == "megan" && whee != null) and if (whee != null && whee.toLowerCase() == "megan"), but neither of them worked. So now I'm wondering what would be the best way to do this. Thanks for the help!Megan
  10. Oh, well, that's nice to know! I'd still appreciate the examples, if anyone can give them (if not, quite understandable), but thank you for telling me that! I'll definately keep it in mind!
  11. 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.)
  12. 1: With the onerror event, I don't fully get the concept of having it return true or false. For example, this example from the tutorial: <html><head><script type="text/javascript">onerror=handleErrvar txt=""function handleErr(msg,url,l){txt="There was an error on this page.\n\n"txt+="Error: " + msg + "\n"txt+="URL: " + url + "\n"txt+="Line: " + l + "\n\n"txt+="Click OK to continue.\n\n"alert(txt)return true}function message(){adddlert("Welcome guest!")}</script></head><body><input type="button" value="View message" onclick="message()" /></body></html> Can someone tell me why the heck it has return true typed in there? I took it out and the code seems fully functional to me. Why do onerror statements return true or false? And do you just CHOOSE how they turn out, like how it seems here, with "return true" being typed out and all? I guess I'm just a little confused on that.2: Could someone tell me of any time you would want to write a throw statement with an object? I know you can do things with a Boolean like throw (true) or a string like throw ("Err") and whatnot, but when would you want to set it off with an object? I don't necessarily need written-out code (though that would be nice), but more of an explanation of at what time this would be useful.3: Has anyone else has problems with getting the special characters to work? I tried them out with alert boxes. Ampersand WORKS, but it also works if I don't even put the backslash before it! And form feed doesn't work, but maybe that one makes sense. It's appearing as the symbol for female with the circle on top and then a cross below it. And backspace is appearing as like an inverted bullet. Has anyone else had a similar problem and if so, can they tell me why this is happening?
  13. For number five, I meant... for example.... <script type = "text/javascript">var i = 0;for (i = 0, i < 5, i++){...};</script> Here's another, with txt being defined beforehand as "" for "seemingly" no reason (or is it just to make it global?): <script type="text/javascript">var txt=""function message(){try { adddlert("Welcome guest!") }catch(err) { txt="There was an error on this page.\n\n" txt+="Error description: " + err.description + "\n\n" txt+="Click OK to continue.\n\n" alert(txt) }}</script> Seems kind of pointless to me, but I have already this and a few other similar situations done already, so I couldn't help wondering if there was a good reason to do so.And as for number three, does anyone else know more about this? Even if it was more like radio buttons in an alert box, where you choose one or the other. I just want to know if there is a way to do this or not.Otherwise, thanks for the help!
  14. Sorry I messed up the title! I meant to type For...In Statements. Grah.The tutorial on this site says that a for...in statement can be used to loop through the elements of an array or the properties of an object. They gave an example with an array, but I can't figure out how one would use it for objects. Could somebody give me an example? Also, it said that the argument variable could be a named variable, an array element, or a property of an object. Could someone also give an example demonstrating how it would work if you used a property of an object? (this could probably be paired with the example I asked for above, I think)
  15. 1. In switches, do variables declared in a case only exist in that case or from every case after that one? What I thought was that if you used break; at the end of a case, all variables declared in that case would not continue on to the next case, but if you didn't put in the break; for whatever reason, they would bleed over into the next case and so on until a break; was written. Am I right on this or is it a little different?2. This next question is just a question of whether this is good code or not. I KNOW it works, but is there a better way to do this, or is this way good? <script type = "text/javascript"><!--do { var name = prompt ("What is your name?",""); };while (name != "Megan");//--></script> 3. Is there a way to make a box like a confirm box, except you choose what the buttons say rather than them having OK and Cancel?4. Are there cases where you would use the continue; out of an if statement? Like I know you usually see it as if (...) {continue;};, but I have seen the break used out of if statements, like for the end of cases on switches. Are there any cases where continue; is not used as an if statement, then? Just wondering.5. Is it best to declare or NOT declare the variable used in the perimeters in a for statement beforehand? And if you DO declare it beforehand, is it a bad thing to leave it out in the perimeters? (I noticed you can so long as you don't omit that semicolon)6. This too is just a question of style. Is this a good way to use a break? Or would it be better to just have the while statement be while (name != null && name != "Megan");?? <html><body><script type = "text/javascript"><!--do { var name = prompt ("What is your name?",""); if (name == null) {break;}; };while (name != "Megan"); //--></script></body></html> Thanks in advance!Megan
  16. The for..in version also seems to be a lot less strict. I typed in<html><body><script type="text/javascript">var xvar mycars = new Array()mycars[2] = "BMW"mycars[0] = "Saab"for (x in mycars){document.write(mycars[x] + "<br />")}</script></body></html> and it spit outBMWSaabjust in the order I typed them above, while the for loop version would not tolerate this and would spit outSaabundefinedBMWlining them up numerically and taking note of the fact that 1 is missing from the array. So the for..in version seems a lot more lienient. Are there any other differences?PS: Um... why aren't the BB tags for posting code snippets working?
  17. Yay! That makes a lot of sense and is a good thing to know. And I am saving the link (the first one).
  18. I think I get it!!!! That makes a lot of sense, so thanks a bunch! Static members, eh?
  19. 1. Can anyone tell me why this won't work? I didn't have any better luck by using || either. <html><body><script type="text/javascript"><!--var x = 10;var y = 2;if (y = 2 && x = 10){ document.write("Yay");};//--></script></body></html> 2. Also, for switches, if you don't HAVE a default, I'm assuming it's all right not to put break on the end of the last case because the switch is ending. Am I right in assuming this?3. Which of these is better? if (promptbox != null && promptbox != "") { greetings(); function greetings() { alert("Greetings, " + promptbox + "!"); }; }; or if (promptbox != null && promptbox != "") { function greetings() { alert("Greetings, " + promptbox + "!"); }; greetings();}; I would THINK the second one is better. Are there times when the first just absolutely won't work? It's just that it seems to me like you are calling the function before you are defining it, which seems impossible, but obviously not, because it works. So could anyone give me some insite on this?4. So when they say functions are always defined in the head, do they mean always always? Like there is absolutely no time they would be anywhere else? I think I can see why, as the point of them is so the code doesn't occur instantaneously, but still... was just wondering.5. Lastly, can someone please tell me why this doesn't work? I'm sure there's some idiotically obvious reason, but I'm just not seeing it: <html><body><script="text/javascript"><!--var a = 5;var b = (a == 5) ? 3 : 2; document.write(b);//--></script></body></html> Thanks!Megan
  20. Well, maybe it's not possible to do, but I was trying to make it so x equals some value, and then what would print out to the screen would be one value less. Now I know I could just write x-1 instead, but I wanted to know WHY this didn't work: var x = 53;document.write(x--); It spits out the value 53, instead of what I wanted, 52. Is there a reason this won't work? I got the -- to work when assigning to x, like this: var x = 53;x-- but not in the first situation. Why is that?Also, just on a quick sidenote, I happened to see <script language="javascript" type="text/javascript"> written somewhere. Is there any particular reason one would use this instead of just the shorter <script type="text/javascript>?
  21. All right, I'm starting to grasp it, but that also leaves me questioning why this is okay: var r = Math.random();if (r > 0.5){document.write("Whee!");};else{document.write("Yay!");}; Why don't you have to write new Math.random? I'm sure the reason is blantantly obvious, but I'm just not getting it.Oh, and lastly... my question before, that you answered about the single/double quotes. Well, since I just discovered that you can reverse the order, is there any difference at all at any time between which one you use. What I mean is, 1 or 2:1: document.write('<font color="blue">Hello</font>'); or 2: document.write("<font color='blue'>Hello</font>"); I'm assuming these are always interchangeable and just want to check in case it's otherwise.
  22. Thanks for the explanation about the double quotes! I honestly didn't know that! Um... what exactly do you mean by this? So when you are using a object like that in a variable, I realize that you can't just put d = Date(); instead of new Date(); in that program and still have it work, because I tried it to see, but I'm still not fully grasping why, what this new is doing. Is it just to use these predefined objects as variables that we use the new?As for the third question, yeah, thanks, that makes perfect sense! I can't believe I didn't think of that! That's how it always seems when my questions get answered. But either way, thank you both very much!Megan
  23. Well, I am an old-time newbie, in the fact that I have attempted to pick up JavaScript two times previously but gave up, not out of difficulty, but because I just stopped practicing. So here I am at it again, and this time I'll actually take the time to ASK questions when I am confused. My preferred method is to fool around with the code (albeit simple code, at this point) until I figure it out, and then if I still don't know, to ask. So here are my three questions:1. When using the document.write() function, is it possible to use the <font> tag for formatting? I tried and it didn't work at all. I got it to work with the <p> tag and some others, but not the font tag. And are there other formatting tags like this that aren't really supported with the document.write() function? What I wrote is... <html><head></head><body><script type="text/javascript"><!--document.write("<font size="4" face="arial" color="blue">Whee!</font>");//--></script></body></html> and with little success, and was wondering why.2. What exactly is the word new in new Date() doing? I couldn't find any articles on it.3. How do you make a text alert box appear or not appear based on the time? I understand that my coding is inadequate, as the function gets called regardless of the time, but I can't figure out how to write it correctly, so I thought I would ask. I wrote... <html><head><script type="text/javascript"><!--// this will make a message// appear if it is before noonvar d = new Date();var time = d.getHours();if (time < 12){ function message() { alert("Hey!"); };};//--></script></head><body onload="message()"></body></html Thanks in advance! Any help you could give me would be much appreciated!!!
×
×
  • Create New...