JavaScript - Newbie Question; Check String After A For Loop
Function findsometing()
( var myString = 'results' + '<BR>' for (var initiator, test condition,incrementor.) { if (.....) { myString ='some quote'; } else if (.....) { myString += something + '<br>' } else if (....... ) { myString += something + '<br>' } else { myString += ''; } } displayMessage(myString) ) I am a complete newbie to javascript and programming, so excuse any faux pas, but I want to check the myString variable after all the loops in the for loop statement have been completed. For example if nothing was added to the var myString after the completeion of the for loop, i want to display a message that no results found. i tried various attempts of an if statement along lines of } } if (myString = 'results') ....i also tried if myString= ('results' + '<BR>') { displayMessage('no results found') } else { displayMessage(myString) } ) Any pointers as to where i am going wrong? Similar TutorialsHey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response I need to check if a var (lessonnum) is a string that cannot be converted to a number, i.e. "fred" "george" "catalyst" etc. but not "5" "456" or "34564" etc.: Code: var lessonnum = readCookie('lesson'); if ((intro != "12")||((lessonnum != "A STRING THAT COULD BE A NUMBER"))){ do sth. } How could I do that? string check in javascript do suggest me something ....... if you have any two words like , "me" and "you". Hi all, I have three arrays and what I would like to do is to check the present year to see in which array it could be found. If in the cycleA array then we would have document.write("Cycle A"); If in cycleB, then document.write("Cycle B"); etc. If it was just one array then an IF statement within a FOR loop would work, but I'm thinking there could be a more compact way of checking three different arrays instead of having three different FOR and IFs. Any help would be appreciated. George Code: <script> var cycleA = new Array("2011","2014","2017","2020","@023","2026","2029","2032","2035","2038","2041","2044","2047","2050","2053","2056","2059","2062","2065","2068","2071","2074","2077","2080","2083","2086","2089","2092","2095","2098"); var cycleB = new Array("2012","2015","2018","2021","2024","2027","2030","2033","2036","2039","2042","2045","2048","2051","2054","2057","2060","2063","2066","2069","2072","2075","2078","2081","2084","2087","2090","2093","2096","2099"); var cycleC = new Array("2013","2016","2019","2022","2025","2028","2031","2034","2037","2040","2043","2046","2049","2052","2055","2058","2061","2064","2067","2070","2073","2076","2079","2082","2085","2088","2091","2094","2097","2100"); var gpl = new Date(); var whatyear = gpl.getFullYear(); if(whatyear%2 == 0)document.write("Year II"); else document.write("Year I"); </script> Basically I have this: function semaphore(phrase) { var len = phrase.length; var str1 = ""; for (var i=0;i<len;i++) { if(phrase[i]=" ") { phrase[i] = "interval"; } var str = "images/semaphore/" + phrase[i] + ".gif"; str1 = str1 + "<img src='" + str + "'>"; } return str1; } so if phrase = i love coding forums I want all the spaces to be replaced with the text "interval" Thanks Danny I have thought about using the standard email checker pattern but we have had problems in the past so looking to check if the string follows these simple rules. has no spaces, has a character/s at the start followed by an @ then has a character/s followed by a (dot) then another character/s i.e a@a.a aa@aa.aa aaa@aaaaaaaaa.aaa i am not sure how the standard pattern works but feel that it may be to complex which causes some email address to fail. Here is the code i am trying. Code: var str="اسدف"; patt=/[\u0600-\u06FF\u0750-\u077F]/; result=patt.test(str); document.write("<br />Returned value: " + result); </script> This should return true, as the string in the str variable is indeed Arabic character. But it returns false. Need some help here on this.. Thanks in Advance This is what I am aiming for. I want to be able to click on a word that would then write text in a specified area. This is what I came up with but it writes it at the top of the page and only for a moment then disappears.... These are my clickable words Code: <a href="#textarea" onClick="MathHomework('W2')">Week 2</a> <a href="#textarea" onClick="MathHomework('W3')">Week 3</a> the anchor named 'textarea' is where I want the output to be written which is placed on my html page. This is my newbie script Code: <script type="text/javascript"> function MathHomework(week) { if (week == "W2") document.write("<p>Review Chapter Twelve</p>"); else if (week == "W3") document.write("<p>Final Chapter Review Test</p>"); } </script> <a name="textarea"> (text area for javascript output)</a> so is my onclick syntax wrong maybe? and yes I have read rule#5 and I am trying to understand my mistakes. Thank you in advance Hi Everyone I'm learning Javascript with varying degree's of success but am very much in the early phase. My question/problem is this: [ICODE]<!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/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <script language="javascript" type="text/javascript"> var degFahren = new Array(212, "string data", -459.67); var degCent = new Array(); var loopCounter; for(loopcounter =0; loopCounter <= 2; loopCounter++){ if(isNaN(degFahren[loopCounter])){ alert("Data " + degFahren[loopCounter] + " at array index " + loopCounter + " is invalid"); continue; } degcent[loopCounter] = 5/9 * (degFahren[loopCounter] -32); } for (loopCounter =2; loopCounter >=0; loopCounter --){ document.write("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees Fahrenheit"); document.write(" which is " + degCent[loopCounter] + " degrees centigrade<br />"); } </script> </body> </html> /ICODE] The following code does not return any errors in my browser but I'm confused since I'm sure I should be seeing an alert box telling me that there is invalid data entered bu this doesn't happen, also as far as I can make out I should only be receiving an error for the 2nd loop but there seem to be an error message returned for all 3 loops?? This is all possibly correct but I'm learning from a book and I have no-one to ask these mundane questions to. Any help would be greatly received. Sara I'm in a computer coding class, and I'm having a bit of trouble with Javascript. I'm not looking for anyone to do my homework for me, but any hints you could give me as to why this code isn't working would be greatly appreciated. Basically, I need an empty text box where the user enters their guess as to my age. The user enters their guess, and then gets an answer as to whether their guess is right, too high, or too low. The page looks right, but I can't get the function to work. Thank you... <html> <head> <script> function howoldami(age) if(age=23) { alert("You guessed right!"); } else if { (age>23) alert("I'm not that old."); } else { alert("I'm not that young."); } </script> </head> <body> <form> Guess My Age!<input name="age"><br> <input type=button value=Guess! onclick="howoldami(age.value)"> </form> </body> </html> Hello, I'm in the process of getting a better understanding of javascript (instead of just copying and pasting JQuery script onto my site). So I have a(dumb?) question. Can I name a var what ever I want? For example, var car_name("Ford Escape"); Or am I misunderstanding this? THanks. Thinking in Objects 4 objects w/constructors/overloads min 3 properties on each min 3 methods total was just wondering if anyone had any good links or tutorials to get me started on my first javascript assignment Hi! Im new to the world of javascript, so bare with me. It might be a stupid question but i got a input form, i know how to get the data in but i need to single out one of the numbers. the input looks like: xxxxxx-xxxx I've tried almost everything that i can think of... A nudge in the right direction would be appreciated. I've had several problems with document.getElementById() and haven't been able to find a reason for this. I've searched these forums and I'm sorry if I've still managed to miss if there's an answer to this, as it seems to me to be a basic question. When I use getElementById() it keeps returning as 'undefined' in both FF och IE. I've had to use getElementsByName() instead, and since I only have one element, I have to write element[0] to make it work which seems rather redundant. This is parts of my JS code: Code: var postName = document.getElementsByName("postName"); //getElementById doesn't work here var postText = document.getElementsByName("postText"); //...nor here. if (postName[0].value == "" && postText[0].value == "") { document.getElementById('feedback').innerHTML = "Some text."; //For some stupid reason getElementById works here! } And the HTML looks somewhat like this: Code: <div class="addName" id="addName"> <input type="text" name="postName" id="postName" class="textBox"></input> </div> <div class="addText" id="addText"> <textarea class="textarea" name="post" id="postText"></textarea> </div> <div class="feedback" id="feedback"></div> I don't see why it wouldn't work, but this isn't the only time it's happened so I must be missing something. Anyone who can enlighten me as to what that is? I have yet to have my ah-ha moment with Javascript but I think its coming soon. there are small things that seem to be baffling me and Im hoping posting to the forums to get help from people of limitless wisdom. What exactly does the "|" mean in this code? or I should say what does it do? Code: document.template.Aprod_string.value = document.template.Aprod_string.value + "|" + string_recode(Aprod_array) ; Hi, I am just beginning out with Javascript and trying to complete a simple login page. Clicking on the Login button should bring up a message 'Access authorised' or 'Access denied' in the 'output' div of the page. Unfortunately only 'Access Denied' displays, even if the password entered is correct. There are no messages displayed in the Error Console. Any advice greatly appreciated! Code: <html> <head> <title>Function Example</title> <script language="javascript" type="text/javascript"> function isAccessAuthorised(enteredPassword) { var result; if(enteredPassword=="password") { result=true; } else { result=false; } return result; } function buttonClicked() { var isAuthorised = isAccessAuthorised("enteredPassword").value; if(isAuthorised==true) { document.getElementById("output").innerHTML=<p>Access authorised</p>; } else { document.getElementById("output").innerHTML=<p>Access denied</p>; } } </script> </head> <body> <form> <input type="text" id="username" /> <br/> <input type="text" id="enteredPassword" /> <br/> <input type="button" onClick="buttonClicked()" value="Login"> </form> <div id="output"> </div> </body> </html> Hi, I'm just starting out with a few bits of javascript and have hit a problem. The story is this: I have a basic HTML page that I want to display using a different css file depending on the page that the request comes from. I though I'd do this by appending "?cs=a" or "?cs=b" depending on the calling page. My problems are occuring whilst trying to read the data in the URL (querystring?) The code below is meant to be a bit of a debugger sort of affair in that it should display the variables but for the life of me I can't get it to work. The problem is that none of the variables seem to be getting populated with data, therefore nothing is displayed on the page and no css file is loaded. I'm pretty sure its something relatively simple so would appreciate if an expert could cast their eye over what I have below and point out the error of my ways... Code: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <!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> <SCRIPT type="text/javascript"> var thequerystring = new String(Request.QueryString("cs")); if (thequerystring == "a") { cssfile = "css1.css"; } if (thequerystring == "b") { cssfile = "css2.css"; } document.write('<link href="'+cssfile+'" type="text/css" rel="stylesheet">'); </SCRIPT> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p><span class="texttype">hello</span></p> this should change the format of the text depending on the css file that was loaded... in theory!! <p> </p> <p>Here is a list of the variables and their values... <script type="text/javascript"> document.write('<b>Some variables...</b><br>'); document.write('cssfilename: '+cssfile+'<br>'); document.write('Querystring text: '+thequerystring+'<br>'); </script> </p> <a href="javascript: history.go(-1)">Back <-- </a> </body> </html> Many thanks in advance Dan hi. i know a little php, but new to javascript. much of the syntax is identical. in reading the new rhino book (flanagan 6th), he gives the following example: Code: var square = function(x) { return x*x; }; okay, but then he does this: Code: square(plus1(y)) as an example of "invoking two functions in one expresson". (Prior he gives an example of defining a function with plus1, which is how I normally think of functions.) uh, square is a *variable*, but he is using it as a FUNCTION. Can somebody help me understand how this can be? how it works? I'm having trouble conceptualizng how we use a variable as a function. Not much programming experience, but some C++ and Python. I would like to make a web page that 1. flashes and image for 1 second 2. flashes a a pair of images for 1 second 3. displays an image and waits 4. Wait for a certain correct keystroke to be entered. 5. Repeat 1-4 a few hundred times. ^The above would iterate through a loop, say of 300 counts, and sub-iterate through an array of images, say 30, going through all 30 images 10 times for example. Oh, and the array elements would be displayed in random order. Also, I would like to keep track of errors (wrong keystroke) and time it took to enter correct keystroke. I would prefer that this webpage work on the iphone, even older ones. Java is limited on iphone? I tried playing a Java chess game and had no luck on iphone. So, would I be able to do this using Javascript? If not, Java would be best? As I said, I would prefer iphone compatibility, but it is not required. I just want to know which one I should use before I bury my nose in a book about programming Java or Javascript. |