JavaScript - Idiot Proof Explanation? / Easy Question
hi guys, l am currently studying web technologies on a part time basis, and learning javascript.
l got a very useful website, but still l dont understand certain things like this one: Quote: When a function has performed an assignment, it may provide a value that other functions would need, for any reason you judge necessary. When a function produces a value an makes it available to other functions, such a function is said to return a value. To define a function that returns a value, just before the closing curly bracket, type the return keyword followed by the value that the function returns. The value should be a valid and expected type. Because JavaScript is not a type-checking language, meaning it hardly checks anything you are doing, it is your responsibility to make sure that your function returns the right value. Here is a function that returns a value: function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } Even if a function returns a value, it can still be called like any other function. If a function returns a value, its name can be assigned to a variable to give a value to, or change the value of, the variable. Here is an example: Code: <Script Language="JavaScript"> function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } function displayArea() { var l, h, rectArea; l = 52.05; h = 46.55; rectArea = rectangleArea(l, h); document.write("The area of the is ", rectArea); } </Script> <Script Language="JavaScript"> displayArea(); </Script> what l understand from the code is, l have 2 functions, first function is a return function and has got a value, but where is this value going? l mean what's happing with it? l mean l cant see this return value in the second function, why? l ma also not understand why we need to have 2 functions, why not just 1 ?/?? can someone explain me one by one the lines maybe? thanks Similar TutorialsHi - Firstly, my hands up - i don't have a clue what i am doing, but hey i am trying.. I am having issues with my site (that i have been creating myself and learning as i go along in dreamweaver), i would like to have three images that rotate, i found this page and followed the instructions - http://www.communitymx.com/content/a....cfm?cid=651FF, this worked fine for my first image as it was just a matter of copying and pasting the script but i am trying to apply it to the others as i want more than one to rotate, i honestly have no clue to figure out the script and am just guessing - here is what i have done so far - http://secretwardrobe.co.uk/NewPics/newpage.html images are changing but they are referring to my first array... any suggestions would be greatly appreciated - thanks for your time:-) hi guys, l am trying to learn javascript, although certain things seem to be easy to understand, l still have plenty of questions. so l was reading some articles on w3schools and l was an interesting table about operations. http://i1087.photobucket.com/albums/...kesnosense.png if you check above's table you will see at the increment section as well decrement , you will see that in some cases the x and y dont have the same value, now l was just wondering, what is the difference between x=++y and x=y++ ? Hey! Sorry if this is a very remedial question, but I'm trying to make a table, of even and odd numbers organized in two columns (with as many rows as it takes to get through the sequence) and I'm having an issue with my code, obviously - haha. I'm still slightly new.. here is my code Code: document.write('<table>'); for (i=1; i < 50; i++) { document.write("<tr>"); for (j = 0; j < 50; j++) { if (j % 2==0) { document.write('<td>' + j + '</td>'); } else { document.write('<td>' + i + '</td>'); } } } document.write("</tr>); document.write("</table>"); Any help would be greatly appreciated! This is javascript that turns your current url into a tinyurl. How would I go about getting this to open into a new tab instead of the same tab? Code: javascript:void(location.href='http://tinyurl.com/create.php?url='+encodeURIComponent(location.href)) hey i just started javascript, i have a little syntax question: <HTML> <HEAD> <script type="text/javascript"> function ifPrime(input) { thesqrt = Math.sqrt(input) for(a = 2; a <= thesqrt; a++) { if(input % a == 0) return false; } return true; } function nthPrime(input) { for(i = 2; i <= input; i++) { if( ifPrime(i) ) { document.write(i); document.write("<br />"); } } } </script> </HEAD> <BODY> Enter how many primes <INPUT TYPE=TEXT NAME="userInput" SIZE=6> <INPUT TYPE=BUTTON VALUE="Do it" ONCLICK="value = userInput"> <br /> <br /> <script type="text/javascript"> nthPrime(value) </script> </BODY> </HTML> i would like it post 2 3 5 7 when i type in 10 and press the button. how do I do that? any other programing tips would be appreciated I'm very very new to javasctipt/html programming and have come upon a stump.Let me explain Code: <form name="for1"> <input type="radio" name="fr1"value="1" > 5 <br> <input type="radio" name="fr1"value="2" > Όσα τα pixels<br> <input type="radio" name="fr1"value="3" > Δεν υπάρχει όριο<br> </form> <input type="button" value="Εκχώρηση Απάντησης" onClick="temp=document.for1.fr1.value"> What i was hoping with this is to store the value of the selected radio in a variable.The variable is declared at the beginning (not shown here).I would appreciate greatly if i'm on the right track to please tell me how to make this work and if possible not create functions and other complicated procedures as it will take me out of my "learning curve".Thanks in advance I am just learning javascript. I am playing around with different html elements and document.write. I have a HTML page with elements h1, h2, h3, and h4. I want to write something (in this example "found it") next to the elements. <head> <script type="text/javascript" language="javascript"> var headerList = getElementsByTagNames('h1,h2,h3,h4'); if (headerList) { document.write("Found it!"); } </script> </head> <body> <h1> </h1> <h2> </h2> <h3> </h3> <h4> </h4> </body> Why doesnt this work? I am assuming my logic is flawed? I don't know much about programming but I can't figure out how this works and I want to implement it on my site. I want to copy the code from this page sdapc.com/billing/pay-online.html and implement something similar. I understand that the form uses post and then goes to form-action.php and the information is verified using the script.js, but how is the information sent anywhere. I mean, I don't see any reference to any script that compiles the data and sends it to the billing department. How do they get the info??? Thanks in advance. P.S. If anyone knows of similar scripts or methods out there of doing this I'm all ears (or eyes) This would just be temporary until full automation is developed. For now, I don't mind entering their info into PayPal myself. I know it's possible to do this in php, but what would the javascript equivalent be? Code: var scripts = new Array(); scripts[] = "PHP"; scripts[] = "ASP"; scripts[] = "JavaScript"; scripts[] = "HTML"; But this obviously doesn't work. And just to clarify, I mean the ability to add an array whenever I want to in my page instead of having to name the array like... Code: var scripts = new Array(); scripts[0] = "PHP"; scripts[1] = "ASP"; scripts[2] = "JavaScript"; scripts[3] = "HTML"; I'm using it for such as in a form: Code: <input type="text" name="formarray[]"> <input type="text" name="formarray[]"> Thank you in advance. Hey guys, this should be a very easy fix for someone who knows Javascript (which I don't). The following code allows the class "ul.slideshow-nav li" to control the navigation of the slideshow, I want to adjust this script to allow "ul.slideshow-nav li" AND "ul.slideshow-nav2 li" to do this, possible? Code: $slideshow = { context: false, tabs: false, timeout: 8000, fx: 'scrollLeft', slideSpeed: 900, tabSpeed: 900, init: function() { this.context = $('#content-slideshow'); this.tabs = $('ul.slideshow-nav li', this.context); this.tabs.remove(); this.startSlideshow(); }, startSlideshow: function() { $('div.slideshow > ul', $slideshow.context).cycle({ fx: $slideshow.fx, pager: $('ul.slideshow-nav', $slideshow.context), pagerAnchorBuilder: $slideshow.startTabs, befo $slideshow.Tabactive, timeout: $slideshow.timeout, speed: $slideshow.slideSpeed, fastOnEvent: $slideshow.tabSpeed, pauseOnPagerHover: true, pause: true }); }, startTabs: function(i, slide) { return $slideshow.tabs.eq(i); }, Tabactive: function(currentSlide, nextSlide) { var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context); if(activeTab.length) { $slideshow.tabs.removeClass('on'); activeTab.parent().addClass('on'); } } }; $(function() { $slideshow.init(); }); Help me anyone I am completely stuck. If you do this I shall build a shrine in your honour. In the following code the 'for' loop gets data from the relevant tags of an XML file, then puts them into the 'txt' variable. It then moves onto the next record in the XML file and does the same thing. However, I don't want the contents of txt to be overwritten each time the code loops through each record. Instead I want it to add to txt, then loop around and add the new contents of txt to the previous contents of txt. I tried txt = txt + artist + title + year; but this didn't work. Can anyone explain how to do this? Please note the contents of the variables are strings of text not numbers. Code: x=xmlDoc.getElementsByTagName("CD"); i=0; function displayCD() { for (var i=0;i<x.length;i++) { artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue); txt= "Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year; } document.getElementById("showCD").innerHTML=txt; } I need to change the source of an image but I can't seem to figure out how. I believe that I have the correct syntax, but it does not seem to be working. Maybe I am typing it in wrong or something. I try to test my javascript by typing the script: <strong>javascript:document.getElementById('img1').src='./images/image1.jpg';</strong> into the address bar. Nothing happens. I also try to test it out by putting this on the page, but once again, nothing happens. Code: <script type="text/javascript"><!-- document.getElementById('im1').src="./scripts/'.$upload_image.'"; //--></script> This is what my HTML code looks like: Code: <img src="./images/temp_wizard_picture.gif" alt="" style="padding:4px; background:#fff; border:1px #bbb solid;" id="img1" /> Can somebody please fill me in on what I am doing wrong. Thank you. Short version at bottom Need to add an array before an array to init thousands of arrays. Mostly worked out, just need to add an array before an array. ........Please check it out. Really want to finish this tonight or tomorrow. Alright, so two days later I finally have this portion working but its only because of the awesome people of this forum and unforunately these people thought I had some half decent grasp of javascript (which I don't) and so their answers were meant to solve my problem but each time I was left with no idea how to repeat what they did. So, I've learned a lot of extra stuff that I really could have done without in the effort to try and understand what they did. This is all well and good because I'm much farther than I am had I gone it alone (so thank you!) but please, anyone that posts an answer, could you try and explain a bit of how I might use your solution again. For example, today I was confused for about an hour because I didn't understand how [CODE]var newArray=[], a, i=0;CODE] worked but only after staring at it long enough and not finding anything on google related to "values inputed after array initialization" did I finally realized that these were not params of a new array but just new variables. Code: var alphabetArray =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','all1']; var a; var i=0; while(a=alphabetArray[i++]){ eval('var _img_'+a+' = []'); eval('var _h_'+a+' = []'); eval('var _r_'+a+' = []'); eval('var _m_'+a+' = []'); eval('var _yt_'+a+' = []'); } alert(_h_all1) and its working perfectly but I somehow I need to add Code: var pageNum = ['','p2_','p3_','p4_','p5_','p6_','p7_','p8_','p9_','p10_'] need to add pageNum to every alphabetArray so... _img_a = [],_img_p2_a = [], img_p3_a = [].... _img_p10_a = [] repeated for every variable in alphabetArray. _yt_p10_all1=[] Super short version Code: var alphabetArray =[letters/numbers]; var a; var i=0; while(a=alphabetArray[i++]){ eval('var _img_'+a+' = []'); } // works now I need to add 10 page prefixes before each var in alphabetArray Code: var pageNum = ['','p2_','p3_','p4_','p5_','p6_','p7_','p8_','p9_','p10_'] need to add pageNum to every alphabetArray so... _img_a = [],_img_p2_a = [], img_p3_a = [].... _img_p10_a = [] repeated for every variable in alphabetArray. _yt_p10_all1=[] Please explain how this might be possible in a way that your dog might understand. Hi I am currently reading through an example in my book and I'm stuck on a function which is passed 'that' as it's parameter. What I'll do is type the code and then my question's so I can explain myself a little better. Code: Code: <head> <script type="text/javascript"> var myImages = new Array("usa.gif","canada.gif","jamaica.gif","mexico.gif"); function changeImg(that) { var newImgNumber = Math.round(Math.random() * 3); while (that.src.indexOf(myImages[newImgNumber]) != -1) { newImgNumber = Math.round(Math.random() * 3); } that.src = myImages[newImgNumber]; return false; } </script> </head> <body> <img name="img0" src="usa.gif" border="0" onclick="return changeImg(this)" /> <img name="img1" src="mexico.gif" border="0" onclick="return changeImg(this)" /> Now what I cannot grasp is how does 'this' relate to 'that'? You return to the onclick handler the function name with the parameter as (this). The function parameter is (that) and I see that 'that' is used within the conditional part of the while statement. What I do not understand is why is 'this' used as the parameter in the onclick event's function, when in the actual function it is 'that'. I hope I have explained myself well enough and I look forward to any responses you may have. Thank-you. LC Code: <script type="text/javascript"> var K_Thomas = "Kurt Thomas" , E_Turner = "Evan Turner" , P_George = "Paul George" , B_Davis = "Baron Davis"; var G_Vasquez = "Greivis Vasquez" , G_Dragic = "Goran Dragic" , K_Love = "Kevin Love" , V_Carter = "Vince Carter"; var L_Odom ="Lamar Odom" , JasonSmith="Jason Smith" , D_Jordan = "DeAndre Jordan" , J_ONeal = "Jermaine O'Neal"; var playername = prompt("Find Player Salary","First Name Last Name"); if (playername = K_Thomas) { alert("Kurt Thomas - $1.3 M 2012 Bucks"); } else if (playername = E_Turner) { alert("Evan Turner - $4.48 M 2012++ Bucks [$2 M Paid 2013]"); } else if (playername = P_George) { alert("Paul George - $1.71 M 2012++ Bucks"); } else if (playername = B_Davis) { alert("Baron Davis - $13.44 M 2012+ Bucks [$5.44 M Paid 2012]"); } else if (playername = G_Vasquez) { alert("Greivis Vasquez - $1.02 M M 2012++ Bucks"); } else if (playername = G_Dragic) { alert("Goran Dragic - $1.97 M 2012 Bucks"); } else if (playername = K_Love) { alert("Kevin Love - $3.88 M 2012 Bucks"); } else if (playername = V_Carter) { alert("Vince Carter - $17.52 M 2012 Bucks [$2 M Paid 2012]"); } else if (playername = L_Odom) { alert("Lamar Odom - $8.2 M 2013 Bucks [$4 M Paid 2012 & $4 M Paid 2013]"); } else if (playername = JasonSmith) { alert("Jason Smith - $1 M 2012 Bucks"); } else if (playername = D_Jordan) { alert("DeAndre Jordan - $10.1 M 2015 Bucks"); } else if (playername = J_ONeal) { alert("Jermaine O'Neal - $0.3 M 2012 Bucks"); } else { alert("Player Not Found."); } </script> So, whenever I run this code it either always shows up with Kurt Thomas's alert or with all of them. (I've tried using both document.write and alert) I don't know what I'm doing wrong with the conditions, but I'm new to javascript and it could be something simple. The Mozilla error console didn't find anything. Just for clarification I want it to happen so that when I type in a player's first and last name it brings up their salary information. Thanks in advance for your help. I don't really understand why a.d() fails in the following, or rather why a.d can't access local vars in a, or how to rewrite this so that it can PHP Code: window.a = (function () { var b = '<br>hello '; var c = function (){ document.write(b+'from c') // in this scope we can access the local vars of a }; c() // this will work and write hello from c document.write(b) // this will work and write hello from b return{c:c} })(); a.d = function () { document.write(b+'from d') // even though as far as I can tell I have added d to the object a, } // this still can't access the local vars of a... why not? how can I change that? a.d() // doesn't work :( a.c() // this works too because we returned c in a's return statement Hey Guys, I have been trying to work out this problem for the last couple of days have having found long way around of doing it but i was wondering why this way isn't working. I have attached to following code but can't seem to get it to show me the average which i am looking for which is 17.2 i have tried a number of things and mostly it is just curiosity as i have also pasted the deadline for this piece of work and was wondering is someone would be able to enlighten me on why its just giving me the output. 'The average height is NaN' (Not a Number) I know that it is probably something stupid like iam not declaring something as numeric or it might be that the whole think is wrong. I would very much appreciate any-help that people can give me but please don't direct me to another post. Code: <HTML> <HEAD> <TITLE> Plant Experiments </TITLE> <SCRIPT LANGUAGE = "JavaScript"> var plantHeights = [15,16,17,18,19]; var plantNumbers = [2,1,5,4,3]; var plant = new Array(5); for (var plantt = 0; plantt < plant.length; plantt = plantt + 1) { plant[plantt] = plantHeights[plantt] * plantNumbers[plantt]; } document.write('The heigh is ' + (plantNumbers[plant] + plantHeights[plant]/plant)); </SCRIPT> </HEAD> <BODY> </BODY> </HTML> Many Thanks, Tom I have devised the following constructor based loosely on the observer pattern Code: Observer = function (ConditionIsTrue , codeToExecute){ var observer = this , ConditionWasMet = false , CheckIfReady = function () { if (ConditionIsTrue()) { if(!ConditionWasMet) codeToExecute(); ConditionWasMet = true; } else { if (ConditionWasMet) ConditionWasMet = false; } }; Loop.call(observer , CheckIfReady) observer.speed(1) }; It works fine, no problems that I know of... But the most interesting thing happened when I attached it to my lib and ran it through the http://closure-compiler.appspot.com/home compiler Code: $['Observer'] = function (//...etc... For the first time the compiler has added something to my global scope Code: var i=!1; window.$=function(){//...etc... ... Observer:function(j,k){var h=i;Loop.call(this,function(){j()?(h||k(),h=!0):h&&(h=i)});this.speed(1)} } })(); Can someone explain to me why the var ConditionWasMet had to be exported to the global scope? Was it already in the global scope? That would confuse me considering the observer can be called from multiple instances without conflict... But I don't want any surprise conflicts jumping out at me later, I appreciate the consideration. I am learning javascript from this site, and one of the programs code given is now explained to the point that I can not use it. I don't know how to call it. I would be glad if someone would examine the code to see how it should be applied. Date and Time Scripts Frank hey guys im trying to make something that selects two different images but it keeps selecting two images that are the same, and dont know how to go about it to make it select two different. please help Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/javascript; charset=utf-8" /> <title>Untitled Document</title> <script language="JavaScript"> <!-- var theImages = new Array() theImages[0] = '100.jpg' theImages[1] = '200.JPG' theImages[2] = '300.JPG' theImages[3] = '400.jpg' var j = 0 var p = theImages.length; var preBuffer = new Array() for (i = 0; i < p; i++){ preBuffer[i] = new Image() preBuffer[i].src = theImages[i] } var whichImage = Math.round(Math.random()*(p-1)); function showImage(){ document.write('<img src="'+theImages[whichImage]+'">'); } //--> </script> </head> <body> <div align="center"></div> <table width="996" height="511" border="1"> <tr> <th scope="col"><script language="JavaScript"> <!-- showImage(); //--> </script> or <script language="JavaScript"> <!-- showImage(); //--> </script> </th> </tr> </table> </body> </html> |