Avatar of fadi_fadi
fadi_fadi
 asked on

out pyramid using Javascript

Dear Expert,

I'm trying to fix this code :

function println(str) {

}

function set_indent(i) {
   var indent = i;
}

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

function printtable(size) {
   set_indent(0);
   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;
printtable(5);

to make the output look like this:


This is a pyramid

        *

       ***

      *****

     *******

    *********

But it's doesn't work... Can you help to find the bug in the code?
and help me to finishing the function println.


Thanks
Scripting LanguagesJavaScript

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
Michel Plungjan

function printLn(str) {
  document.write(str);
]

or just replace println with document.write

Be aware that you need to do this inline and have a non=proportional font or write inside <pre></pre> tags

Also this does nothing

function set_indent(i) {
   var indent = i;
}
ASKER CERTIFIED SOLUTION
jrm213jrm213

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
Michel Plungjan

this
function set_indent(i) {
   var indent = i;
}

should likely be

var indent=0;
function set_indent(i) {
   indent = i;
}


PS JRM you want the second <pre> to be </pre>
jrm213jrm213

notes:

1. use document.write to write out your str in your println function.
2. declare variable indent at the page/script level instead of inside your function set_indent
3. Actually add the <pre> and </pre> tags to the items you are writing out
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
jrm213jrm213

line 19 of the script i posted should be

   return pre + str + "<\pre>";
jrm213jrm213

grr...
   return pre + str + "</pre>";
fadi_fadi

ASKER
there is no other way to rewrite the

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 += "*";
       }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jrm213jrm213

message ID:36345703

I am not sure what you are asking. You want to write that function differently?

Michel Plungjan

Of course. Million of ways

This one does not QUITE make it, but I am off to bed

document.write("<pre>")
var stars = "***********";
var indents = "            ";
for (var indent = 10;indent>0;indent--) {
  document.write("\n"+indents.substring(0,indent/2)+stars.substring(indent,stars.length));
}
document.write("</pre>")
fadi_fadi

ASKER
give one way of the millon?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Michel Plungjan

I just did - did you not see the code?

Here is a demo http://jsfiddle.net/mplungjan/Cyyy5/
a little crooked but I suck at math
fadi_fadi

ASKER
Of course i did,
But I mean the function printtable.
Thanks
fadi_fadi

ASKER
Thanks Experts
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Michel Plungjan

A split would have been nice