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

S

stealth_spoof

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!!!
 
V

VK

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 = 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';
 
R

RobG

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.

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top