Programming in JavaScript: Term 4 Assignment 3

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:

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.

  1. What will be printed by the following code fragment
    
    var x = 2 ;
    var y = 2 ;
    if ( x > y ) { 
    	print("True") ;
    }
    else {
    	print("False") ;
    }
    
    					
    True
    False
    Nothing
    Will print an error message
  2. What will be printed by the following code fragment
    
    var x = "Monday" ;
    var y = "Monday" ;
    if ( x == y ) {
    	print("True") ;
    else {
    	print("False") ;
    }
    
    					
    True
    False
    Nothing
    Will print an error message
  3. What will be printed by the following code fragment
    					
    						var x = 2 ;
    						if ( x == 2 ) {
    							print("True") ;
    						}
    					
    					
    True
    False
    Nothing
    Will print an error message
  4. What will be printed by the following code fragment
    
    var x = 3 ;
    if ( x == 2 ) {
    	print("True") ;
    }
    
    					
    True
    False
    Nothing
    Will print an error message
  5. 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) ;
    
    						
  6. 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) ;
    
    						
  7. 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) ;
    
    						
  8. Select the values of x for which the following code will produce the value "yes". There may be more than one correct choice.
    
    if ( ! (x < 5) ) {
     print("yes") ;
    }
    else {
     print("No") ;
    }
    
    						
    3
    5
    10
    50
  9. 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) ) ;
    						
    						
    3
    5
    10
    100
  10. 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.
  11. 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.