Jump to content

If (variable = Anything In Array)?


EmperorZenos

Recommended Posts

I'm wondering if I could take a variable and see if it equals anything in an array.I don't want countless if statements, it gets messy, and I wanna save time.I can't just have:

var sym=document.getElementById("ukelop").value;var ag=new Array("G\'Ziir","K\'iir", etc. etc. "J\diir", etc. etc.);if (sym==ag) {var curr=sym;} 

So, in this case, I want to set "curr" with anything from "ag" by using "sym."I also want to know how can I do something like a console for a game or command line, in a simple for w/o too many ifs.So, if I command put in is something like this: (In a text input area)set curr J'ZiirKey:[commandtype] [variable] [modwith]So, I want the script to recognize a command with commandtype, and modify variable with modwith w/o 600 different ifs for large amounts of variables.One last thing;I want a script to recognize something where it registers an array, and gives it a value for each line. I then want it to get number of values in that array.

Link to comment
Share on other sites

Well, to answer your first question, you can add this to your script:

Array.prototype.search = function(V) {  var R = false;  for(var i=0;i<this.length;i++) {	if(V == this[i]) {	  R = true;	  break;	}  }  return R;}

And then you can make your code like this:

var ag=new Array("G\'Ziir","K\'iir", etc. etc. "J\diir", etc. etc.);if (ag.search(sym)) {var curr=sym;}

To answer your second question: it would be quite a complicated thing. You would have to build an application that's ready for it, using string manipulation functions and regular expressions. It's too much work and thinking for me to just do it for you.The amount of elements in an array can be obtained in the Array.length property: http://w3schools.com/jsref/jsref_length_array.asp

Link to comment
Share on other sites

Thanks for the reply, I can understand the second question and thank you for that. But you answered half of my third question.I want to know how to register new values for an array for example:

Put all of your pets in here, and separate each by a new line:<textarea id="dd"><textarea><input type="button" onClick="ddd"><script type="text/javascript">[...]</script>

Link to comment
Share on other sites

So, if I command put in is something like this: (In a text input area)set curr J'ZiirKey:[commandtype] [variable] [modwith]
For simple situations you can use eval.
var str = new String(document.getElementById("commandtext").value);var chunks = str.split(" ");switch (chunks[0]){  case "set":	eval(chunks[1] + " = \"" + chunks[2] + "\";");  break;  default:	alert("Command not recognized");  break;}

That's pretty simplified, but it will work to assign whatever value (without spaces) you want as a string to a variable.

Link to comment
Share on other sites

For simple situations you can use eval.
var str = new String(document.getElementById("commandtext").value);var chunks = str.split(" ");switch (chunks[0]){  case "set":	eval(chunks[1] + " = \"" + chunks[2] + "\";");  break;  default:	alert("Command not recognized");  break;}

That's pretty simplified, but it will work to assign whatever value (without spaces) you want as a string to a variable.

Interesting, so I can use:var {Whateverchunks[1]is}=chunks[2]; after eval? Or does it already do it?
Link to comment
Share on other sites

The eval function runs a string as executable code. If the command is this:set curr J'ZiirThen chunks[1] == "curr", and chunks[2] == "J'Ziir". The string sent to eval is [curr = "J'Ziir";], so it sets the variable curr to that value. You can't refer to the variable curr using chunks[1], because chunks[1] is a string value, not a variable name. The eval function is what executes the string as a line of code.

Link to comment
Share on other sites

The eval function runs a string as executable code. If the command is this:set curr J'ZiirThen chunks[1] == "curr", and chunks[2] == "J'Ziir". The string sent to eval is [curr = "J'Ziir";], so it sets the variable curr to that value. You can't refer to the variable curr using chunks[1], because chunks[1] is a string value, not a variable name. The eval function is what executes the string as a line of code.
Where in the script should this portion be added? Seeing as the location of some snippets depends on the location.
Link to comment
Share on other sites

You can add it wherever you want, that's just an example of parsing a command and running it using eval. If you write regular JavaScript commands in the text box instead of your own set of commands then you can just eval it without parsing it. Obviously it's only going to be able to work with global variables though, it's not going to be able to access local variables from any other function or scope, unless you run the eval line in the scope of your other variables. JavaScript functions have a call method to run them in another scope.funcname.call(scope);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...