I wrote this js code, and if I want to write is like a loop, which loop is best? while-loop,for loop, do-while loop.
var loan = 1000000;
var amoyear = 6000;
var answer = loan / amoyear;
document.write("A loan of 1000000 SEK is paid after "
+ Math.ceil(answer) + " years if installment is 500 kr / month. ");
It is very easy, but I can ́t get my mind trough it.
-
1What result do you want, that you need a loop?AndreKR– AndreKR2010年12月29日 14:40:17 +00:00Commented Dec 29, 2010 at 14:40
-
1Im going to give you the most generic answer possible: It depends :) But seriously, what do you want to change in each iteration of the loop? I can't make it out from just looking at the code. Seems to be nothing to loop over.Jakob– Jakob2010年12月29日 14:41:22 +00:00Commented Dec 29, 2010 at 14:41
-
Bonus points for using SEK by the way ;)Jakob– Jakob2010年12月29日 14:41:44 +00:00Commented Dec 29, 2010 at 14:41
-
AndreKR: Use it to calculate how many years it takes to repay a loan of 1 000 000 if you pay back 500 per months. The variable "amoyear" is for a hole year 500 * 12 = 6000.SHUMA– SHUMA2010年12月29日 14:47:27 +00:00Commented Dec 29, 2010 at 14:47
-
Jakob: Jag tänkte också att det inte behöver någon loop, förstår inte heller varför man skall använda en iteration. Men vi har det som hemuppgift i högskolan, det är en nybörjar kurs på javascript.SHUMA– SHUMA2010年12月29日 14:48:51 +00:00Commented Dec 29, 2010 at 14:48
5 Answers 5
(Approaching your question as a general question about loop constructs and how they are 'typically' used)
It depends on what your exit condition is...
- Use a
forloop if you want to iterate a fixed number of times and need to use an incrementing (or decrementing) value in your loop - Use a
whileloop if your exit condition can be expressed as a boolean statement, and the minumum number of iterations of your loop is zero (because the boolean statement is located before the loop code) - Use a
do...whileloop if your exit condition can be expressed as a boolean statement, and the minumum number of iterations of your loop is one (because the boolean statement is located after the loop code)
Comments
I think you want to perform the division part with loop though what you have done is more efficient.
I hope this is not a homework :)
Here is the answer:
var loan = 1000000;
var amoyear = 6000;
var answer;
for(answer=1; amoyear*answer<=loan; answer++);
The above code uses simple for loop and I think it would be the best for this situation.
1 Comment
A rough, generic answer to your question (not considering your particular use-case):
If you want to loop over an interval, increasing/decreasing a value by a fixed amount in each step, then use a for-loop since it is very idiomatic for that (in almost any imperative or object-oriented language).
For almost all other purposes, use the while-loop.
I'd say that do-while is not very useful and not seen often, even if you of course can find situations for that too. But some slight refactoring can often change it into a while-loop that is (usually) more readable anyway.
Comments
Just taking a superficial look at your unformatted code.. it looks like you want to do a loop to calculcate the amount of time different loans will take to pay off. When your working with numbers, it's usually easier to work with a for loop. The following examples both calculate time to pay off loans between 100,000ドル and 20,000,000ドル (at intervals of 100,000ドル)
var maxLoanAmount=20000000;
var amoyear = 6000;
for (var loan = 100000; loan<=maxLoanAmount; loan = loan + 100000){
var answer = loan / amoyear;
document.write("A loan of " + loan + " SEK is paid after " + Math.ceil(answer) + " years if installment is " + (amoyear/12) + " kr / month. ");
}
although you can easily do the exact same thing with a while loop.
var maxLoanAmount=20000000;
var amoyear = 6000;
var loan = 100000;
while (loan<=maxLoanAmount){
var answer = loan / amoyear;
document.write("A loan of " + loan + " SEK is paid after " + Math.ceil(answer) + " years if installment is " + (amoyear/12) + " kr / month. ");
loan = loan + 100000;
}
3 Comments
Use for loop when you now the number of items before looping it. For example looping via elements of an array or array-like object:
var items = [];
....
for(var i = 0; i < items.length; i++){
...
}
If you iterate via fields of some object you may use the following form of for loop:
var obj = {};
...
for(var field in object){
//access each value as object.field
}
while-loop, is used, when you don't know the term when loop is finished.
do-while loop use also when you don't know the term when loop is finished but you need to iterate at least one time Compare:
do{
//do something, then check the term
}while(check the term)
vs.
while(check the term){
//do something
}
PS: there is a form of for loop - so called foreach, but i'm not sure it is standard, so I don't use it yet.