0
var hash = "";
var count = 1;
var n = 3;
    for (var pound = 1; pound <=7; pound ++)
        {while (hash.length != count)
            hash += "#";
            hash += "\n";
            count += n;
            n ++;}
         console.log(hash);

Hi, I'm new to Javascript and fairly new to coding in general. I've spend a couple of hours analyzing my code above. BTW, I generated myself without any assistance. I wouldn't say I'm proud because I dont know why or how it works. I guess I get the basic math behind the work and when I check the values of the variables at the final end, the results match my calculation. But I dont know exactly what happens. It generates a triangle using '#'. I've tried removing the For loop to see exactly what the while loop is doing but I cant get a hang of it. Please take a look.

At first, in the first For loop and coming into the while loop, the value of hash.length is 0. Comparing it to count which 1, they do not match and so nextline. After adding "#" and "\n", hash.length becomes 2. But the code is read to the end making var count now 4 and var n equal to 4 also.

Now, the next loop does not happen inside the while loop and I've tried this out myself. It goes back to the For Loop. Checking the condition of "hash.length != count", this condition still holds as hash.length is 2 and count is now 4. Since they are not equal, shouldn't the while take repeat before going back to the For loop again?

3
  • This doesn't look like valid javascript. At the very least, the for's left parentheses is unmatched. This shouldn't run. Commented Mar 31, 2016 at 3:08
  • @Carcigenicate: It is valid. It executes. It is just horribly misformatted. Commented Mar 31, 2016 at 5:22
  • @Amadan Whoops! On the mobile app, the right end of the for loop is cut off until you scroll. Commented Mar 31, 2016 at 13:01

3 Answers 3

2
while (cond)
  statement1;
  statement2;

does not do what you think (and the indentation is lying to you). Reformating the code to be true to the logical flow gives you this:

var hash = "";
var count = 1;
var n = 3;
for (var pound = 1; pound <= 7; pound++) {
  while (hash.length != count)
    hash += "#";
  hash += "\n";
  count += n;
  n++;
}
console.log(hash);

While repeats only the very next statement. If you need it to repeat more, you have to use a compound statement (statement block) using the curly braces.

Here, for loop will go once for each row; within that, while will go once for each hash character.

Sign up to request clarification or add additional context in comments.

1 Comment

@Amadan...I appreciate your comment. Thank you very much.
0

Any loop without brackets { <-code here-> } only loops looking the following line.

So your while is doing only this:

while (hash.length != count)
    hash += "#";

So, removing your for loop, you will have hash = "#" (because count starts at 1, and hash.length is 0), and then the following will happen:

hash += "\n";
count += n;
n++;

Basically, hash will be # with a line break.

1 Comment

@GBarroso...Thanks a lot. It all makes sense now.
0

The answer to your question is that since the while loop has no brackets to indicate the code inside the loop. only one line, the
hash += "#";
gets execute, and when you get to the line that modifies the value of the count:
count += n;,
the while loop wont repeat since you already exited that loop.
Usually you want to keep the code as simple as possible. you can get the same result as the code you posted by simply writing it this way:

   var hash="";
   for (var pound = 1; pound <=7; pound ++)
        {
            hash += "#";
            console.log(hash);            
         }

you will use far less variables, the length of hash will greatly be reduce. sometimes too many string concatenation slows down code execution.

what the code above does is run a for loop seven times, each time, it appends a '#' to the hash string and displays it on the console...
same result, fewer lines, faster execution, and much easier to read.

if your are new to programming, I would recommend you start with a programming language the encourage good programming practice.

although PASCAL is not very popular anymore, learning it first before any C style language, would most likely get you up and running in writing very good code.

I know Pascal haters will probably criticize.. but, this language is good to get you to concentrate on algorithms and good programming style. which should be your first step before jumping into any language.

1 Comment

...Thanks a lot! Your comment was very helpful, I learnt a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.