Jump to content
Killersites Community
rebelsoul777

Javascript ch7 while loop problem

Recommended Posts

Hi everyone,

I need some help regarding the topic of while loop mentioned in ch7 lesson3. I'm stuck for first time in this course. Till now everything was super easy, but now I really don't understand what's going on in this loop.

There is that loop:

var loopCount = 5;

				while(loopCount > 0) {

						var target = document.getElementById("target");
						target.innerHTML = "LoopCount: " + loopCount + "<br>" + target.innerHTML;
						console.log("LoopCount is now: " + loopCount);
						loopCount = loopCount -1;
					}

In console result looks like this:

LoopCount is now: 5
LoopCount is now: 4
LoopCount is now: 3
LoopCount is now: 2
LoopCount is now: 1
End loop

but in html it starts from 1 to 5, why?:

LoopCount: 1
LoopCount: 2
LoopCount: 3
LoopCount: 4
LoopCount: 5

I don't understand that line:

target.innerHTML = "LoopCount: " + loopCount + "<br>" + target.innerHTML;

Since in the beginning variable loopCount is set to 5, why in html it starts with 1. Where is it come from?

Thanks

 

 

Share this post


Link to post
Share on other sites

Hi!

The HTML is rendering in reverse order because of something to do with the innerHTML is being used. 

Can you post the HTML code?

  • Like 1

Share this post


Link to post
Share on other sites

I'm trying to past some code but I get this error:

Forbidden

You don't have permission to access /community/index.php on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

Share this post


Link to post
Share on other sites

Ok, Stefan, thanks for your time.

I think I get it now. I had to think that while loop over.

It's 2:30 in the morning right now, so I guess I might be crazy.  :)

Edited by rebelsoul777

Share this post


Link to post
Share on other sites
5 hours ago, rebelsoul777 said:

Ok, Stefan, thanks for your time.

I think I get it now. I had to think that while loop over.

It's 2:30 in the morning right now, so I guess I might be crazy.  :)

Hi. Cool that you got it on your own.

As far as code posting, I believe with this new forum software, you have to you use code options in the editor.

Stef

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×