Name: Surname:
Due date: Save your answered form as a PDF file with name "Assigment3-Term4.pdf" and submit it as an attachment via Email to msantos@dragonacademy.org.
Prompting the user for a value
We can get data from the user of our program by asking her via a prompt statement. For example
var name = promtp("Enter your name")
This shows a dialog box with the message "Enter your name" and a text box where one can type in text. Pressing Enter after that, the text we entered will be assigned to the variable name.
Branching via if-then-else
A program contains branching when there are multiple possible courses of action.
Boolean conditions are used to determine which course of action should be taken. Sometimes multiple conditions are used to divide action into multiple branches.
When the branching ends, the courses of action rejoin.
Branching with if-then-else involves the following 3 types of code structures:
//A)
if ( condition 1) {
statement executed ONLY if condition 1 is true; ignored otherwise.
}
//B)
if ( condition1 ) {
statements executed ONLY if condition 1 is true
}
else {
statements executed ONLY if condition 1 is false
}
//C)
if ( condition1 ) {
statements ONLY if condition 1 is true
}
else if( condition 2) {
statements ONLY if condition 1 is false AND condition 2 is true
}
else {
statements ONLY if codition 1 is false AND condition 2 is false
}
Boolean expressions
These are expressions that are either True or False. If a variable x is a number, it can also be used as a condition. In this case, 0 is equivalent to False, and any other number is equivalent to True.
Examples:
How to check if two variables x and y are equal?: We use three equal signs (remember, one equal sign means assignment). The following code prints the text 'Both variables are equal' only if x contains the same value as y; otherwise the print statement is ignored.
if ( x == y ){
print( "Both variables are equal")
}
Does the variable name contains the value John?
if ( name == "John") {
print("Hi John!") ;
}
How to check if the variable x contains a number larger than or equal to 3?: The following code prints "Is at least 3" if x is larger or equal than 3; otherwise, it shows an alert saying "Is smaller than 3".
if ( x >= 3 ){
print("Is at least 3") ;
}
else {
alert("Is smaller than 3") ;
}
Analogously, we can check for x being strictly larger than y ( x > y) or strictly smaller (x < y ), or also smaller than or equal to y ( x <= y)
How to check if two variables store different values?. We use the exclamation sign. We can do that in two ways. The following two codes print "Time out!" if the variable time is equal to 0; otherwise it prints the content of time saying "The time left is ... seconds", where the dots stand for the content of the variable time.
if ( time !== 0){
print("The time left is " + time + " seconds.") ;
}
else {
print("Time out!") ;
}
We could also write this as follows
if ( ! (time == 0) ){
print("The time left is " + time + " seconds.") ;
}
else {
print("Time out!") ;
}
Here the exclamation sign ! acts as negation: if time is not equal to 0 then...
You can find a detailed summary of boolean condition here
The following is a video on multiple branching scenarios.
Exercises
Write your answer in the corresponding textboxes or choose the rigth radio buttons/checkboxes.
Consider the function print a function defined somewhere that prints its arguments on the page or opens a dialog window and prints them there.
What will be printed by the following code fragment
var x = 2 ;
var y = 2 ;
if ( x > y ) {
print("True") ;
}
else {
print("False") ;
}
What will be printed by the following code fragment
var x = "Monday" ;
var y = "Monday" ;
if ( x == y ) {
print("True") ;
else {
print("False") ;
}
What will be printed by the following code fragment
var x = 2 ;
if ( x == 2 ) {
print("True") ;
}
What will be printed by the following code fragment
var x = 3 ;
if ( x == 2 ) {
print("True") ;
}
Enter the value that will be printed by the following code fragment.
x = 3 ;
y = 10 ;
if (x > 0){
y = 12;
}
y = y + 5 ;
print(y) ;
Enter the value that will be printed by the following code fragment.
x = -3 ;
y = 10 ;
if (x > 0)}
y = 12 ;
}
y = y + 5 ;
print(y) ;
Enter the value that will be printed by the following code fragment.
x = -3 ;
y = 10 ;
if ( x > 0 ) {
y = 12 ;
y = y + 5 ;
}
print(y) ;
Select the values of x for which the following code will produce the value "yes". There may be more than one correct choice.
Select the values of y for which the following code will produce the value "No". There may be more than one correct choice.
function choose(x){
if ( x < 5) || x > 90) {
return "yes";
}
else {
return "No" ;
}
}
print( choose(y) ) ;
Write a program that prompts the user to guess a number. If it is correct, show a dialog window with the message "Correct!". If the guess is not right, show a dialog box with the message "Sorry. Incorrect guess.". You have to choose the hidden number yourself.
Write a program that prompts the user to guess a number. This time we will give the user 3 posibilities to guess it right. At any time, if the guess is correct, show a dialog window saying so. If a guess is larger than the hidden number the program must show a dialog box saying "It's smaller!"; if a guess is smaller than the right number, the program must print "It's larger!". If after the third try the guess is not right, show a dialog box with the message "Game over.". You have to choose the hidden number yourself.