javascript
Brief description  about Online courses   join in Online courses
View Amit Kumar Dubey 's Profile

Loop Assignment - pattern generation

Hi Sir,

I am trying to do the Loop assignment - pattern generation for Write a JavaScript code so that numbers appear in following format,
for n=5. Input 'n' value from user.
I have got the below code working but doesnt have the spaces according to the picture. Please check and suggest.

My Code:

user = prompt("Type any number");
k = "";
for (i=1; i<=user; i ) {
j = i.toString();
document.write(k j "<br>");
k = k j;
}
k = k.toString();
for (x=user; x>0; x--) {
document.write(k.substr(0, (x-1)) "<br>");
}
Asked by Amit Kumar Dubey | Apr 29, 2013 |  Reply now
Replies (2)
View teacher siliconindia 's Profile
Hi Amit Kumar Dubey

You can try this code .

<script language="javascript">
document.write("<center>");
for(var i = 1; i <= 5; i++)
{
for(var x = 1; x <=i; x++)
{
document.write(x+"&nbsp;&nbsp;");
}
document.write("<br/>");
}

for(var i = 1; i <= 5; i++)
{
for(var x = 1; x <=5-i; x++)
{
document.write(x+"&nbsp;&nbsp;");
}
document.write("<br/>");
}

document.write("</center>");
</script>

May 1, 2013
View amit kumar dubey 's Profile
Any Teacher. Please reply. I need to submit the assignment in time.
Apr 30, 2013