Is this true?
Solution: There are two expressions connected by &&.
The first part ((4>=6)||("grass"!="green")).
4>=6 is false.But grass!=green is true. So, this expression
is true.
The second part (((12*2)==144) && true).
(12*2)==144 is true. trues itself is always truthy.So, this
expression is true. (true && true)
So, true && true gives true.
Ex 2.2: Use the techniques shown so far to write a program that calculates and shows the value of 210 (2 to the 10th power). You are, obviously, not allowed to use a cheap trick like just writing
2 * 2 * ...
.Solution: Since, upto this point, only the while loop is explained, I am going to use this:
var result=1;
var counter=1;
while(counter<=10)
{ result=result*2;
counter=counter+1;
}
print(result);
Ex 2.3: With some slight modifications, the solution to the previous exercise can be made to draw a triangle. And when I say 'draw a triangle' I mean 'print out some text that almost looks like a triangle when you squint'.
Print out ten lines. On the first line there is one '#' character. On the second there are two. And so on.
Solution: Here, again I will use the while loop:
var line=" ";
var counter=1;
while(counter<=10)
{ line=line+"#"; // concatenating the hashes
counter=counter+1;
print(line);
}
Ex 2.4: Rewrite the solutions of the previous two exercises to use
for
instead of while
.Solution: first problem-
var result=1;
var i=1;
for(i=1;i<=10;i++)
result=result*2;
print(result);
second problem:
var line=" ";
var i=1;
for(i=1;i<=10;i++)
{ line=line+"#";
print(line);
}
Ex 2.5:Write a program to ask yourself, using
prompt
, what the value of 2 + 2 is. If the answer is "4", use alert
to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean.Solution: var ans=prompt("what is 2+2?","think!!!");
if(ans=="4")
alert("Bravo!!");
else if((ans=="3")||(ans="5"))
alert("Almost!");
else
alert("Stupid!!");
Ex 2.6:Add a
while
and optionally a break
to your solution for the previous exercise, so that it keeps repeating the question until a correct answer is given.Solution:
var ans;
while(ans!=4)
{
var ans=prompt("what is 2+2?","think!!!");
if(ans=="4")
{alert("Bravo!!");
break;}
else if((ans=="3")||(ans="5"))
alert("Almost!");
else
alert("Stupid!!");
}
Note: In the above solution, the break statement is really unnecessary.
Final Question: Create an example that shows a valid use of the 'guard' and 'default' operators in Javascript
Solution:
The && operator is also called the guard or the logical and operator. The && operator returns the first operand only if it is falsy(i.e, false,null,undefined,"",0) .Otherwise, it returns the second operand .
eg: var ans=prompt("Predict a number","");
var b=10;
var value= ((ans==10) && "Correct prediction!!!");
In the above example, the user is asked to predict a number. If the answer matches 10, then the output "Correct prediction!!!" is to be stored in a variable and displayed on the screen.
The || operator is also called the 'default' or the logical or operator. The || operator returns the second operand only if the first is falsy(i.e, false,null,undefined,"",0) .
eg: var ans=prompt("Predict a number","");
var b=10;
var value=((ans!=10) || "Wrong prediction.")
The above example is just the opposite of the previous example. Here if the user doesn't enter 10, wrong prediction is stored.