why does the show function not show anything?

G

greenflame

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 = new Array(cols);
}
return output;
}
function copy1darr(input) {
output = new Array(input.length);
for (i=0;i<input.length;i++) {
output = input;
}
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[j] = input[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 + " ");
}
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[j] = input[j].toString();
L[j] = S[j].length;
T[j] = L[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[j].length < 6*(M[j]-S[j].length)) {
S[j] = '&nbsp;' + S[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.length-1;j++) {
document.write(S[j] + " ");
}
document.write(S[S.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);
 
R

RobG

greenflame said:
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 = 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 + " ");
}
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...

[...]
 
J

Jc

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
 
G

greenflame

OK. I will try this idea. Thanks for this advice. I will try to be more
carefull in the future.
 
G

greenflame

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?
 
R

RobG

greenflame said:
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[j].length < 6*(M[j]-S[j].length)) {
S[j] = '&nbsp;' + S[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.
 
G

greenflame

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 ])
 

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,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top