Monday 31 January 2011

Week 1 Exercise 2.2 Two to the Power Ten

I created a variable count. As part of the function, 1 is added to the value of count after each loop. Initially I in the while statement, I used while (count <=10). That was incorrect because when count is 1, 2 to the power 1 is still 2, so you are not actually multiplying by 2. The loop will only have to run 9 times. The way to correct this is either start the count as 2 or use (count < 10) instead of (count <=10).

currentNum = 2;
var count = 1;
while (count <10) {
  currentNum= currentNum*2;
  count +=1;
}
print ( currentNum);

No comments:

Post a Comment