471,337 Members | 1,088 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

javascript creating an array, that's length is the result of a match

Hi People wondering if anyone can help me with a problem I'm having

I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code that I have

what I have so far is a page that has a text box for user input, which
will take whatever the user enters, will count all the characters in
the inputted text which also then uses the "split" command, to split
the users text (string) into its individual pieces.

the code I have so far is as follows
<html>
<head>

<script type="text/javascript">
function myfunction()
{
var v = document.form1.Text_Box.value;
var chop =v;
var x = v.length;
var parts=chop.split("");
var winPtr = window.open('', '_blank', '...features...');
var str = '<html>\n';
str += '<head>\n';
str += '<title>Dynamic Page</title>\n';
str += '</head>\n';
str += '<body>\n';
str += '<p>\n';
str += v + '\n';
str += '<br>\n';
str += '<br>\n';
str += x + '\n';
str += '<br>\n';
str += '<br>\n';
str += parts + '\n';
str += '</p>\n';
str += '</body>\n';
str += '</html>';
winPtr.document.open();
winPtr.document.writeln(str);
winPtr.document.close();
}

</script>

</head>
<body>

<form name="form1">

<TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>

<input type="button"
onclick="myfunction()"
value="Font 1">

</form>

</body>
</html>

I'm aiming to perform some comparisons on the arrays elements, once
I've overcome this hurdle!

any and all help greatly appreciated!!!

Thanks In Advance!!!

Mar 6 '06 #1
2 1802
VK

stealth_sp...@yahoo.co.uk wrote:
Hi People wondering if anyone can help me with a problem I'm having

I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code that I have
var v = document.form1.Text_Box.value;

Why this intermediary variable below? Is Text_Box a textbox or
textarea? var chop =v;
var x = v.length;

This gives you an array with a single element containing the entire
string: var parts=chop.split("");
var parts = new Array();
for (var i=0; i<x; x++) {
parts[i] = v.charAt(i);
}
var winPtr = window.open('', '_blank', '...features...');
var str = '<html>\n';
str += '<head>\n';
str += '<title>Dynamic Page</title>\n';
str += '</head>\n';
str += '<body>\n';
str += '<p>\n';
str += v + '\n';
str += '<br>\n';
str += '<br>\n';
str += x + '\n';
str += '<br>\n';
str += '<br>\n';
This now an equivalent of str += parts.join(',') + '\n';
All array members will be joined back to one string
with comma as separator. Of course you can use another
separator of default one, say str += parts.join('-') + '\n'; str += parts + '\n';
str += '</p>\n';
str += '</body>\n';
str += '</html>';
winPtr.document.open();
winPtr.document.writeln(str);
winPtr.document.close();
}

</script>

</head>
<body>

<form name="form1">

<TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>

<input type="button"
onclick="myfunction()"
value="Font 1">

</form>

</body>
</html>

I'm aiming to perform some comparisons on the arrays elements, once
I've overcome this hurdle!

any and all help greatly appreciated!!!

Thanks In Advance!!!


Mar 6 '06 #2
st***********@yahoo.co.uk wrote:
Hi People wondering if anyone can help me with a problem I'm having

I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code that I have

what I have so far is a page that has a text box for user input, which
will take whatever the user enters, will count all the characters in
the inputted text which also then uses the "split" command, to split
the users text (string) into its individual pieces.

the code I have so far is as follows
<html>
<head>

<script type="text/javascript">
function myfunction()
{
var v = document.form1.Text_Box.value;
var chop =v;
var x = v.length;
var parts=chop.split("");
'parts' is now an array of the individual characters of 'chop'.
Incidentally, the length of 'parts' will be the same as v.length, i.e.
the value you assigned to 'x'. Now it is available in 3 places, x seem
redundant.

var winPtr = window.open('', '_blank', '...features...');
var str = '<html>\n';
When writing a document, you should make sure you write valid HTML,
including a doctype. You can omit the head and body tags if you like,
they are optional.

str += '<head>\n';
str += '<title>Dynamic Page</title>\n';
str += '</head>\n';
str += '<body>\n';
str += '<p>\n';
str += v + '\n';
str += '<br>\n';
str += '<br>\n';
str += x + '\n';
str += '<br>\n';
str += '<br>\n';
str += parts + '\n';
str += '</p>\n';
str += '</body>\n';
str += '</html>';
String concatenation using the '+=' compound operator is notoriously
slow in some browsers, though you won't notice it here. You could put
all this in an array:

var str = [
'<html>',
'<head>',
'<title>Dynamic Page<\/title>',
...
'<\/body>',
'<\/html>'
];
Note all the newlines are removed (less to type :-) ). Then write it with:

winPtr.document.write(str.join('\n'));

Adding newlines will just make the HTML look better when you use view
source, otherwise they aren't necessary.

A joined array may not be faster in all browsers, but it is in most. If
nothing else, it is less to type. The forward-slash character should be
quoted when it follows '<' when using document.write.

winPtr.document.open();
There is no need to call document.open(), it is called automatically
with document.write().

winPtr.document.writeln(str);
winPtr.document.close();
}

</script>

</head>
<body>

<form name="form1">
Form elements require an action attribute, even if it's empty.

<TEXTAREA NAME="Text_Box" COLS="20" ROWS="4" VALUE=""></TEXTAREA>
There is no HTML value attribute for a textarea, its value is its text
content.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-TEXTAREA>


<input type="button"
onclick="myfunction()"
value="Font 1">

</form>

</body>
</html>

I'm aiming to perform some comparisons on the arrays elements, once
I've overcome this hurdle!


What hurdle? What are you trying to do? There is almost never any need
to declare the length of a JavaScript array, it is self-adjusting. Just
insert elements wherever you want:

// Initialise an array named 'A'
var A = [];

// Give the 6th element a value
A[5] = 'blah';
After step 1, A has a length of 0. After adding an element at index 5,
it has a length of 6 - the length property of an array is special, it is
always larger than the highest index that has been given a value and it
adjusts automatically. Read the ECMAScript Language specification
Section 15.4, particularly section 15.4.4 Properties of the Array
Prototype Object, it is quite readable.

JavaScript arrays are also sparse, that is, they only take up memory for
the elements that have been given a value. Search the archives, there
have been some long and detailed threads on arrays within the last 6 months.

--
Rob
Mar 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by zwetan | last post: by
4 posts views Thread by bboyle18 | last post: by
13 posts views Thread by Oleg Konovalov | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.