Making a diamond with number of stars that user inputs
Hi,
I'm new here, and I'm a programming newb but am very interested in learning and possibly pursuing programming in the future.
As such, I'm taking an intro to Javascript class at my university, and our homework is to make a function that when called upon makes a diamond out of *'s. It is supposed to do so by prompting a user for an int and then makes a diamond shaped object with the inputted number of stars in the middle of the diamond.
an example of what this would look like (if a user entered in "5" to the prompt):
*<br>
***<br>
*****<br>
***<br>
*<br>
I am not sure how to set up this for loop, and have been trying so many different codes and have come nowhere close.
If you make an honest effort--if you write some code, for example, that almost works but not quite--*THEN* we can help you. But no, we can't do your homework for you.
I have to make a diamond in Javascript.. I have the odd case no problem But i am having a little trouble with the even case.. I am only going to post the top half of code cause the bottom is just a repeat.. I got it all but I can not figure how to get the one * on the very top middle
var input=10;
for (var topheight = input+1; topheight >= 1; topheight -= 2)
{
for (var space = 1; space <= topheight; space++)
{
document.write(" ");
}
if (topheight % 2 != 0)
{
for (var asterisk = input; asterisk >= topheight; asterisk--)
{
document.write(" * ");
}
}
document.write ("<br> ");
}
this will be with user input later but now i just hard coded a var input
thank you for any help
I think you have made an effort, but you need to use <pre> tags for the spacing. Note the number of rows of asterisks in the diamond must be odd!
Here you are:-
Code:
<html>
<head>
<title>Diamond Pattern</title>
</head>
<body>
<script type = "text/javascript">
function diamond(n) {
n = parseInt(n,10);
if (n% 2 == 0 ) {
alert( "Odd numbers only!" );
return false;
}
document.write("<pre>");
// top
for(i=1; i<=n; i+=2) {
for (s=0; s<(n-i/2-n/2); s++) { // -n/2 positions diamond pattern at left hand edge of page
document.write(" ");
}
for (j=1; j<=i; j++) {
document.write("*");
}
document.write("<br>")
}
// bottom
for (i=n-2; i>=0; i=i-2) {
for (s=0; s<(n-i/2-n/2); s++) {
document.write(" ");
}
for (j=1; j<=i; j++) {
document.write("*");
}
document.write("<br>")
}
document.write("</pre>");
}
diamond(7);
</script>
</body>
</html>
"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 04-05-2012 at 04:22 PM..
Reason: Improved
I don't see how you can have a true diamond which you describe as even case. It requires the single asterisk at the top to be output to a position between the two below it.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Yes not sure they call it true diamond but that is what they want. I am just having trouble finding a way to get the single one on top and bottom but once i figure out the top the bottom will be no problem..
<script type = "text/javascript">
function diamond( n )
{
n = parseInt(n,10);
var i, s;
// top: 1 to n
document.write("<pre>");
for(i = 1; i <= n; ++i )
{
// write n-i spaces:
for ( s = 1; s <= n-i; ++s )
{
document.write(" ");
}
// then write i asterisk+space sets:
for ( s = 1; s <= i; ++s )
{
document.write("* ");
}
document.write("\n");
}
// bottom: n-1 down to 1
for(i = n-1; i >= 1; --i )
{
// write n-i spaces:
for ( s = 1; s <= n-i; ++s )
{
document.write(" ");
}
// then write i asterisk+space sets:
for ( s = 1; s <= i; ++s )
{
document.write("* ");
}
document.write("\n");
}
document.write("</pre>");
}
diamond(7);
document.write("<hr>\n");
diamond(10);
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Yes not sure they call it true diamond but that is what they want. I am just having trouble finding a way to get the single one on top and bottom but once i figure out the top the bottom will be no problem..
There's a few examples on google as well. This is a common homework exercise.