why does the show function not show anything?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • greenflame

    #1

    why does the show function not show anything?

    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(row s,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.len gth);
    for (i=0;i<input.le ngth;i++) {
    output[i] = input[i];
    }
    return output;
    }
    function copy2darr(input ) {
    output = create2darr(inp ut.length,input[0].length);
    for (i=0;i<input.le ngth;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.len gth;i++) {
    for (j=0;j<Temp.len gth-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.le ngth-1;i++) {
    document.write( input[i] + " ");
    }
    document.write( input[input.length-1] + "<b>]</b><br>");
    } else {
    S = create2darr(inp ut.length,input[0].length);
    L = create2darr(S.l ength,S[0].length);
    T = create2darr(S[0].length,S.lengt h);
    M = new Array(S[0].length);
    for (i=0;i<input.le ngth;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.leng th;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>&#2 33;</font>");
    break;
    case S.length-1:
    document.write( "<font face=symbol>&#2 35;</font>");
    break;
    default: document.write( "<font
    face=symbol>&#2 34;</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>&#2 49;</font>");
    break;
    case S.length-1:
    document.write( "<font face=symbol>&#2 51;</font>");
    break;
    default: document.write( "<font
    face=symbol>&#2 39;</font>");
    }
    document.write( "<br>");
    }
    }
    }
    a = [
    [2.33,43463, 45676786],
    [334, 23, 5.3],
    [ 3, 2.6, 57087]
    ];
    show(a);

  • RobG

    #2
    Re: why does the show function not show anything?

    greenflame wrote:[color=blue]
    > 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:[/color]

    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.
    [color=blue]
    >
    > function create2darr(row s,cols) {
    > output = new Array(rows);[/color]

    Keep 'output' local:

    var output = ...
    [color=blue]
    > for (i=0;i<rows;i++ ) {[/color]

    Keep 'i' local.

    for (var i=0;i<rows;i++) {
    [color=blue]
    > output[i] = new Array(cols);
    > }
    > return output;
    > }
    > function copy1darr(input ) {
    > output = new Array(input.len gth);[/color]

    Here is 'output' again - keep it local:

    var output = new Array(input.len gth);
    [color=blue]
    > for (i=0;i<input.le ngth;i++) {[/color]

    And 'i' again...

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

    And so on for all your functions.

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

    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.
    [color=blue]
    > } else {
    > S = create2darr(inp ut.length,input[0].length);
    > L = create2darr(S.l ength,S[0].length);
    > T = create2darr(S[0].length,S.lengt h);
    > M = new Array(S[0].length);
    > for (i=0;i<input.le ngth;i++) {[/color]

    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).
    [color=blue]
    > for (j=0;j<input[0].length;j++) {[/color]

    ad infinitum...

    [...]

    --
    Rob

    Comment

    • Jc

      #3
      Re: why does the show function not show anything?

      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.create Element() and appendChild(). Using innerHTML or
      insertAdjacentH TML 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):


      Comment

      • greenflame

        #4
        Re: why does the show function not show anything?

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

        Comment

        • greenflame

          #5
          Re: why does the show function not show anything?

          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?

          Comment

          • RobG

            #6
            Re: why does the show function not show anything?

            greenflame wrote:[color=blue]
            > 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?
            >[/color]

            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>&#2 33;</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

            Comment

            • greenflame

              #7
              Re: why does the show function not show anything?

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

              Comment

              Working...