Jump to content

violagirl

Members
  • Posts

    26
  • Joined

  • Last visited

violagirl's Achievements

Newbie

Newbie (1/7)

0

Reputation

  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!
×
×
  • Create New...