JavaScript - Simple Solution To Pseudo-code
Similar Tutorialshow do i write a recursive program to calculate the factorial of a number between 0 and 6.if the user enters a number greater than 6 you should prompt the user again to input another number or -1 to exit the program.how do i write the pseudo code and the javascript code for this program? i have no idea of how to do this and have been trying to figure it out since the last hour but I just can't figure it out. I have trawled the net for a solution and tried many different things that I have seen but still no luck. Maybe the answer is staring me right in the face and I just can't see it. The code it executing the first time but as the setTimeout tries to start the second loop I get an error 'box is not defined'. Any suggestions and explanations of what I am doing wrong much appreciated! Code: function expand(box) { var box = document.getElementById(box); var originalHeight = parseInt(box.style.height); var inc = 5; newHeight = (originalHeight + inc); box.style.height = newHeight + "px"; int = setTimeout('expand(box)',100); } Hello, I used to own a book that explained an approach to coding, but I lost the book, cannot remember the author or title, plus I have only recently returned to coding and I have half forgotten the method the book taught It was brilliant as it used a system of non-language specific psuedo code in order to structure the solution to coding problems before actually coding in the language of choice Before moving on to JavaScript, I used to code Lingo/Director projects. Before finding this book, I used to struggle when attempting intermediate+ problems as I found it difficult to organise a clear approach before coding, so I just used to dive in there (which is bad). After finding the book, I found it quite easy to create a psudo code solution to most coding problems then, using this structured psuedo code as a guide, it was quite simple to code the structured psuedo code solution in the chosen language I have tried Amazon etc and although they have some books on structured programming, they are not the book I had and I get the sense that what they mean by structured programming is different to what the book I had actually taught I was hoping that someone on this forum might have used the same book, or knows what I mean and knows of a book or website that explains structured programming techniques It would be a great help to me if I could find this information again and I think it would be of great benefit to others on here - especially beginner javascripters like myself Thank you Hi guys, I'm trying to write a function such that it can be called, with a particular string specified in the call, and the function will then search a pre-defined array ("pages") of webpage URLs and headers in the form ["[www.sampleurl1.com] Sample Header 1. ", "[www.sampleurl2.com] Sample Header 2. "]; etc. etc., to see if the aforementioned string occurs in the "header" part of any of the indexes of the "pages" array, and then if it does, to alert "Found in x,y,z" where x y and z are the indexes of the "pages" array in which the specified string was found to occur. Here is what I have so far: Code: var pages=["[www.lboro.ac.uk] Loughborough University. ", "[www.oxford.ac.uk] Oxford University. "]; var founds=[] function findIdxsC(s){ for(i=0;i<pages.length;i++) if((pages[i].substring(pages[i].indexOf("]") + 1,pages[i].lastIndexOf("."))).match(/s/)) founds.push(pages[i]) } var array=founds.filter(findIdxsC) var arr2str=array.toStr; alert("Found in "+arr2str); findIdxsC("nIvERsi"); In theory, the above example should alert "Found in 0,1" because I have used the "match" command (so the search is case-insensitive) and the string "niversi" is found in the "header" parts of indexes 0 and 1. However, what it actually alerts is "Found in undefined". As far as I can see the problem lies with either the if-statement(and the associated command to "push" the indexes of "pages" for which the condition applies, to a new array, "founds") or with the filter and toString commands used towards the end. Perhaps it is one of these, or perhaps there is something else I am missing? Either way I would appreciate it if anyone could identify what in my code is causing the "Found in undefined" alert and suggest how I could rectify it, as it's causing me a bit of a headache Many thanks The code I commeted out dosen't work I want to check if its null and if so alert them. { // PART 2: YOUR CODE STARTS AFTER THIS LINE correctAnswer = Math.floor((Math.random() * 100) + 1); // For testing purposes. alert("Testing purposes, correct answer is: " + correctAnswer); var number = 0; //// Calculations //if (isNaN(number) = true) { // alert("Thats not a number"); //} //document.write(number); while (number != correctAnswer) { number = prompt("Wrong Number, guess again."); number = Number(number); if (number < correctAnswer) { document.writeln("Small guess: " + number); } if (number > correctAnswer) { document.writeln("Large guess: " + number); } if (number == correctAnswer) { document.writeln("Correct guess: " + number); } } } I would like to compare my work to the work of others, can you please solve this and post your result? Declare an 8 x 8 matrix and an array of length 22. 1.- Populate the matrix. 2.- Copy the elements of the first row of the matrix, the anti-diagonal, and the last row of the matrix into the array, to form a Z shape. 3.- Sort the array. 4.- Assuming that the data in the arrays are grades, compute the average of the grades stored in the even locations of the array. 5.- Copy the array back into the matrix.(back into the Z) 6.- Print out the matrix values. Simple random pic script that I found on the some other forum (I forget what it was) [CODE]var aryimages = new Array('images/pic/01.jpg', 'images/pic/37.jpg', 'images/pic/02.jpg', 'images/pic/family/08.jpg','images/pic/08.jpg', 'images/pic/food/03.jpg'); randompic.src = aryimages[Math.floor(Math.random() * aryimages.length)]; [CODE] code anchors to HTML markup [CODE]<img name="randompic" id="bg" />[CODE] it is working perfectly for Safari and Chrome. Nothing is showing for FF. Any suggestions appreciated. Hi... i have this code: <script type='text/javascript'> cookie_name = GetCookie("href_location"); if(cookie_name){ alert("Cookie found, redirecting to stored cookie."); document.location.href=cookie_name; } </script> I will like to create a iframe using the info from the "cookie_name" value <iframe name="FRAME1" src="cookie_name" width="350" height="320" frameborder="0" scrolling="auto"></iframe> I have a script that set a cookie with the value of a chosed website, what i need now is to create an iframe with that value. Any help will be apriciated. Sorry for my poor enlish Here's the basic concept, its somewhat of a counter, and I think javascripts the way to go... Basically I'm looking to generate a running number... x + 11 = print the number The tricky part is, I need it to never stop, somewhat like a timer (example http://www.hashemian.com/tools/javascript-countdown.htm) but not a timer... Think the tally mark on McDonald's 99 billion served.. Any suggestions or ideas? I just started learning JavaScript. I'm sure it's something really simple but I cannot figure out what is missing from my code. No matter what year I enter into the text box, I keep getting "The year you entered, 2080393, is a leap year." It's not returning false...ever. Can someone please tell me what I'm doing wrong so I can stop pulling my hair out? Thanks! <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Is It A Leap Year?</title> <script type="text/javascript"> /* <![CDATA[ */ function isLeapYear(year) { year = parseInt(year); var leapYear = true; var remainder = 0; if(isDivisibleBy(year, 4)) { leapYear = true; } if(isDivisibleBy(year, 100)) { leapYear = false; } if(isDivisibleBy(year, 400)) { leapYear = true; } if(leapYear) { window.alert("The year you entered, " + year + ", is a leap year."); } else { window.alert("The year you entered, " + year + ", is not a leap year."); } } function isDivisibleBy(year, divisor) { var remainder = 0; remainder = year%divisor; if(remainder == 0) { return true; } else { return false; } } /* ]]> */ </script> </head> <body> <form action="" id="GetLeapYear"> <p><strong>Is It a Leap Year?</strong></p> <p><strong>Enter a valid 4-digit year below to find out!</strong><br /></p> <p> <input type="text" name="year" size="20" style="color: black; border-style: solid; border-color: inherit; border-width: medium; background-color: Transparent" value="" /> <input type="button" value="Leap Year?" onclick="isLeapYear(document.getElementById('GetLeapYear').year.value);" /> </p> </form> </body> index.html Code: <html> <head> <title>:: wtmp</title> </head> <body> <p>Welcome to my page</p> <script type="text/javascript" src="file.js"></script> </body> </html> file.js Code: function one(p1, p2) { var j_text=p1+" "+p2; return j_text; } function two() { var rslt=one("Hi", "there!"); } document.write(rslt); var mainscrpt=one("Hello", "world!"); window.alert(mainscrpt); Basically, I have a Simon Says game with very simple functions but I added an image map with buttons that I now want to use, rather than a table with images as buttons. The problem is, now I can't get the game to work with the image map. Can anyone help me?
can some please tell me what this line does... Code: <div onClick="getElementById('tInfo').onclick();"> So frustrating that lately I've been having trouble with the simplest of code, and this is about as simple as it gets. Matched it up with every similiar example I can find both in books and on the net, and it seems to be perfectly valid code, but when I run it, nothing. :S 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/html; charset=utf-8" /> <title>Untitled Document</title> <script language="javascript" type="text/javascript"> function favouriteColour() { var index = document.getElementById("select1").options[document.getElementById("select1").selectedIndex].value; document.getElementById = "Your favourite colour is " + index; } </script> </head> <body> <select id="select1"> <option></option> <option id="black" onchange="favouriteColour()">Black</option> <option id="white" onchange="favouriteColour()">White</option> <div id="favcolour"></div> <option> </select> </body> </html> Thanks in advance. Hi everyone, I've been coding Perl for quite some time, but I'm new to Javascript and can't quite figure this out. I want to call a Javascript function that is sent to the browser via a perl script. When I hard code the string "Fargo" into the code it works just fine, when I pass the word Fargo via a variable the script will not call the function what so ever. Is it possible to call a javascript function via a Perl script with Perl providing all the necessary data? Here is my code: #!/usr/local/bin/perl use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); use Cwd; print header; print "top<br><br>"; $data = "fargo"; print <<html; <html><head></head>my heading is here<br><body></body> <script type="text/javascript"> function testz(inbound) { document.write("im in ", inbound); } //the script will only work if I uncomment the line below and comment out two lines below //var data1="fargo"; var data1 = $data; testz(data1); </script> </html> html Thanks to everyone in advance for your help. Hello, What I need is a simple service area, zip code validation form that redirects to a certain URL when a valid zip code is submitted and a different URL when an invalid zip code is submitted. I found a form script example that works well with only a single zip code. My problem is I can't figure out how to modify it so that multiple zip codes are valid. Here is the head part. 60016 is one of about 50 valid zip codes I need the form to accept as valid - [code] <script> var correctCode = "60016"; function validateCode() { var code = document.getElementById("codeTextBox").value; if (code == correctCode) { window.location.href = "/ggc/test1"; } else { window.location.href = "/ggc/test2"; } } </script> [code] Here is the body part - [code] Please enter your zip code: <input type="text" name="codeTextBox" id="codeTextBox" /> <input type="submit" name="Submit" value="Submit" onclick="validateCode()" /> [code] I'm getting a runtime error on the code in red. Can anyone see anything that is wrong with it? These functions are called by an onBlur by the way. <script type="text/javascript"> //THIS GENERATES QUANTITY OF MATERIALS function generate3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); if (tot3 > 28) { alert("16 is total amount of materials for now."); } else { for(var i =1; i<=tot3; i++) { tbl3.innerHTML = tbl3.innerHTML + '<tr><td colspan=\"2\"><hr /></td></tr><tr><td>'+i+') Quantity: <input type=\"text\" name=\"qty'+i+'\" size=\"3\"\/></td><td>Description: <input type=\"text\" name=\"material'+i+'\" /></td></tr><tr><td colspan=\'2\' \">Your Truck<input type=\"checkbox\" \/> Other truck<input type=\"checkbox\" \/> Cash<input type=\"checkbox\" \/> Credit Card<input type=\"checkbox\" \/> P.O.<input type=\"checkbox\" \/></td></tr>'; } tbl3.innerHTML = tbl3.innerHTML + '<br>'; } } function remove3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); for(var i =1;i<=tot3; i++) { tbl3.innerHTML = ""; document.ComplianceForm.materialamount.value = ""; tbl3.innerHTML = ""; } } </script> I'm pretty new to Javascript so don't lay into me too hard. haha thanks. -Ty Hi there, I'm not a js coder but needed to cobble something together for a simple IE7 "sniffing" task. It's basically taken from a helpful poster over at http://css-tricks.com/snippets/javas...rnet-explorer/ I'm pretty sure that what I've done is as inelegant as hell!! Although it works, I'd be grateful if someone could show me how to tidy things up. Many thanks Code: $(function(){ if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x; var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (ieversion<=7) $(".tooltipthumb").tipTip({attribute: "title", defaultPosition: "top", fadeIn: 200, delay: 0}); else if (ieversion>=8) $(".tooltipthumb").tipTip({attribute: "title", edgeOffset: 130, defaultPosition: "top", fadeIn: 200, delay: 0}); } else { $(".tooltipthumb").tipTip({attribute: "title", edgeOffset: 130, defaultPosition: "top", fadeIn: 200, delay: 0}); } }); $(function(){ $(".tooltip").tipTip({attribute: "title", fadeIn: 200, delay: 0}); }); $(function(){ $(".tooltip_small_thumb").tipTip({attribute: "title", edgeOffset: 90, defaultPosition: "top", fadeIn: 200, delay: 0}); }); $(function(){ if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x; var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (ieversion<=7) $(".tooltip_small_thumb").tipTip({attribute: "title", defaultPosition: "top", fadeIn: 200, delay: 0}); else if (ieversion>=8) $(".tooltip_small_thumb").tipTip({attribute: "title", edgeOffset: 90, defaultPosition: "top", fadeIn: 200, delay: 0}); } else { $(".tooltip_small_thumb").tipTip({attribute: "title", edgeOffset: 90, defaultPosition: "top", fadeIn: 200, delay: 0}); } }); |