Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > why does the show function not show anything?

Reply
Thread Tools

why does the show function not show anything?

 
 
greenflame
Guest
Posts: n/a
 
      06-13-2005
I have been working for some time on a script that will show a matrix
with the elements aligned on the right for sometime and finally got it
to work. Then I did some patching up and ran the script and it showed a
blank page and no error in the status bar. The script is rather long
and I call functions that do things that I may not need the functiosn
for but they are there because I so that task so many times that I jsut
made it into a function. OK here is the script:

function create2darr(rows,cols) {
output = new Array(rows);
for (i=0;i<rows;i++) {
output[i] = new Array(cols);
}
return output;
}
function copy1darr(input) {
output = new Array(input.length);
for (i=0;i<input.length;i++) {
output[i] = input[i];
}
return output;
}
function copy2darr(input) {
output = create2darr(input.length,input[0].length);
for (i=0;i<input.length;i++) {
for (j=0;j<input[0].length;j++) {
output[i][j] = input[i][j];
}
}
return output;
}
function findmax(input) {
Temp = copy1darr(input);
for (i=0;i<Temp.length;i++) {
for (j=0;j<Temp.length-1;j++) {
Temp[j] = Math.max(Temp[j],Temp[j+1]);
}
}
return Temp[0];
}
function show(input) {
if (input[0].length == undefined) {
document.write("<b>[</b>");
for (i=0;i<input.length-1;i++) {
document.write(input[i] + " ");
}
document.write(input[input.length-1] + "<b>]</b><br>");
} else {
S = create2darr(input.length,input[0].length);
L = create2darr(S.length,S[0].length);
T = create2darr(S[0].length,S.length);
M = new Array(S[0].length);
for (i=0;i<input.length;i++) {
for (j=0;j<input[0].length;j++) {
S[i][j] = input[i][j].toString();
L[i][j] = S[i][j].length;
T[j][i] = L[i][j];
}
}
for (as=0;as<M.length;as++) {
M[as] = findmax(T[as]);
}
for (i=0;i<S.length;i++) {
for (j=0;j<S[0].length;j++) {
while (S[i][j].length < 6*(M[j]-S[i][j].length)) {
S[i][j] = '&nbsp;' + S[i][j];
}
}
for (i=0;i<S.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");
break;
case S.length-1:
document.write("<font face=symbol>ë</font>");
break;
default: document.write("<font
face=symbol>ê</font>");
}
for (j=0;j<S[i].length-1;j++) {
document.write(S[i][j] + " ");
}
document.write(S[i][S[i].length-1]);
switch (i) {
case 0:
document.write("<font face=symbol>ù</font>");
break;
case S.length-1:
document.write("<font face=symbol>û</font>");
break;
default: document.write("<font
face=symbol>ï</font>");
}
document.write("<br>");
}
}
}
a = [
[2.33,43463, 45676786],
[334, 23, 5.3],
[ 3, 2.6, 57087]
];
show(a);

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-13-2005
greenflame wrote:
> I have been working for some time on a script that will show a matrix
> with the elements aligned on the right for sometime and finally got it
> to work. Then I did some patching up and ran the script and it showed a
> blank page and no error in the status bar. The script is rather long
> and I call functions that do things that I may not need the functiosn
> for but they are there because I so that task so many times that I jsut
> made it into a function. OK here is the script:


I'm not going to debug this entire script, here's a start. When you
declare a variable without the var keyword, it becomes global so it
exists outside your function. You re-use 'output', 'input', 'i',
etc. as global variables and no doubt they are being modified in ways
you are not aware of. That the script worked at all is likely pure
luck.

>
> function create2darr(rows,cols) {
> output = new Array(rows);


Keep 'output' local:

var output = ...

> for (i=0;i<rows;i++) {


Keep 'i' local.

for (var i=0;i<rows;i++) {

> output[i] = new Array(cols);
> }
> return output;
> }
> function copy1darr(input) {
> output = new Array(input.length);


Here is 'output' again - keep it local:

var output = new Array(input.length);

> for (i=0;i<input.length;i++) {


And 'i' again...

for (var i=0;i<input.length;i++) {

And so on for all your functions.

[...]
> function show(input) {
> if (input[0].length == undefined) {
> document.write("<b>[</b>");
> for (i=0;i<input.length-1;i++) {
> document.write(input[i] + " ");
> }
> document.write(input[input.length-1] + "<b>]</b><br>");


There are much more efficient ways to write the output than many
document.write statements. When you've got your output correct and
going to an alert or a simple document.write, then worry about more
complex output.

> } else {
> S = create2darr(input.length,input[0].length);
> L = create2darr(S.length,S[0].length);
> T = create2darr(S[0].length,S.length);
> M = new Array(S[0].length);
> for (i=0;i<input.length;i++) {


Here we are again with a slew of globals that don't need to be (and
'i' is caught up in the mix yet again).

> for (j=0;j<input[0].length;j++) {


ad infinitum...

[...]

--
Rob
 
Reply With Quote
 
 
 
 
Jc
Guest
Posts: n/a
 
      06-13-2005
greenflame wrote:
<summary>
why does the show function in the below code not show anything?
</summary>

FYI: It makes it easier to quote your post if you also ask your
question in the body of the post.

It depends on when you are calling those functions. The
document.write() call isn't intended to be used after the page has
finished loading, it is intended to be used by inline script blocks
that get executed before the page is fully parsed.

For example, if you are calling your function in an onclick event, this
would be occurring well after the page has loaded. In that case, you
should be using another technique to modify the DOM, such as
document.createElement() and appendChild(). Using innerHTML or
insertAdjacentHTML allow you to add blocks of HTML in one call, but
they are not standard DOM methods and are not guaranteed to work in all
browsers.

Also, when you use document.write(), you should really be calling
open() and close() before and after the call. Do a quick web search to
pull up some documentation, or check out this link (scroll down to the
"The write() Method" section):
http://www.unix.org.ua/orelly/web/jscript/ch14_01.html

 
Reply With Quote
 
greenflame
Guest
Posts: n/a
 
      06-14-2005
OK. I will try this idea. Thanks for this advice. I will try to be more
carefull in the future.

 
Reply With Quote
 
greenflame
Guest
Posts: n/a
 
      06-16-2005
I tryed putting var in front of all the variables. And giving the
variables better names and it still does not show anything. Could
there be anything else wrong? Or maybe do you think I should try all
over again?

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-17-2005
greenflame wrote:
> I tryed putting var in front of all the variables. And giving the
> variables better names and it still does not show anything. Could
> there be anything else wrong? Or maybe do you think I should try all
> over again?
>


Use Firefox, you are missing a single '}'.

[...]
for (i=0;i<S.length;i++) {
for (j=0;j<S[0].length;j++) {
while (S[i][j].length < 6*(M[j]-S[i][j].length)) {
S[i][j] = '&nbsp;' + S[i][j];
}
}
--------^

for (i=0;i<S.length;i++) {
switch (i) {
case 0:
document.write("<font face=symbol>é</font>");
break;
case S.length-1:
[...]

Put a blank line after each large code block, makes life easier...

Incidentally, what are you trying to do? The code in general can be
significantly optimised.


--
Rob
 
Reply With Quote
 
greenflame
Guest
Posts: n/a
 
      06-18-2005
OK. I got it to work. Thanks for putting up with me . The problems
were the missing brace you pointed out, one of the for loops had i=0
and I incremented by saying as++ when as is not a variable I defined,
and the while loop only added upto one space. The steps I am trying to
follow are:

1. Convert the matrix to a string version
2. Make an array of lengths of each element by rows
3. Transpose this array to obtain the lengths of the elements
by columns instead of rows
4. Make an array that containes the maximum of each row of the
transposed array thus telling me the length of the longest
element in that column of the matrix
5. Add space as needed to the elements in the string version
6. Loop over the rows
7. If write appropriate beginning bracket (ie appropriate [)
7. Loop over the columns-1 and write that element with a space
added at the end of each element.
8. Write the final elemnet of the row.
9. Write the appropriate ending bracket (ie appropriate ])

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does the crypto key show in "show run" on some switches and notothers? ttripp Cisco 2 10-19-2010 08:45 AM
Why a member function will override an free function with the samename even if the arguments does not match? Peng Yu C++ 4 09-20-2008 02:00 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments