JavaScript - Please Help Me Simplify This Algorithm
SO, thanks for checking this out.
If i have 3 text areas for output and an array that can be of size 0 to however big a user passes in values. How would I display the last 3 values of the array in the text areas? Here's what I was thinking psuedoCode, but I just want to know if there is an easier way. thnx! Code: //handling all the cases (ie if i have less than 3 items) var size = array.length display 0 if(size <= 3) if(size == '3'){ display0 = array[0] display1 = array[1] display2 = array[2] if(size == '2') do the same but copy the 2 elements if(size == '1') do the same else var newSize = (size -1) - 1 //to find where the first of the 3 elements will be display0 = array[newsize] display1 = array[newsize+1] display2 = array]newsize+2] Its seems a bit extraneous doing it this method, so i was just wondering if anyone could think of a better way. Thanks again! Similar TutorialsMy crumby DVR (security dvr not tivo) has a remote access page that will run ONLY on internet explorer because it uses active x. This means I can't run it on any other awesome browser and no phones . I want to re write it a little bit, all I need is the log in so I need to re write this a little bit so it will work on all browsers once that happens i can port it to my phone. the video stream comes unprotected and can be viewed decently through its RTSP I just can't control anything. My thoughts are to make a simple webpage with the video streams coming in via RTSP and when you log in you can send commands and can muk about the settings its just the java uses active x and theres a weird server side script I can't make heads or tails of. Here is my log on page, for a limited time I set it back to defaults... http://67.85.223.199:3001 Username: ADMINISTRATOR Password: 000000 so just view the source you don't even have to be in IE to view the source and look at the amount js files. I don't need any of the languages obviously and I don't need to set schedules or any settings, just need the PTZ (pan tilt zoom) controls to work and I need to be logged into do that, I just cant figure out how to make the log in when i re write it. Feel free to mess around I unhooked all cameras except for one, the only thing I ask is that someone look at the source code and take a look at the js and help me make heads/tails of what I don't need I want to make just a basic log in! ohh if you want to just stream the video through RTSP then the RTSP link in rtsp://67.85.223.199:110/cgi-bin/rtspStream/1 ohh and the DVR is a Bosch 600 series a 650-16A to be precise! Don't care what you do, this thing is useless to me if I can't CONTROL it from my phone like they told me. What's the best way to go about this? -Mike hi guys, can someonr put this into an array for me please--don't know much about js.. much obliged for your help btw...when I call the site from google, the divs are open but when I call the site from my browser address bar...the divs are hidden as should be (only in firefox, otherwise great. Any ideas why that might be) thanks a ton DD Code: jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headinga").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headingb").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headingc").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); Hi guys, Is there a way to simplify/shorten this already small piece of code? Ternary is the only thing I can think of, but it will only complicate the code. I don't believe a switch statement would apply. Code: if (sourceCode.substr(startStatus, 60).indexOf("Open") !== -1){ discStatus[i] = "OPEN"; } else if (sourceCode.substr(startStatus, 60).indexOf("Closed") !== -1){ discStatus[i] = "CLOSED"; } else if (sourceCode.substr(startStatus, 60).indexOf("W-List") !== -1){ discStatus[i] = "WAITLISTED"; } else if (sourceCode.substr(startStatus, 60).indexOf("Cancelled") !== -1){ discStatus[i] = "CANCELLED"; } Basically, there's a small substring that I'm checking for one of the four values (open, closed, w-list, and cancelled). Thanks for your time! can someone see a way to minimize this down to one statement that makes it infinate? if(x == i) || (x*1 == i) || (x*2 == i) || (x*3 == i) || (x*4 == i) || ............. x is a var user input. it is numeric. i is the counter placed above the function. not sure why this wont sort in asending order... i tend to make little dumb mistakes sorry. :/ Code: <script type="text/javascript"> function sort(nums) { var rangeStart = 0; var rangeEnd = nums.length - 1; var i = 0; var minPosition = rangeStart; while(rangeStart < rangeEnd) { // find minumum for(i = rangeStart; i < rangeEnd; i++) { if(nums[i] <= nums[minPosition+1]) { minPosition = i; } } // swap var temp = nums[rangeStart]; nums[rangeStart] = nums[minPosition]; nums[minPosition] = temp; // change range rangeStart++; } } document.write("<h3>Examples</h3>"); first10 = [2,3,5,7,9,4,8,0,6,1]; document.write("<div>Sorting <tt>["+first10+"]</tt> with current code gives "); sort(first10); document.write("<tt>["+first10+"]</tt></div>"); ages = [19,34,20,66,82,53,88,74,39,13]; document.write("<div>Sorting <tt>["+ages+"]</tt> with current code gives "); sort(ages); document.write("<tt>["+ages+"]</tt></div>"); </script> I've written a function that "condenses" a string if it is too long. Code: function shortenMsg(msg,maxLen){ if (msg.length > maxLen){ var over = msg.length - maxLen, // amount that needs to be trimmed method1 = (msg.match(/,\s/g) || []).length, // amount that method1 can trim method2 = (msg.match(/\]\s\[/g) || []).length; // etc... method3 = (msg.match(/demonstration/gi) || []).length * 6, // etc... if (method1 >= over){ msg = msg.replace(/,\s/g, ","); } else if (method2 >= over){ msg = msg.replace(/\]\s\[/g, "]["); } else if (method3 >= over){ msg = msg.replace(/nstration/gi, ""); } else { // optimal combination of 2+ methods } } return msg; } var longMsg = "...", shortMsg = shortenMsg(longMsg, 50); // example usage Each of the methods 1-3 is the amount that that specific method can trim from the string. I'd like to be able to trim as little as possible. For example, if the string needs 5 characters to be trimmed, and method1 can trim 8 characters, but method3 can trim 6, then method3 should be used. If none of the methods can individually trim the string enough, then I'd like the optimal combination of the methods that will get the job done. I can't figure out what sort of code structure I need for this (besides a ton of if/else statements). Maybe an array that contains each of the methods, arranged in increasing order....? Help would be appreciated! Sorry for posting so much recently Hello, I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below: Code: <script type="text/javascript" language="javascript"> // in head function checkRefresh() { alert("BEFO value = " + document.getElementById("visited").getAttribute("value")); if (document.getElementById("visited").getAttribute("value") == null || document.getElementById("visited").getAttribute("value") == "") { document.getElementById("visited").setAttribute("value", "refreshed") } alert("AFTER: value = " + document.getElementById("visited").getAttribute("value")); } </script> ... <body onload="Javascript:checkRefresh();"> ... <form id="hiddenform"> <input type="hidden" id="visited" value="" /> </form> </body> My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality): 1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name. 2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference. 3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it. I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed. Am I doing something wrong? Thanks for any help. I have an assignment to code a java class that plays rock, paper, or scissors. The class can access the history of past gestures played by both itself and its opponent, but nothing else. The class will be played against others in 10,000 matches and the winner will be determined via round robin format. Other than using a greedy algorithm to determine statistically the best choice from prior gestures and basic pattern recognition I have no decent ideas. I'd appreciate any suggestions.
|