Click to See Complete Forum and Search --> : Java script help


newtda
10-12-2003, 07:33 PM
Hello people I am new to the board and I am new to java script. I am actually taking a class in java script and need alot of help. here is what we have to come up with.



1) Prompts for how many bars. For example, lets say 3.

2) Then prompts you 3 times.
a) you enter a 1
b) you enter a 4
c) you enter a 3

3) It will display 3 rows (from 1)
Each row will contain the number of images, like
a) row 1, will show 1 image
b) row 2, will show 4 images
c) row 3, will show 3 images

-Make sure it displays all of them after you do all the prompts.
-You can replace ALL the number with any number.


Here is what I came up with:

<html>
<head>
<script>

var the_image = new array();

var bars = prompt("how many images do you want?","");

for (var loop=0; loop < image; loop++)
{
var value = prompt("how long is this chart? (1-5)","");
the_image[loop] = value;
}
for (var loop=0; loop < image; loop++)
{
drawbars(the_image[loop])
}

{

function drawbars(the_numbber)
for (var loop=0; loop < the_image; loop++)
{
window.document.writte("<img src=ampsub.jpg>");
}


</script>
</head>


<body>
</body>

jrbp
10-12-2003, 07:52 PM
Heres one problem right off the bat:

{
window.document.writte("<img src=ampsub.jpg>");
}

There is 1 too many T's in the Document.write , it should be like this:

{
window.document.write("<img src=ampsub.jpg>");
}

stepheno
10-12-2003, 09:47 PM
Your main problem was your for loops. You kept trying to use image as the top end of the loop.
For you first loop you want to use bars as this is the loop that controls the question for how long the bars are supposed to be. You need to ask the question once for each bar.
For your second loop I also used bars as the top end control. This is the loop that controls displaying of each bar.
In your third loop use the number you stored in the array as the top end of your loop because this will control how many images are printed in each line.
Adding the line window.document.writeln('<br>'); at the end of your drawbars function causes the page to start a new line for the next bar of images.

<html>
<head>
<script language="JavaScript">

var the_image = new Array();

var bars = prompt("how many images do you want?","");

for (var loop=0; loop < bars; loop++)
{
var value = prompt("how long is this chart? (1-5)","");
the_image[loop] = value;
}

for (var loop=0; loop < bars; loop++)
{
drawbars(the_image[loop]);
}



function drawbars(the_numbber) {
for (var loop=0; loop < the_numbber; loop++)
{
window.document.write('<img src=image.jpg>');
}
window.document.writeln('<br>');

}
</script>
</head>


<body>

</body>

newtda
10-12-2003, 09:58 PM
i keep on getting a undefined array in this line


var the_image = new array();

Look up for all the codeing

Charles
10-13-2003, 04:25 AM
JavaScript is case sensitive and follows the Java Naming Conventions (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html). The other thing that you need to know is that in JavaScript arrays are members of the Array object class.

var the_image = new array();

should have been written

var theImage = new Array();

lillu
10-13-2003, 04:43 AM
Make sure the_number is NOT the_numbber

<html>
<head>
<script language="JavaScript">

var the_image = new Array();

var bars = prompt("How many images do you want?","");

for (var loop=0; loop < bars; loop++)
{
var value = prompt("How long is this chart? (1-5)","");
the_image[loop] = value;
}

for (var loop=0; loop < bars; loop++)
{
drawbars(the_image[loop]);
}


function drawbars(the_number) {
for (var loop=0; loop < the_number; loop++)
{
window.document.write('<img src=image.jpg>');
}
window.document.writeln('<br>');

}
</script>
</head>


<body>

</body>
</html>