Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-26-2011, 01:25 AM   PM User | #1
digitalballoon
New to the CF scene

 
Join Date: Oct 2011
Location: Western MA
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
digitalballoon is an unknown quantity at this point
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):
&nbsp;&nbsp;&nbsp;&nbsp;*<br>
&nbsp;&nbsp;***<br>
*****<br>
&nbsp;&nbsp;***<br>
&nbsp;&nbsp;&nbsp;&nbsp;*<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.

Any help or insight would be so much appreciated.
digitalballoon is offline   Reply With Quote
Old 10-26-2011, 03:05 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
you mean this?
Code:
  *
 ***
*****
 ***
  *
??

First of all read this:
http://www.codingforums.com/rules.htm
especially rule 1.5

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 will give you one hint:

Start your page with something like this:
Code:
<html>
<head>
<style type="text/css">
* { font-family: courier; }
</style>
If you use a proportional font (such as arial, used in these forums) you'll never get the spacing right. Even if you do (correctly!) use &nbsp;
__________________
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.
Old Pedant is offline   Reply With Quote
Old 04-05-2012, 02:10 AM   PM User | #3
ueskim
New to the CF scene

 
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ueskim is an unknown quantity at this point
Making a diamond in Javascript

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("&nbsp");
}
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
ueskim is offline   Reply With Quote
Old 04-05-2012, 08:48 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Old 04-05-2012, 12:40 PM   PM User | #5
ueskim
New to the CF scene

 
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ueskim is an unknown quantity at this point
reply

Thank you.. I have the odd case.. we have to write an even case so it would look like this

*
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
*

i am doing the input check in another function so that I have an even case function and an odd case function I can call later

sorry I am not sure how to maintain the shape of the diamond on the post it keeps taking away my spaces..

Last edited by ueskim; 04-05-2012 at 12:43 PM..
ueskim is offline   Reply With Quote
Old 04-05-2012, 03:50 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.

Last edited by Philip M; 04-05-2012 at 06:45 PM..
Philip M is offline   Reply With Quote
Old 04-05-2012, 06:17 PM   PM User | #7
ueskim
New to the CF scene

 
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ueskim is an unknown quantity at this point
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..


thank you for all your help
ueskim is offline   Reply With Quote
Old 04-05-2012, 06:40 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I would check your assignment, as I think it is impossible. The number of rows of asterisks making up the diamond must be odd!

What you could do is repeat the middle line of the diamond thus making the number of rows even (but it is then not truly a diamond). In the above code

Code:
// bottom
for (i=n; i>=0; i=i-2) {
__________________

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 06:45 PM..
Philip M is offline   Reply With Quote
Old 04-06-2012, 02:50 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
HUH?

Easy:
Code:
     *
    * *
   * * *
  * * * *
 * * * * *
* * * * * *
 * * * * *
  * * * *
   * * *
    * *
     *
The "trick" is to just use spaces to make it look right. And then it looks essentially the same with odd number as even number.
Code:
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
Excepting for distortion, it looks the same sideways. If we used square images, we could make it look the same sideways.

NOW see if you can figure it out.
__________________
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.

Last edited by Old Pedant; 04-06-2012 at 02:54 AM..
Old Pedant is offline   Reply With Quote
Old 04-06-2012, 03:06 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh, w.t.h.
Code:
<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.
Old Pedant is offline   Reply With Quote
Old 04-06-2012, 03:10 AM   PM User | #11
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
Quote:
Originally Posted by Philip M View Post
I think it is impossible.
No it's not

In addition to old pedant's hint, you could use 2 spaces as a unit space so that the first star can be placed at 3.5 stars in his 6 star diamond.
Mishu is offline   Reply With Quote
Old 04-06-2012, 03:12 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Or a more efficient way:
Code:
<script type = "text/javascript">
var spaces = " ";
var rows = "* ";
while( rows.length < 200 )
{
    spaces += spaces; // doubles length each time
    rows += rows; // ditto
}

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:
        document.write( spaces.substring(0,n-i) );

        // then write i asterisk+space sets:
        document.write( rows.substring(0,i+i) + "\n" );
    }
    // bottom: n-1 down to 1
    for(i = n-1; i >= 1; --i )
    {
        // write n-i spaces:
        document.write( spaces.substring(0,n-i) );

        // then write i asterisk+space sets:
        document.write( rows.substring(0,i+i) + "\n" );
    }
    document.write("</pre>");
}
diamond(9);
document.write("<hr>\n");
diamond(6);

</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.
Old Pedant is offline   Reply With Quote
Old 04-06-2012, 03:15 AM   PM User | #13
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
Quote:
Originally Posted by ueskim View Post
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.
Mishu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.