Link to home
Start Free TrialLog in
Avatar of fadi_fadi
fadi_fadi

asked on

rewrite the function in JavaScript

Dear Expert,
I'm trying to rewite the function printtable() in another way,but the code is dosnot work?
Can you help me to find the soultion?

<html>
<head>
<script type="text/javascript">

var indent;

function println(str) {
document.write(str);
                      }

function set_indent(i) {
  indent = i;
                       }

function mystr(str) {
   pre = "<pre>";
   alert(indent);
   for (var i=0; i<indent;i++) {
       pre+=" ";
   }
   return pre + str + "<pre>";
}

function printtable(size) {
   println("This is a pyramid");
   for (var i=0; i<size; i++) {
       set_indent(size*2-i);
       s="";
       for (var j=0; j<i*2+1;j++) {
           s += "*";
       }
       println(mystr(s));
   }
}

indent=0;

</script>
</head>
<body>
<script type="text/javascript">
printtable(5);
</script>
</body>
</html>
Avatar of cmalakar
cmalakar
Flag of India image

>>I'm trying to rewite the function printtable() in another way,but the code is dosnot work?

Which is the other way ?
Avatar of fadi_fadi
fadi_fadi

ASKER

This is my question Another way of writing the code.
The above code is working...
In what way you are trying to rewrite it?
I'm trying to write it in another way ?
SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pratima
<script type="text/javascript" language="javascript">

document.write("<center>"); //write a center tag to make sure the pyramid displays correctly(try it without this step to see what happens)
for(var i = 0; i <= 6; i++) //a loop, this counts from 0 to 10 (how many rows of stars)
{
      for(var x = 0; x <= i; x++)// a loop, counting from 0 to whatever value i is currently on
      {
            document.write("*");//write a * character
      }
      document.write("<br/>"); //write a br tag, meaning new line, after every star in the row has been created
}
document.write("</center>"); //close the center tag, opened at the beginning

</script>
Hello,
But i just want to change the function printtable() part only, Not the whole code.
thansk.
>>But i just want to change the function printtable() part only, Not the whole code.

you didn't say that initially ?
That's right,I'm so sorry.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But the function of the code is different:
........................................................................................................................
<html>
<head>
<script type="text/javascript">
var indent;
function println(str) {
      document.write(str);
}

function set_indent(i) {
  indent = i;
}

function mystr(str) {
   pre = "<pre>";
   alert(indent);
   for (var i=0; i<indent;i++) {
       pre+=" ";
   }
   return pre + str + "<pre>";
}

var indent = 0;

function printtable(size)
{
   println("This is a pyramid");
   for (var i=0; i<size; i++)
   {
       indent = size*2 - i;
       s = "";
         for ( var j=0; j<indent; j++ )
         {
               s += " ";
         }
       for (var j=0; j<i*2+1;j++)
         {
           s += "*";
       }
       document.write( "<pre>" + s + "</pre>" );
   }
}

</script>
</head>
<body>
<script type="text/javascript">
printtable(5);
</script>
</body>
</html>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks