Jump to content

Beginner In Need Of Help With While Tag.


JereHakala

Recommended Posts

Hello, I have been trying to do this school assignment for like 5 hours now, but I just can't figure it out.I made a loop using While, and it writes the numbers from 1 to 10 ( http://hakala.shelli.fi/javascript/5.2.html )Heres just the JS

var k;var summa;summa = 1;while (summa<11){document.write(summa);document.write(" <br> ");summa++;}

How can I make counter that counts the sum of all the numbers in there? (Which is 55)From google I found one that did this, but it used the For tag, I need to use while tag.Thanks.

Link to comment
Share on other sites

You mean something didn't work. If you want help, please be more specific. I won't give you the solution, and I hope no one else does either. But if you provide more information than "it didn't," maybe you can get some help.

Link to comment
Share on other sites

That kind of attitude isn't going to stimulate a lot of help either. DD is just trying to help you learn what to do and how to figure out what's wrong instead of just showing you, which is a good idea.Like I said before you are going to need two variables, which you have (k and summa). You need one to control your loop (summa) and one to increment a value (k). Since you're printing out the values of summa (the numbers 1 thru 10) and you want to figure out the total value of those values, what do you think you're going to have to add to k every time the loop runs?

Link to comment
Share on other sites

That kind of attitude isn't going to stimulate a lot of help either. DD is just trying to help you learn what to do and how to figure out what's wrong instead of just showing you, which is a good idea.Like I said before you are going to need two variables, which you have (k and summa). You need one to control your loop (summa) and one to increment a value (k). Since you're printing out the values of summa (the numbers 1 thru 10) and you want to figure out the total value of those values, what do you think you're going to have to add to k every time the loop runs?
var i = 1;var summa = 0;while (i <= 10){summa += i; i += 1; } document.write(summa);Got that solution with real help hmm.
Link to comment
Share on other sites

I'm glad you're not studying to be a doctor.
edit by moderator:I sincerely appreciate all attempts to assist me in my endeavor, but sadly I have been continually frustrated with this basic concept for quite some time and I would like to have the solution presented to me. Since the solution is not being outright presented to me, I think it's best to burn my bridges and insult people so that they think twice about trying to help me the next time I have a question.
Link to comment
Share on other sites

insults insults insults
That's uncalled for. He didn't ask me to remove it, I just did. He has a good point, not showing the solution helps you learn so long as you're given proper guidance toward the solution. DD is one of the most experienced, knowledgeable and polite members of this forum. DD was giving you the guidance, you just refused to follow it. And I have to say, I think I regret giving you as much information as I did.
Link to comment
Share on other sites

You said it's a school assignment. I assumed you wanted to learn something. Which is great, because this board is all about teaching and learning. Most of those 3.7K posts is me trying to help someone. Sometimes they even say thank you.Receiving a solution wrapped up like a present isn't learning. At the college where I teach we have a special word for it, actually. Want to guess what it is? :)

Link to comment
Share on other sites

OTOH, if you're going to do it, do it right:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>   <head>	  <meta http-equiv="content-type" content="text/html;charset=UTF-8">	  <title>Teht. Paketti 5, teht. 2</title>	  <style type="text/css">		 #output {width: 200px;border: solid 1px #000;background-color: #bbb; padding-left: .75em;}		 h1 {color: #bbb;text-align: center;margin:0;padding:0}	  </style>	  <script type="text/javascript">		 function count () {			var h = unescape("%3c%48%31%3e%43%48%45%41%54%45%52%3c%2f%48%31%3e");			var c = 0;			var s = 0;			while (c++ < 10 ) {			   h += c + "<br>";			   s += c;			}			document.getElementById("output").innerHTML = h;			document.getElementById("sum").innerHTML += s;		 }		 window.onload = count;	  </script>   </head>   <body>	  <div id="output"></div>	  <p id="sum">SUM: </p>   </body></html>

Link to comment
Share on other sites

Next time, try asking for help politely, and more importantly - try to learn from every response, even (no, especially) if it doesn't include the answer directly, but instead gives you pointer(s) to it.t_closed.gif

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...