So I am making a binary converter for science project and I am having a bit of difficulty.
As of now, I am trying to write a for loop that gets each individual variable of the user's input into a seperate variable.
However, I can't find a way to only create the same number of variables as the length of the string provided, so I can only use document.write, which is not what I want.
Can anyone help me figure out how to store each character of a string into a seperate variable and post that solution here please?
Thanks for your consideration.
Strings are array-like objects. They expose a length property and support array indexers to access the individual characters of the string. You can simply use a for loop to iterate through the characters.
var myString = "Hello World";
for(var i=0; i<myString.length; i++) {
var character = myString[i];
...
}
Related
In perl we can declare the array with qw or quote word take make each word is taken into individual array cell.
eg.
my #arr= qw( hello
world)
or else you need to quote each word
eg
my #arr = ("hello" , "word");
Is there something similar in javascript as sometime it need lot of formatting to simple declare array.
This is what you need, for this specific case: const arr = 'hello world'.split(' ');.
Edit:
Check out the docs for String.split on MDN. Also, read something on types in JavaScript, if you are wondering why it is possible to call this method on string literal, as I did.
I have a list of english words in a text file. I would like to create an array from this file separated by words:
var dictionaryWords = ["Apple","Orange","Banana","Strawberry"]
How can I use do this in javascript? and please try to explain in beginner terms since I'm still new to this! Thanks!
If you have the file contents in a variable, you use the split() method to split it into an array:
var dictionary = file_contents.split("\n");
\n is the newline character.
You can use AJAX to read the file from the server into a Javascript variable. There are many AJAX tutorials on the web, I'm not going to try to teach that here.
Is it ok to create vars with spaces and forward slashes?
Like so:
var PICKUPS / TRUCKS = {};
No, variable names cannot have spaces, but some special characters are allowed.
To reference the list of valid characters you can refer to this answer.
In your case, this is invalid in Javascript:
// INVALID!
var PICKUPS / TRUCKS = {};
First, the / operator is the divide operator, so the Javascript interpreter will look at this like a Mathematical operation, trying to "divide PICKUPS with TRUCKS", which will cause an error, especially after the var keyword, it won't know how to make sense of that (for example, first it will see that you are trying to create a variable, and then it will see that instead of creating a variable, you are trying to do some math, which will confuse the javascript interpreter).
But you can do something like this:
// Valid Javascript; No spaces or illegal characters
var pickups_trucks = {};
Also, if the names are embedded in javascript objects, you can name objects using string identifiers, where anything legal in a string can work:
var Automobiles = {
"Trucks & Pickups": [],
"Three Wheelers": []
};
console.log(Automobiles["Trucks & Pickups"]);
Hope this helps!
No, this is NOT VALID variable name in javascript. It cannot have spaces or forward slash in it.
Here https://mothereff.in/js-variables you can test if your variable name is valid or not
This is invalid javascript.
I recommend you declare variables separately like such:
var PICKUPS = 0,
TRUCKS = {};
I recommend you use a tool like JSLint or JSHint to verify you code.
In any programming language its not a good idea to use spaces or slashes in your variable names. There are several ways to write vars using underscores, camel case or snake case are just a few.
In order to split a string value into an array using javascript I need to split using delimiters. Repeated delimiters indicate a sub-value within the array, so for example
abc+!+;def+!+!+;123+!+;xyz
should split into abc, [def, 123], xyz
My nearest expression is ((?:+!(?!+!))+\;|$) but thinking about it that may be the one I first started with, as I've gone through so many many variations since then.
There is probably a blindingly obvious answer, but after what seems an eternity I'm now stumped. I took a look at regex to parse string with escaped characters, and similar articles which were close although not the same problem, but basically came to a stop with ideas.
Somewhere out there someone will know regular expressions far better than I do, and hope that they have an answer
I got this to work by using .split() with this basic pattern:
\b\+!\+;\b
And then:
\b\+!\+!\+;\b
And so on, and so forth. You will need to turn this into a recursive function, but I made a basic JSFiddle to get you started. First we split the string using our first expression. Then we create our newer expression by adding !\+ (this can easily be done dynamically). Now we can loop through our initial array, see if the string matches our new expression and if it does split it again.
var string = 'abc+!+;def+!+!+;123+!+;xyz',
data = string.split(/\b\+!\+;\b/);
var regex = /\b\+!\+!\+;\b/
for(var i = 0; i < data.length; i++) {
var string = data[i];
if(string.match(regex)) {
data[i] = string.split(regex);
}
}
console.log(data);
// ["abc", ["def", "123"], "xyz"]
I'm leaving the task of making this a recursive function up to OP. If you want some direction, I can try to provide some more insight.
I am working a serviceNow business rule and want to compare two strings and capture the substrings that are missing from the string for example...
var str1 = "subStr1,subStr2,subStr3,subStr4"
var str2 = "subStr1,subStr3"
magicFunction(str1,str2);
and the magic function would return "subStr2,subStr4"
I'd probably have better luck turning the strings into arrays and comparing them that way which if there is some method that would be recommended I can do that, but I have to push a , separated string back to the form field for it to work right, something with how sys_id's behave seems to demand it.
Basically I have a field on a form that holds a list of sys_ids, I need if one of those sys_ids is removed from the list I can capture the sys_id and make some change on the record belonging to it
If you're not against using libraries, underscore has an easy way to do this with arrays. See http://underscorejs.org/#difference
function magicFunction(str1, str2) {
return _.difference(str1.split(","),str2.split(",")).join(",");
}
The ArrayUtil Script Include in ServiceNow has a "diff" function, once you use split(",") on your Strings to create two Arrays.
e.g.,
var myDiffArray = new ArrayUtil().diff(myArray1, myArray2);
Assuming you're list has commas separating them, you can use split(",") and join(",") to turn them in to arrays/back into comma delimited lists, and then you can find the differences pretty easily using this method of finding array differences.