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>
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>
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?
In what way you are trying to rewrite it?
ASKER
I'm trying to write it in another way ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<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("*");//writ e 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>
document.write("<center>")
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("*");//writ
}
document.write("<br/>"); //write a br tag, meaning new line, after every star in the row has been created
}
document.write("</center>"
</script>
ASKER
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.
thansk.
>>But i just want to change the function printtable() part only, Not the whole code.
you didn't say that initially ?
you didn't say that initially ?
ASKER
That's right,I'm so sorry.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
..........................
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Which is the other way ?