wirte a jquery or javascript function

Hi guys,

I have a jquery/javascript problem. I am collecting a few values and i want to pass those values through a function. using those values I want to return a result.

basically I will have a predermine list of about 90 records that will look something like this:
1,1,2.5,3.4 = 786ugj

I would like to have a function where I would pass the four variables to get the result so it will look something like this

getCode(valuea, valueb, valuec,valued){
//in here return code
}

could this be done by building an array and having the index of the array be the code that gets returned?

or be done at all? i really need helpwith this

many many many thanks!!!
vthunder70Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shinesh PremrajanEngineering ManagerCommented:
The problem is still not very clear.

what is the pattern that you are having in the 90 records
i mean is it a comma seperated values or just a numerals.

1,1,2.5,3.4 = 786ugj


Let us know

vthunder70Author Commented:
The patter should be a comma separeted list.

Basically it takes four values to spit out a code something like this:
Value 1,value 2, value 3,value 4 = code

And this values are numeric

I hope this helps

Thanks
StealthyDevCommented:
Hope the below code will help you!!!

Best regards.
<script>
	function MyPattern()
	{
		this.Value1 = "";
		this.Value2 = "";
		this.Value3 = "";
		this.Value4 = "";
		this.Code = "";
	}

	function splitMyPattern(myPattern)
	{
		var mySplit = myPattern.split(",");
		var mySplit2;
		if(mySplit.length == 4)
		{
			mySplit2 = mySplit[3].split("=");
			if(mySplit2.length == 2)
			{
				var objPattern = new MyPattern();
				objPattern.Value1 = mySplit[0];
				objPattern.Value2 = mySplit[1];
				objPattern.Value3 = mySplit[2];
				objPattern.Value4 = mySplit2[0];
				objPattern.Code = mySplit2[1];
			}
		}
		return objPattern
	}

	var objPattern1 = splitMyPattern("Value 1,value 2, value 3,value 4 = code");
	alert(objPattern1.Value3);
	alert(objPattern1.Code);
</script>

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Shinesh PremrajanEngineering ManagerCommented:
Hope the attached code will serve you the need.
<script type="text/javascript" >

var myPattern = "Value 1,value 2, value 3,value 4 = code";

function splitMyPattern(myPattern)
{

var runn=new RegExp(/\,+|=/);

var mySplit = myPattern.split(runn);

var objPattern = new MyPattern();

objPattern.Value1 = mySplit[0];
objPattern.Value2 = mySplit[1];
objPattern.Value3 = mySplit[2];
objPattern.Value4 = mySplit[3];
objPattern.Code = mySplit[4];

}

var objPattern1 = splitMyPattern("Value 1,value 2, value 3,value 4 = code");
alert(objPattern1.Value3);
alert(objPattern1.Code);


</script>

Open in new window

vthunder70Author Commented:
I'm sorry I was not clear. I see I messed up giving you guys the details.

the user will select 4 options and I want to pass these 4 values trough a function. Once inside the function it will check against a list to match those options giving by the user and then spit out the code that comes from the user selecting those particular options.

something like


getcode(value1,value2, value3, value4){
1,2,3,4 = slflfj
2,2.3,4,5 = dlfkjfs
}

so if the user choses in the in interface 1,2,3,4 when the click the button get code it will spit out "slflfj"
StealthyDevCommented:
Please be more specific. What does the getCode function does? Explain in a list of rules.

Regards.
vthunder70Author Commented:
will do:

When the page loads the user will be presented with:
-Radio list which has four values (1,2,3 and 4)
-Dropdown list that has four values (1,2,3,4)
- 2 Sliders with values (10 to 20) and (80 to 90)

When the user clicks the submit button I want to compare those values with a list of values that i have living in an array.


so let's say the user chooses:
radiobutton = 1
dropdown = 2
first slider = 12
second slider = 83

when he/she clicks enter it will find that combination in my predetermine list and get the code... so it will find this 1,2,12,83 = a34bs. and I want to output the code "a34bs"

I hope this makes it a little more clear.

Thanks!
StealthyDevCommented:
I hope you can fill a hash table to match the value.

Please find the below code, try to execute it in a browser.
Hope it helps.

Best Regards.
<script type="text/javascript">
	function Hash()
	{
		this.length = 0;
		this.items = new Array();
		for (var i = 0; i < arguments.length; i += 2) {
			if (typeof(arguments[i + 1]) != 'undefined') {
					this.items[arguments[i]] = arguments[i + 1];
					this.length++;
			}
		}

		this.removeItem = function(in_key)
		{
			var tmp_value;
			if (typeof(this.items[in_key]) != 'undefined') {
					this.length--;
					var tmp_value = this.items[in_key];
					delete this.items[in_key];
			}

			return tmp_value;
		}

		this.getItem = function(in_key) {
			return this.items[in_key];
		}

		this.setItem = function(in_key, in_value)
		{
			if (typeof(in_value) != 'undefined') {
					if (typeof(this.items[in_key]) == 'undefined') {
							this.length++;
					}

					this.items[in_key] = in_value;
			}

			return in_value;
		}

		this.hasItem = function(in_key)
		{
			return typeof(this.items[in_key]) != 'undefined';
		}
	}
</script>

<input id="myText1" value="1">
<input id="myText2" value="2">
<input id="myText3" value="12">
<input id="myText4" value="83">
<input type="button" value="Test" onclick="compare()">

<script>
	function compare()
	{
		var myHash = new Hash("1,3,12,83", "senthur", "1,2,12,83", "a34bs", "2,1,11,88", "ss123f");
		var myKey = document.getElementById("myText1").value + "," + 
					document.getElementById("myText2").value + "," + 
					document.getElementById("myText3").value + "," + 
					document.getElementById("myText4").value;
		alert(myHash.getItem(myKey));
		/*for (var i in myHash.items)
		{
			alert('key is: [' + i + '] value is: ' + myHash.items[i]);
		}*/
	}
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vthunder70Author Commented:
this is pertty cool! I'm still waiting to get the actual data but as far as this looks this should work.

do you mind explaining the code a bit?

Thanks.

I'm waiting to get back the actual data then I'll close the question
StealthyDevCommented:
Hi, Hash Table is one, where you can store your data similar to the ARRAYs.
Simple different between Arrays and HastTables are:

In array you access the object like -  a[1], a[100]
In HashTable you access the object like - a["myKey1"],a["myKey2"]

So, we are loading keys and values. And, we get it back using key search.

Hope this explains a bit?
vthunder70Author Commented:
would this code run fine with about 100 items in the hash table?
StealthyDevCommented:
Hope so. Please test it by adding simply here:
var myHash = new Hash("1,3,12,83", "senthur", "1,2,12,83", "a34bs", "2,1,11,88", "ss123f");

Hash table should work with a large amount of data. Dont worry. But test and revert back.
Regards.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.