I am having trouble making these triangles into diamonds. The other side needs t
ID: 3625624 • Letter: I
Question
I am having trouble making these triangles into diamonds. The other side needs to mirror the triangle. This program is using doubly nested loops to create a diamond shape. Plus my program keeps wanting to run... when I should be creating two diamonds on a pageAssignment:
Provide a documented textbox for the user to enter a size indicating the number of asterisks to use on a side for the ASCII-art diamonds.
[note: the height and width will be twice that, minus one]
Ensure that the minimum size will be two.
Else, display a message, and let the user re-enter.
Print two diamonds on successive sets of lines.
the first will be solid asterisks.
the second will be an outline of asterisks only.
<html>
<title></title>
<script language="javaScript">
function printStars(form)
{
var i=0,j=0;
var n=form.size.value;
var k=0
for(i = 0; i < n; i++)
{
for (j = 0; j <= i+k; j++)
document.write("*");
document.write("<br/>");
k++;
}
k=k-2;
for(i = n-1; i >0; i--)
{
for (j = i+k; j>0 ; j--)
document.write("*");
document.write("<br/>");
k--;
}
}
</script>
</head>
<body>
<form>
Enter Size: <input type="text" name="size" /><br />
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Explanation / Answer
<html>
<title></title>
<script language="javaScript">
function printStars(form)
{
var i=0,j=0;
var n=form.size.value;
var k=0
for(i = 0; i < n; i++)
{
for(j=n; j>i; j--)
document.write(" ");
for (j = 0; j <=2*i; j++)
document.write("*");
document.write("<br/>");
}
for(i = n-1; i >0; i--)
{
for(j=n; j>=i; j--)
document.write(" ");
for (j = 2*(i-1); j>=0 ; j--)
document.write("*");
document.write("<br/>");
}
}
</script>
</head>
<body>
<form>
Enter Size: <input type="text" name="size" /><br />
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.