JavaScript - Logic Problem
im trying to add the following numbers: 0+1+2+3+4. The actual answer being 10 obviously. i dont realy get why its coming out as 5 and not 10.
Code: var sum = 0; for (i = 0; i < 5; i++); { sum = sum + i; } document.write("Sum is: " + sum); Similar TutorialsHow can we identify logical errors from other types of errors?
I was going through LinkedIn today, when I noticed something that I couldn't figure out, logic-wise. When you search using their search feature at top left, results come up live... no big deal, easy enough, keyup, no biggie. When you click outside the input box, it disappears, which is blur. However, if you click out of the input onto the search results, it remains, and stays up until you click somewhere outside of the input box or the search results. This I have no idea how is done. Is there a way to know what element a user clicks on? Anyone have thoughts/guidance for this? I'm using jQuery as a framework if that helps? Hello everyone. I need some guidance on what might be the best way to tackle this problem. I am creating a page for work. It is a local html site that will be used to show different "questions" in the form of a number of different <div>'s. Currently, the page is set up to have two initial questions that are always visible. The first question asks the user to select from 9 checkboxes and the second asks to choose between 7 radio buttons. Choosing both calls on an onclick javascript event called setup page. This is where I need the help. There are currently 25 hidden <div>'s on the page. The premise is that, depending on your answers to the first two questions, some of those 25 divs become visible for the user to answer those questions only. Currently, when that setup page onclick event is called, the setup page function goes through each of the 25 questions and says Code: pseudocode: if q1_a.checked || q1_b.checked && q2_a.checked, show question 3 div if q1_c.checked && q2_b.checked, show question 4 div etc... I'm thinking there's got to be a better way. This page is becoming unruly and hard to troubleshoot with so much javascript/variables. I was thinking about using case statements but with so many variable options between those two first questions I think there would be too many cases. Any suggestions on another way I can go about this? Thanks!! In my previous post, I asked how to create a function that would allow me to add to or subtract from a maxvalue, and thankfully I got help regarding that. However, something's got me stumped for the past few weeks that I still can't resolve... This is the relevant code: Code: <html> <head> <script type="text/javascript" language="Javascript"> function chgAdv(btn) { var form = btn.form; var newqty = parseInt(btn.value); var maxqty = form.maxqty; var maxadv = form.maxadv; if (eval("btn.checked") == true && parseInt(maxadv.value) + newqty <= 15) { var newmax = parseInt(maxqty.value) - newqty; var newadv = parseInt(maxadv.value) + newqty; } else if (eval("btn.checked") == false && parseInt(maxadv.value) - newqty >= 0) { var newmax = parseInt(maxqty.value) + newqty; var newadv = parseInt(maxadv.value) - newqty; } else { var newmax = parseInt(maxqty.value); var newadv = parseInt(maxadv.value); } if ( newadv >= 0 && newmax >= 0) { maxqty.value = newmax; maxadv.value = newadv; } } function chgDis(btn) { var form = btn.form; var newqty = parseInt(btn.value); var maxqty = form.maxqty; var maxdis = form.maxdis; if (eval("btn.checked") == true && parseInt(maxdis.value) + newqty <= 10) { var newmax = parseInt(maxqty.value) + newqty; var newdis = parseInt(maxdis.value) + newqty; } else if (eval("btn.checked") == false && parseInt(maxdis.value) - newqty >= 0) { var newmax = parseInt(maxqty.value) - newqty; var newdis = parseInt(maxdis.value) - newqty; } else { var newmax = parseInt(maxqty.value); var newdis = parseInt(maxdis.value); } if (newdis >= 0 && newmax >= 0) { maxqty.value = newmax; maxdis.value = newdis; } } </script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <form name="f1"> <b>Advantages</b> (<input type="text" class="RO" name="maxadv" value="0" />)<br /> <input type="checkbox" class="RO" name="Absolute Direction" onclick="chgAdv(this);" value="2" />Absolute Direction<br /> <input type="checkbox" class="RO" name="Allies" onclick="chgAdv(this);" value="3" />Allies<br /> <hr> <b>Disadvantages</b> (<input type="text" class="RO" name="maxdis" value="0" />)<br /> <input type="checkbox" class="RO" name="Antisocial" onclick="chgDis(this);" value="2" />Antisocial<br /> <input type="checkbox" class="RO" name="Ascetic" onclick="chgDis(this);" value="4" />Ascetic<br /> max quantity: <input type="text" readonly class="RO" name="maxqty" value="40" /> </form> </body> </html> styles.css is simply: Code: input.RO { border: none; width: 40px; text-align: center; } As it stands, it works as long as the total value is less than or equal to the limit (for advantages it's 15, for disadvantages it's 10). However, the moment it goes past the limit -- for instance, if you change Antisocial's value from 2 to 7, or if you add several additional checkboxes with any values -- the whole thing starts going crazy; specifically, maxvalue doesn't revert to 40 when the all the checkboxes are unchecked. What I'd want to happen is that the user cannot select or apply advantages or disadvantages if the maximum advantage is greater than 15 or the maximum disadvantages is greater than 10. I'm thinking that a button for adding disadvantage points to/subtracting advantage points from the maxvalue (that becomes disabled if the user goes past his limit on advantages/disadvantages) is useful at this point, but I'm really stumped on the implementation of the idea. An alternate idea of mine is that all other advantage/disadvantage checkboxes would become disabled if their value is greater than the remaining number of points, but I don't know how that is possible either. Any and all help is greatly appreciated. Putting some javascript into a pdf file and can't get this to work out right. Basic setup: Field X = checkbox; Field Y = numeric input field; Here's what should happen (increment is toggled): Y has a value. When X is checked, Y is increased by 5. When X is unchecked, Y goes back to its original, unmodified value. Here's what really happens (increment is subtracted): Y has a value. When X is checked, Y is reduced by 5. When X is unchecked, Y is reduced by 5 again. Anytime X is clicked on, Y is reduced by 5 yet again. Here's the code: Code: function check_box(X, Y){ if(X.checked == true){ Y.value += 5; } else { Y.value -= 5; } Any help or advice is greatly appreciated. was wondering if it made sense to optimize the comparison order inside an if() statement if one of the comparison targets is a function call eg: Code: var a = true, b = function() {return false;}; if (b() || a) {...} if (a || b()) {...} would the second statement run faster because it would theoretically never need to call b() in this situation? can the eval order be relied on? does this depend on the js engine/internal optimizations? thanks, Leon Hello all, As this is an active javascript programming forum...i'm really hoping i'll get a solution for my problem...please help me out.... I have a html: text inside a logic:iterate block like this.. <logic:iterate name="AllocationsForm" property="newSearchAllocations" id="allocations" indexId="myindex" > <html:text name="allocations" property="newStartDate" indexed="true" onblur="allocationTest(this, " + myindex + ");" /> </logic:iterate> i want to write a onblur event for this text field inorder to validate it. Is it the correct way i wrote or something i need to make a change in the above code inorder for the javascript to work. Can we really able to write a onblur event for a indexed text field that is inside a logic:iterate block. please let me know. Thanks in advance. Hi, I am using a wordpress jquery based plugin. TheThe Sliding panels. You can see the jquery he - http://www.get-me-heard.com/wp-conte...jquery.ttsp.js The panel currently auto-opens when the page loads. Which is what I want, however I would like it so that once a user has clicked to close the panel, for it to stay closed even when navigating pages, until they decide to click open again. I would also need the cookie flushed when they leave the website so that the next time they return the panels auto open again. Essentially I just dont want the users to have to click close every time they navigate throughout the website if they don't want to see the panels. NOTE: The plugin has several panels which you can choose from. I am only using the left panel and the right panel. They are all controlled within the jquery. Thanks! Hey there. I'm creating my web-site now. I'm planing to have some slide/object change by itself. I know Java Script perform this task. I have get the code and apply to the image I want. It works. My idea is each of my image upper left corner have the representative number of the slide. When the slide1 go to slide 2 automatically, the number will change colour from 1 to 2. When the user click the number, the slide will change according to the number. Let said I have 3 slide. Each slide and have no 1 ,2 ,3 move forward, move backward icon on the upper left corner. I have slide1 ,slide2, slide3 change by itself within 2.5 second. When the user go thru slide 1, slide 2 and till slide3, user find out he/she interested on slide 1 and by clicking 1 on the upper left corner, the slide will change back to slide 1. Something like showing on the home page at www.easyjet.com.EN or http://www.thomascook.com/ . Both home page show a slide. This is what exactly I want. I learn HTML code before but not Java Script. I have borrow many Java Script reference book. but no idea how to inside the number on the upper left corner on each slide and how the number work accordingly with the slide. So sorry if you can't understand what I was talking about. But, anyone who understand me or have any idea how can I mortify the Java script to what I want. Just answer or email me to trista_soo@yahoo.com or facebook me on sym107@hotmail.com. Thanks for your time and efforts. Your help I really appreciate. Cheers! I don't understand the logic of Break, Return False, Return True. It was never really covered in our college class, and I see everyone using it. I got an A in the class, if that 'proves' that I really tried to apply myself. NOTE: I understand what the function is doing. I just don't understand WHEN to use break, return false or return true if the the translator can determine the conditional statements. PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } else return false; } if the the translator can determine the conditional statements, why not write it like this: PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; // what's the point for the 'break'? Won't the rest of the code be ignored since it passed the first condition? } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } // why not leave the last else out? is it just a 'safety' catch, in case something other than true or false is inputted? else return false; // what's the point? } Questions: Why use return true, if the translator knows it's ture? Why use "return false" if the translator knows it's false and the alert window has already gone up? why not use "break" to stop the code? Why use the "return false" at the end "else" statement? Hi pals, I am really tired in this problem of event keyup. I given same in my keyup function like: $(document).ready(function () { alert("GGG"+parseInt(jQuery.browser.version)); //To display test value working $("#find_text").keyup(function(e) { if(e.which == 13) { alert('Enter key was pressed'); //enter Here alert("FFF"+parseInt(jQuery.browser.version)); //Here got Error } }); }); I got Error : jQuery is not defined alert("FFF"+parseInt(jQuery.browser.version)); I use keycode,which , but no help, It's Work nicely in Chrome Browser but not in FF. Please give a Solution reply ASAP, I am really Tired.The code enter the Condition But that jQuery part make error. Thankfully Anes P.A hi i found some javascript on the internet that seemed to do what I wanted to achieve but cannot get it to work when the mouse is over the image it should show a bigger image. I've used the internet explorer debugger and there seems to be problem in the code, but i dont understand javascript programming. Im a php man myself. all the script is enclosed in this page http://www.web-malta.com/testimage.html if you put the mouse over the photo you will see there is an error on the page. hope someonbe can help. cheers I really don't even know if I'm posting this question in the right section. I am using javascript to make a drop-down menu for a website. I really don't have any experience with javascript. The problem I'm having is that when I go to the page, the drop down menu is already dropped down. If you then hover over it, it pulls up and then hover over it again and it drops back down. It works fine after the initial glitch. Didn't know if anyone had run into this problem or if I'm just being stupid, here is the code that I'm using. Thanks in advance for any help. <a href="/mainpage.html" onMouseOver="show()" onMouseOut="hide()" ><img src="pic.png" border="0" width="90" height="24"></a></br> <div id="div1"><a href="/notherpage.html" onMouseOver="show()" onMouseOut="hide()" ><img src="pic2.png"><br><a href="/notherpage2.html" onMouseOver="show()" onMouseOut="hide()" ><img src="pic3.png"><br><a href="/lastpage.html" onMouseOver="show()" onMouseOut="hide()" ><img src="pic4.png"></div> <script type="text/javascript"> function show(){ document.getElementById("div1").style.display="block"; } function hide(){ document.getElementById("div1").style.display="none"; } </script> Hey guys! :-) How do i translate this javascript sentense in to "real working" javascript. PHP Code: if(a1 "its a box/this is just for info" = got a value written){ c1 also a box = disabled; } How do i write this so it works? My first thought was: PHP Code: if(document.GetElementById(a1).value = true){ document.GetElementById(c1).disabled = true; } But cant get it working - is there somebody there could help me? :-) //Morten Larsen // Denmark. Hi, I have html variable as below: html += "<div class='quickSearchResultDivUnselected' style='width:" + divWidh + "px;max-width:" + divWidh + "px'><a href='#' OnClick='javascript:test('" + title + "')>" + title + "</a></div>"; The problem is with OnClick='javascript:test('" + title + "')>". How to make this link as <a OnClick='javascript:test("VALUE")'></a>, because now as result it looks like this and it doesn't work: <a OnClick='javascript:test('value')'></a>; So the problem is with ' and ". How to make it correctly in this long line of html variable ? Please help. For help I m putting it all together ..copy and paste and run ... I used 2 browsers IE and mozilla... you fill find the problem ...I know some one will definatly solve my problem ..... Code: <script type="text/javascript" language="JavaScript"><!-- function ManageTabPanelDisplay() { var idlist = new Array('tab1focus','tab2focus','tab3focus','tab4focus','tab1ready','tab2ready','tab3ready','tab4ready','content1','content2','content3','content4'); if(arguments.length < 1) { return; } for(var i = 0; i < idlist.length; i++) { var block = false; for(var ii = 0; ii < arguments.length; ii++) { if(idlist[i] == arguments[ii]) { block = true; break; } } if(block) { document.getElementById(idlist[i]).style.display = "block"; } else { document.getElementById(idlist[i]).style.display = "none"; } } } //--></script> <style type="text/css"> .tab { font-family: verdana,sans-serif; font-size: 12px; width: 100px; height:30px; white-space: nowrap; text-align: center; border-style: solid; border-color: #9a9a9a; border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 0px; padding-top: 5px; padding-bottom: 5px; padding-left:3px; padding-right:3px; cursor: pointer; } .tabhold { background-color:#9a9a9a; /*color of menu #666666*/ color: white; } .tabfocus { background-color:white; color: #666666; } .tabcontent { font-family: sans-serif; font-size: 12px; height: 320px; border-style: solid; border-color: #9a9a9a; border-width: 1px; padding-top: 15px; padding-left: 10px; padding-right: 10px; overflow-y:scroll;overflow:-moz-scrollbars-vertical; } </style> <table border="0" cellpadding="0" cellspacing="0" > <tr > <td> <div id="tab1focus" class="tab tabfocus" style="display:block; padding-top:10px; height:39px;" > Overview </div> <div id="tab1ready" class="tab tabhold" style="display:none; padding-top:10px;height:39px;"> <span onclick="ManageTabPanelDisplay('tab1focus','tab2ready','tab3ready','tab4ready','content1')" >Overview </span> </div> </td><td > </td><td> <div id="tab2focus" class="tab tabfocus" style="display:none;"> Dimensions <br /> & Details </div> <div id="tab2ready" class="tab tabhold" style="display:block;"> <span onclick="ManageTabPanelDisplay('tab1ready','tab2focus','tab3ready','tab4ready','content2')">Dimensions <br /> & Details</span> </div> </td><td > </td><td> <div id="tab3focus" class="tab tabfocus" style="display:none;"> Quantity <br /> & Constructions </div> <div id="tab3ready" class="tab tabhold" style="display:block;"> <span onclick="ManageTabPanelDisplay('tab1ready','tab2ready','tab3focus','tab4ready','content3')">Quantity <br /> & Constructions</span> </div> </td> <td > </td><td> <div id="tab4focus" class="tab tabfocus" style="display:none;"> Customization <br /> Options </div> <div id="tab4ready" class="tab tabhold" style="display:block;"> <span onclick="ManageTabPanelDisplay('tab1ready','tab2ready','tab3ready','tab4focus','content4')">Customization <br /> Options </span> </div> </td> <td width="100%"> </td><td> </tr> <tr > <td colspan="8" > <div id="content1" class="tabcontent" style="display:block;"> content1 </div> <div id="content2" class="tabcontent" style="display:none;"> content2 </div> <div id="content3" class="tabcontent" style="display:none;"> content3 </div> <div id="content4" class="tabcontent" style="display:none;"> content4 </div> </td></tr> </table> please help thanks Hi, so I thought I could mash two bits of code together like this: Code: <div><label for="or">From:</label> <select name="or" style="width:200px; float: right" id="or" onchange="populate(this)"> <option value="Choose">Select your starting point</option> <option value="Guatemala">Guatemala City</option> <option value="Xela">Quetzaltenango</option> <option value="Antigua">Antigua</option> <option value="Rio Dulce">Rio Dulce</option> <option value="Coban">Coban</option> </select></div> <div> <label for="de">To:</label> <select name="de" style="width:200px; float: right" id="de"></select> </div> <div> <br> <input type="button" onclick="searchLocations()" value="Show route" /> </div> </div> </div> </div> <script type="text/javascript"> if (GBrowserIsCompatible()) { var gpolylines = []; function populate(o) { d=document.getElementById('de'); if(!d){return;} var mitems=new Array(); mitems['Choose']=['']; mitems['Guatemala']=['Select Destination','Antigua','El Rancho junction','San Pedro La Laguna','Panajachel','Coban','Rio Hondo','Chiquimula','Esquipulas','Copan Ruinas (Honduras)','La Ruidosa junction','Rio Dulce','Flores (via Rio Dulce)','Puerto Barrios','Flores (via Coban)','Quetzaltenango']; mitems['Xela']=['Select Destination','Guatemala City','Antigua','Chichicastenango','Huehuetenango','Panajachel','San Pedro La Laguna']; mitems['Antigua']=['Select Destination','Guatemala City','Quetzaltenango','Escuintla','Monterrico','San Pedro la Laguna','Panajachel','Chichicastenango']; mitems['Rio Dulce']=['Select Destination','Guatemala City','Flores','Lanquin','Coban','Puerto Barrios']; mitems['Coban']=['Select Destination','Lanquin','Rio Dulce','Laguna Lachua NP','Flores']; d.options.length=0; cur=mitems[o.options[o.selectedIndex].value]; if(!cur){return;} d.options.length=cur.length; for(var i=0;i<cur.length;i++) { d.options[i].text=cur[i]; d.options[i].value=cur[i]; } } function searchLocations() { var found = false; var from = document.getElementById('or').value; var to = document.getElementById('de').value; for (var a = 0; a < gpolylines.length; a++) { if (gpolylines[a].myname.toLowerCase() == to.toLowerCase() && gpolylines[a].mycategory.toLowerCase() == from.toLowerCase()) { found = true; gpolylines[a].show(); } } if ( ! found ) alert("No matches found. Please check your spelling or refine your search."); } the first bit, which populates the select boxes works fine, as you can see here but the second bit (the searchLocations function) is giving me a bit more trouble. What it's supposed to be doing is saying that if there's a polyline whose category xml attribute matches the text from the 1st select box AND whose name attribute matches the 2nd select box, it should be shown. The methods have worked in the past, there are no errors reported - the only problem is I get the alert regardless of which option I am selecting. I suspect that the problem is in the if (gpolylines[a].myname.toLowerCase() == to.toLowerCase() && gpolylines[a].mycategory.toLowerCase() == from.toLowerCase()) line, as it's the only one that I really came up with on my own... or can I not get the "or" and "de" elements in that way? Or is it something else entirely? thanks in advance for any suggestions... this is coder javascript : textneu = textneu.replace(/haha/,"<img src='../images/21.gif'>"); Result: <img src='../images/21.gif'> why? i want result is : <img src='../images/21.gif'> |