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>
Scripting LanguagesJavaScript

Avatar of undefined
Last Comment
fadi_fadi

8/22/2022 - Mon
cmalakar

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

Which is the other way ?
fadi_fadi

ASKER
This is my question Another way of writing the code.
Sudhindra A N

The above code is working...
In what way you are trying to rewrite it?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
fadi_fadi

ASKER
I'm trying to write it in another way ?
SOLUTION
cmalakar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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>
fadi_fadi

ASKER
Hello,
But i just want to change the function printtable() part only, Not the whole code.
thansk.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
cmalakar

>>But i just want to change the function printtable() part only, Not the whole code.

you didn't say that initially ?
fadi_fadi

ASKER
That's right,I'm so sorry.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
fadi_fadi

ASKER
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>
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
fadi_fadi

ASKER
Thanks