/    Sign up×
Community /Pin to ProfileBookmark

Newbie needs help with Arrays

Can somebody help, im rather new to Javascript and i would like to know about arrays.

I want to know how you can prompt for values using “window.prompt” and fill an array. After entering the values i would like to print the array to the screen using “document.write”

Any help will be greatly appreciated, and if possible some example coding would be of help

Thanks?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@fredmvNov 03.2003 — [size=3]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
[color=blue]/*
First, create a new array using the Array constructor and store
it in a variable called [b]theArray[/b].
*/[/color]
var theArray = new Array();

<i> </i> [color=blue]/*
<i> </i> Now we need to prompt the user for a number, we can do this using the prompt method.
<i> </i> This number will be the length of the array.
<i> </i> */[/color]
<i> </i> var sizeOfArray = parseInt(prompt("How many items will the array have?", ""));

<i> </i> [color=blue]/*
<i> </i> This next line may look complex, but once you understand what is going on, it is actually
<i> </i> quite simple. This is called a for loop and is a control structure in JavaScript (and many other
<i> </i> programming languages, for that matter). Alright, so lets look at the first part:
<i> </i> [b]for(i=0;[/b]
<i> </i> This is generally referred to as the [i]initalization[/i] stage of the loop. This tells the loop
<i> </i> to start off at zero. Now that the variable [b]i[/b] is defined as 0, we can now work with it.

<i> </i> Now for the next part of the for loop:
<i> </i> [b]i&lt;sizeOfArray;[/b]
<i> </i> This line almost explains itself - keep doing everything after the curleybrace until [b]i[/b] is
<i> </i> less than the [b]sizeOfArray[/b] variable. Remember that we previously prompted the user to enter
<i> </i> a value for this variable, the loop will keep comparing the value that was entered and stored in the
<i> </i> [b]sizeOfArray[/b] variable to the current state of the [b]i[/b] variable.

<i> </i> Finally, the last part of the for loop:
<i> </i> [b]i++)[/b]
<i> </i> What this does is [i]increment[/i] the current value of the [b]i[/b] variable. The term [i]increment[/i]
<i> </i> is just another way of saying [i]adding 1 to[/i].

<i> </i> And that's all there really is to a for loop.
<i> </i> */[/color]
<i> </i> for(i=0; i&lt;sizeOfArray; i++)
<i> </i> {
<i> </i> [color=blue]/*
<i> </i> You see how in the loop, we keep [i]incrementing[/i] the value of the [b]i[/b] variable? What we're doing
<i> </i> here is setting the current cell of the array to whatever [b]i[/b] is currently equal to. For the value of
<i> </i> this specific cell, we're using the [b]prompt[/b] method again to ask for some user input.
<i> </i> */[/color]
<i> </i> theArray[i] = prompt( "What will the value be for cell number " + i + "?", "" );
<i> </i> }

<i> </i> [color=blue]/*
<i> </i> Here we go again with the for loop. The exact same idea is used here.
<i> </i> */[/color]
<i> </i> for(i=0; i&lt;theArray.length; i++)
<i> </i> {
<i> </i> [color=blue]/*
<i> </i> Now we're using the [b]document[/b] object's [b]writeln[/b] method. It works just like the [b]write[/b]
<i> </i> method except for one small difference - it appends a newline ([b]n[/b]) character to the end of the output.

<i> </i> All we're doing here is going through every cell in the array and printing its value and then using a break ([b]&lt;br /&gt;[/b])
<i> </i> to display each array cell value on its own line.
<i> </i> */[/color]
<i> </i> document.writeln("theArray[" + i + "] = " + theArray[i] + "&lt;br /&gt;");
<i> </i> }
<i> </i> [color=blue]/*
<i> </i> And we're done! I hope that clears things up for you.
<i> </i> */[/color]
<i> </i> //]]&gt;
<i> </i> &lt;/script&gt;
<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;&lt;/body&gt;
&lt;/html&gt;[/size]
×

Success!

Help @Bigman1981 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...