Monday 31 January 2011

Week 1 Exercise 2.4 Rewriting using For

2.2 Rewrite for
Initially I failed because I did not understand how to put the statements regarding the currentNum variable into the for statement. When I looked at the solution, I understood that the for loop is only used for one variable, namely count. You put the rest of the statements in another block.

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


2.3 Rewrite using for

currentString = "x";
for (var count = 1; count <=10; count +=1){
  print(currentString);
  currentString = currentString + "x";
}

No comments:

Post a Comment