Jan 28, 2011

JS week 1 track 2 ~ Exercises

Now for the practice

Ex 2.1 - True, False and everything in between
Some logic need to apply here, is the nexst statement is true or false
(4 >= 6 || "grass" != "green") &&   !(12 * 2 == 144 && true)

Processing the answer by breaking the statements to two parts:

■ the first part
4>=6 ► false, 4 is not greater or equal to 6
"grass" != "green" ► true, the grass is not equale green, (only the neighbors grass is green)
so far the first part is 'false or true' ►  (false || true) ► true

■ now for the second part
12*2==144 ► false, 12*2 is equal 24 and not 144
means that second part is:
!(false && true) ► !(false) ► true , [!] indicate the opposite of false

■ all the parts together:
True&&true ► true

Answer - the statement is true !

Ex 2.2 a program that calculates and shows the value of 210 without using 2*2* ....
woof,  that took awhile

var currentNumber =1;
var count=0
    while (count<10) {
     currentNumbercurrentNumber*2
      count = count +1
        
print (currentNumber)


Ex 2.3 printing a triangle made of #
another trial and error
taking the same order from ex 2.2 and changing it

var number = "";
var count = 0;
   while (count < 10) {
      number = number + "#";
      print(number);
      count = count + 1;
}


Ex 2.4 writing the ex 2.2-2.3 using for instead of while

■ the calculation of 210 (2*2*2....)
var currentNumber =1;
for (var count=0; count<10; count = count +1)
currentNumber= currentNumber*2;
print (currentNumber);


■ printing a triangle made of "#'
var number = "";
for (var count = 0;
     count < 10;
     count = count + 1) {
  number number + "#";
  print(number);
}


Ex 2.5 a promp program asking the value of 2+2, with the next conditions:
■ alerting "Almost!" for answers of "3" and "5"
■ If the answer is "4", alert some praising
■ any other answers, say something mean

var answer = prompt("How much is of 2 + 2?", "your answer here please...");
if (answer == "3" || answer == "5")
alert("Almost!");
else if (answer == "4")
alert("well done");
else
alert("You are WAY OFF, try again!");


EX 2.6 using the previous exercise using while and optionally a break to the solution, so that it keeps repeating the question until a correct answer is given

var answer;
while (true)
{
answer = prompt("You! What is the value of 2 + 2?", "your answer here please...");
   {
   if (answer == "4")
      {
      alert("well-done");
      break;
      }
          else if (answer == "3" || answer == "5");
         {
         alert("Almost!");
         }
              else
             {
             aler ("You are WAY OFF, try again!");
             }
   }
}

 ... dog ate my homework ....  Under Construction
....
....
....

No comments:

Post a Comment