Tuesday, 1 February 2011

Week 1 Exercise 3.2 Function greaterThan that returns a function

Write a function greaterThan, which takes one argument, a number, and returns a function that represents a test. When this returned function is called with a single number as argument, it returns a boolean: true if the given number is greater than the number that was used to create the test function, and false otherwise.

On my first attempt I was not what the question was trying to get me to do.

function greaterThan (a){
  function test (y){
    if (a < y) {
      return true;
    }else {
      return false;}
  }
    show (test (50));
  }
  show (greaterThan(3));
 
After reading the solution, it was a bit more clear:

function greaterThan (a){
  return function test (y){
    return y > a;
};   
  }

var greaterThanTen = greaterThan(10);
show (greaterThanTen (9));

The last few parts of chapter two of the book that concerns anonymous function and recursion was more complicated. It requires multiple read through.

Week 1 Exercise 3.1 Function to return the absolute value of a number

Ex 3.1

My attempt to create a function that returns an absolute value of a number. I created an if else statement. If the argument is greater than 0, you return the value. Otherwise, multiply the number with -1 and return that value.

function absolute (a) {
  if (a > 0) {
    return a;
  }
  else {
    a = -1*a;
    return a;
  }
}

Monday, 31 January 2011

Week 1 Exercise 2.6 While and Optional Break

This was my answer. I did not take into account the else if statements.

var answer = prompt ("What is the value of 2+2?");
while (answer != "4"){
  var answer = prompt ("No, try again. What is the value of 2 + 2?");
  if (answer == "4")
    break;
      }

The solution provided by the book was more elegant. It used the while statement that if true, will continue to run until the break.

var answer;
while (true) {
  answer = prompt("You! What is the value of 2 + 2?", "");
  if (answer == "4") {
    alert("You must be a genius or something.");
    break;
  }
  else if (answer == "3" || answer == "5") {
    alert("Almost!");
  }
  else {
    alert("You're an embarrassment.");
  }
}

Week 1 Exercise 2.5 If Else Statement

My first attempt was wrong because when I use Logical Or, I for got to put the whole expression

var answer = prompt("What is the value of 2+2?");
if (answer == "4")
  alert ("well done, you are great with maths!");
else if (answer == "3" || "5")
  alert ("Almost!");
else
  alert ("You are too stupid to live");


The correct answer is this:

var answer = prompt("What is the value of 2+2?");
if (answer == "4")
  alert ("well done, you are great with maths!");
else if (answer == "3" || answer == "5")
  alert ("Almost!");
else
  alert ("You are too stupid to live");

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";
}

Week 1 Exercise 2.3 Drawing Triangles

I modified exercise 2.2 to print out 10 lines of #, adding one more # to each line.

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

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);