Optimizing String Shortening Algorithm?

Aug 16, 2010

I've written a function that "condenses" a string if it is too long.

function shortenMsg(msg,maxLen){
if (msg.length > maxLen){
var over = msg.length - maxLen, // amount that needs to be trimmed

[Code]....

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....?

View 10 Replies


ADVERTISEMENT

Shortening The Code To A Variable?

Jul 8, 2009

how do i shorten the following

var A = document.getElementById('Mydiv');
A.style.color="#999";

when I try this wont work

var A = document.getElementById('Mydiv').style.color;
A="#999";

how to write it and why?

View 2 Replies View Related

Shortening The Show/hide Script?

May 28, 2010

I have a simple hide/show script that is doing what I want, but I was wondering if there is an easier way around having to specify show and hide requirements "onclick" each time. Here is what I've got:

Javascript:

<script type="text/javascript">
function show(boxid) {
document.getElementById(boxid).style.display="block";
}

[code].....

View 5 Replies View Related

Optimizing Rollovers Script

May 18, 2003

i have the following javascript code:

<!-- hide from browsers that dont support js
if(document.images)
{
about_over = new Image
about_over.src = "images/btn_about_r.gif"
about_out = new Image
about_out.src = "images/btn_about.gif"

success_over = new Image
success_over.src = "images/btn_success_r.gif"
success_out = new Image
success_out.src = "images/btn_success.gif"
}
// -->

and i am using it from within the html code in a standard way:

<a href="about.php" onMouseOver="document.about.src='images/btn_about_r.gif'" onMouseOut="document.about.src='images/btn_about.gif'"><img src="images/btn_about.gif" name="about" width="56" height="23" alt=""></a></td>

question: i don't know much about javascript. i know that the script above preloads the images but i don't understand why the images reference names ( "about_over", "about_out", etc) do not get utilized within html code. it seems to me that they should be used.

View 3 Replies View Related

Very Large Lists - Optimizing Performance?

Jul 28, 2010

I am working on a list which displays a large number of contacts (400 to 500 for a typical user). Currently, I am using Dojo (customized widgit) which is created 400 times (once for each contact).This of course is resulting in alot of rendering delay. What is the best approach to display large lists in HTML/javascript? Each list item needs to have an image.

View 2 Replies View Related

Algorithm

May 6, 2005

i am trying to store some shopping cart details in a cookie. I have been able to do that but i want to now retrieve the values and put them in a shopping cart table. An example of the cookie file is

Product1=value,Quantity1=value,Price1=value,Extras1=value;
Product2=value,Quantity2=value,Price2=value,Extras2=value;

I have been able to retrieve the 2 cookies

cookieArray = document.cookie.split("; ")
for(i=0; i<cookieArray.length ; i++)
{
document.write(cookieArray[i] +"<br>")
}

But now for each Product i want to retrieve the 4 values. Can anyoine help me with this or suggest a better way to achieve the same thing.

View 7 Replies View Related

JQuery :: Optimizing Large Quantity Of DOM Manipulations?

Mar 16, 2010

I have a website using jQuery that for the most part works fine, but it contains a very large form with a lot of fields. I have an option to save the settings from the form and to load and retrieve them from the server. The problem I'm running into is that the loading settings involves changing so much in the DOM (the form is huge and contains a lot of fields) that it seems to be freezing up on some browsers or timing out. I can't reproduce this on my computer (although it does take awhile to finish processing) but I've gotten enough reports of the problem that I'm looking for some advice as to how I can speed it up some.

The site is [URL] I've tried to speed it up by caching a good chunk of the selectors I'm interacting with and I'm using IDs to access the fields in most circumstances. But I'm not sure what else I can do to really optimize the loading. how I can go about improving on the load speed?

View 9 Replies View Related

JQuery :: Optimizing Large Dynamic <select>

May 29, 2009

I'm building on-the-fly <select> lists from JSON data fetched from the server. Some of then include a large number of items (>20,000).

The SQL and HTML parts are working fine. The AJAX script fetches data fairly quickly (around 1 second) and large selects are not a problem once they're built (the browser handles them nicely, even IE). The bottleneck is in the process of picking the JSON data and building the <option> tags. That can take a full minute.

What's the recommended (i.e. fastest) method to generate a large <select> list?

My current approach is this:

// Fetch data (GET method allows me to use browser cache)
$.get(url, get, function(jsonValues, txtStatus){
that.values = jsonValues;
}, "json");

[Code].....

View 5 Replies View Related

Recommended Algorithm To Obfuscate Code?

Oct 20, 2009

I want obsfucate and compress the code for my website (HTML, CSS but especially Javascript). Are there any industry standard algorithms that are recommended?

View 5 Replies View Related

What Is The Algorithm To Compute The Factorisation Of An Integer?

Oct 30, 2006

I need some ideas on how to compute the factorisation of a non-zero integer?

For example:

if the user input is 8, its factorisation is 1 * 2 * 2 * 2;
if the user input is -300, its factorisation is -1 * 2 * 2 * 3 * 5 * 5

Does anyone know?

View 3 Replies View Related

Algorithm To Calculate Mortgage Payments..

Nov 2, 2004

Does anybody know of an algorithm to calculate mortgage payments? e.g.

Amount: 100,000
Interest Rate: 4.5%
Term: 25 Years

Monthly Payments: x

I've looked at other js scripts to do this but it seems alot more involved then I thought, and because they use non descriptive variable names I can't work any of them out..

View 1 Replies View Related

Algorithm Only Finds Vertical Path?

Jan 19, 2010

I recently started on an browser based MMORPG, I just found 1 major issue.I'm using these libraries

JS A* Algoritm ( http://devpro.it/code/137.html )
Jquery ( http://www.jquery.com/ )
Jquery UI (http://ui.jquery.com/ )

So I'm using a little piece of JavaScript code in which im accessing the AStar function. The downside is, the AStar algorithm only finds vertical tiles. (See screenshots below)

function mapmouse (obj) {
//Get the coordinates
coords = obj.id.split('_');

[code]....

If you choose a non-vertical path, var path gives an empty array (path.length = 0 )If I choose a vertical path, it's working as intended.

As you can see with this little test here, the AStar function itself is working properly (the libs.js is used by both the game and the example)[URL]

View 1 Replies View Related

Rock-Paper-Scissors Algorithm - Code A Java Class That Plays ?

Dec 5, 2011

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.

View 6 Replies View Related

JQuery :: Display The Result Of Ajax Call As Html String And Not Plain String?

Dec 25, 2010

I want to know if there is a way to return ajax call as html value and not plain text, ie all html formatting will be displayed.

My code:

<script src="jquery.js">
<script>
$(function()
{

[Code]....

String returned from webform4.aspx is html formatted but jquery displayed it as plain text. Is that anyway to display it as html string ?

View 3 Replies View Related

Converting Int To String - Variable To Concatenate It As A String To Find An Input Box

Jun 26, 2010

I have a for loop: Code: for( var i = 0; i < aInput.length; i++ ) I want to use this i variable to concatonate it as a string to find an input box

Code:
var j = i;
var qualname = "discountqualifier" + j;
qualname.toString();
if ( inputName == ( qualname ) )
{

Assuming I have a input box named discountqualifier0, discountqualifier1, discountqualifier2 etc...

View 4 Replies View Related

JQuery :: Display Unicode String In Textbox With Ncr String?

Dec 2, 2010

I have some jquery code like this:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1258" />

[code]....

View 1 Replies View Related

JQuery :: String In String An Branch Based On Found?

Oct 18, 2010

I need a simple, quick and efficient way to logically branch if I find a string is contained in another string in jquery Most other languages this can be resolved in one or two lines and it would be readable.

View 5 Replies View Related

If Statement To Check Whether String Is A String Is Returning False

Jan 25, 2011

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.callback should be a string so why is it saying otherwise?

View 3 Replies View Related

Convert String Into Array - Break Apart A String Into Characters

Jan 27, 2010

Is it possible to break apart a string into characters, be it a word or a sentence, and store each individual character in an array?

View 11 Replies View Related

Combine The Three Values Into A String, Create A Md5 Of The String, Then Call The Value ?

Sep 21, 2010

I have made a basic form, and I need to combine three values within my form, then create an md5 hash of this string.Then assign it to a hidden variable.My form is here...

Code:
<p>
<label for="firstname">First Name: </label>
<input id="firstname" type="text" name="firstname" /><br />[code]....

Or I have created a pastebin of it here, for easy reading: http://pastie.org/1171757.So I need to be able to combine the three values into a string, create a md5 of the string, then call the value of the string into a hidden value all before posting the form.

View 12 Replies View Related

Substring Or Trim The String - Equate The Selection Value To String "Select State" And Execute Alert Message?

Apr 19, 2010

I need help with substring or trim function in javascript. Find below my code. Selection holds the value Select State, and length of the string is 14. I need to equate the Selection value to string "Select State" and execute alert message.

function selected_item() {
if (Selection=="Select State")
alert("Select the State");[code]....

I tried this:

var state=Selection.substring(0,11); and then string would be equated to state variable. But it is not working.

View 9 Replies View Related

Replace Characters / Inner String From A String

Aug 13, 2011

How I would remove characters from a string if they are present?

For example lets say I wanted to remove c:/fakepath/ from the string c:/fakepath/DSF102.jpg and just leave the DSF102.jpg how would I got about this?

View 2 Replies View Related

Validation - From String - Check If Some Char In Textbox Contains One Of The Ones In String "allowedChars"

Jul 14, 2011

I have this, VB.Net code..how can i do exact same thing in javascript?

How to check if some char in textbox contains one of the ones in string "allowedChars"

View 4 Replies View Related

Modify String - Lowercase Letters And Uppercase Letters In A String And Then Swap Them

Jan 19, 2010

I am currently trying to build a new function in javascript that is supposed to handle a string of text. The idea is that it should find the lowercase letters and uppercase letters in a string and then swap them.

Meaning all lowercase letter becomes uppercase letter and vice versa.

So i am just asking if someone could point me in the right direction or give some tips. I've currently been reading about the toUpperCase(); and toLowerCase(); functions and i am fairly confident i know how to use them for switching, however i still need a way to find the lower , upper character in the string so i later can switch them.

View 5 Replies View Related

Use String.replace That Is Not Case Sensitive And Replace Every String Found?

Jul 27, 2010

Here is my code:

<script type="text/javascript">
var str="Welcome to Microsoft! Microsoft Microsoft";
var stringToBeFound = 'Microsoft'
var ReplaceString = 'site'
document.write(str.replace(stringToBeFound , ReplaceString ));
</script>

My problem is im trying to use string.replace that is not case sensitive and replace every string found. I could use regular expression with it but my stringToBeFound is a dynamic variable im getting it from my database

View 9 Replies View Related

Remove A String From Within A String

Jul 23, 2005

How can i remove a string from an existing string in javascript. I have a textbox in a form and want to make sure that when the user clicks a button that certain words are moved, like all instances of "hello" should be taken out of the text the user typed in the textbox.

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved