Falcon
Falcon

Reputation: 585

Can't convert text to JavaScript Array

I have the following code (Be patient I'm not at all a Javascript Programmer)

I've been able to load the concerning text files but am unable to convert them to an array could somebody please explain?

function Login(form) {

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    USRFile = fso.OpenTextFile("C:\\Users.txt", 1, false, 0);
    var USR_LIST = USRFile.Read(1000)
var USRitems = USR_LIST.split(",");
    USRFile.Close();
    fso = null;

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    PWDFile = fso.OpenTextFile("C:\\passwords.txt", 1, false, 0);
    var PWD_LIST = PWDFile.Read(1000)
var PWDitems = PWD_LIST.split(",");
    PWDFile.Close();
    fso = null;

username = new Array(USRitems);
password = new Array(PWDitems);
page = "SETTINGS.html";
if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] || form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9]) {
self.location.href = page;
}
else {
page = "Access_Violation.html";
self.location.href = page;
form.username.focus();
}
return true;
}

I have the following in the text Password.txt file

"p1","p2","p3"

and for the User.txt I have

"u1","u2","u3"

I just can't get the array???

Upvotes: 1

Views: 310

Answers (3)

Lucas M.
Lucas M.

Reputation: 16

You already have both in array form

var USRitems = USR_LIST.split(",");

And

var PWDitems = PWD_LIST.split(",");

Are arrays with the values. The split function is doing that. This is how it works :

string.split([separator][, limit])

Parameters

separator Specifies the character(s) to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string. If separator is an empty string, string is converted to an array of characters.

limit Integer specifying a limit on the number of splits to be found. The split method still splits on every match of separator, but it truncates the returned array to at most limit elements.

You can read more at MDN

Upvotes: 0

Avinash Ware
Avinash Ware

Reputation: 158

var array = string.split(',',2);

Upvotes: 0

user1508519
user1508519

Reputation:

split returns an array. When you do this:

username = new Array(USRitems);

You're putting the array you already have in a new array.

You simply just want to do:

username = USRItems

Example:

var array = [1,2,3];
console.log(array);
var array2 = new Array(array);
console.log(array2);

Outputs:

[1, 2, 3]
[[1, 2, 3]] // note the extra brackets

Upvotes: 1

Related Questions