473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 1915
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
16
by: zwetan | last post by:
Hi, often I see people telling that "javascript sucks" - is it because of the DOM with JS ? - is it because of the language in itself ? - is it because browsers/hosts differences ? - is it...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
13
by: Oleg Konovalov | last post by:
Hi, I am working on a web application which among other things uses DHTML, Java and Javascript. It populates web page based on the contents of the database (resultset), and next to each row...
13
by: mowsen | last post by:
Hello Group, i'm using a little "ajax" loader script to dynamically load files into different "div" tags on my main site. the code for this part looks like: function loader() { var args =...
16
by: drwyness | last post by:
Hello, Please help with the following problem it is causing me some headaches. The following javascript code is designed to fill in text boxes with numbers on an online game (tribalwars). It...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.