Programming in JavaScript: Term Assignment

Last update:

Name: Surname: Due date: . Save this page as a PDF file with name "Assignment-Term.pdf" and submit it as an attachment via Email to msantos@dragonacademy.org.


Review Exercises Sheet

Read the instructions and follow the examples when given.

If you fill-in this form, write your answer in the corresponding text boxes or choose the right radio buttons/checkboxes.

Exercises

  1. Assignments:
    When writing a program we can store a value in different places in memory so that we can use them later when needed. The precise location in memory is chosen automatically by the computer. We need however to choose a label that points to that location.
    Say we need to store an input value from the user, e.g., 31415. The first thing we do is choosing a label, say x. Then we tell the computer that we will store a value on that memory position pointed to by that label. This is done using the keyword var and the assignment symbol = in the following way.
    							
    							var x = 31415 ;
    							
    						
    Note the trailing semi-colon ;.

    We can also assign a literal string to a variable. This is simply a string of characters, including whitespaces and what not, that we tell the computer to keep it exactly as we write it. In order to do so, we use double quotation marks ". For instance, say we want to assign a variable called school the value given by the string Dragon. We would do that in the following way:

    								
    				        var school = "Dragon" ;
    								
    							
    We can also use single quotation marks ' .
    								
    				        var school = 'Dragon' ;
    								
    							
    but we cannot mix single and double quotation marks in the same string
    								
                            var school = "Dragon' ; //THIS is WRONG
    								
    							

    Vocabulary:
    • We call those labels x / school variables. This so because we can always reassign 'x' a new value, say 23 (actually we would be reassigning a value to the memory location pointed to by 'x', but it's common to treat the label as if it were the memory location). So the precise value stored in the memory position denoted by 'x' could be 31415 at the beginning of our code, but somewhere towards the end the value stored could change to 23, without necessarily changing the label.
    • A string is a piece of text that we want the computer to process exactly as we have type it. This means we don't want the computer to worry about the characters we type in. We will see more about what that entails in the examples below.

    1. Write the code that assigns the value 31415 to a variable x:
    2. Write the code that assigns the value 10 to the variable z:
    3. Write the code that assigns the value -10 (negative ten) to the variable negZ:
    4. Write the code that assigns the value 2 to the power of -23 to the variable inpZ:
    5. Write the code that assigns one million times the value of variable invZ, i.e, the product of inpZ times 1000000, to the variable nearOne:
    6. Write the code that assigns the value "exercise" to the variable work:
    7. Write the code that assigns the value 'this is also a string!' to the variable mindTheSpaces:
    8. Write the code that assigns the value 'Dragon' to the variable school:
    9. We can also append text to a given string by using the + sign, kind of like summing them. For example, the following code
      								
      				var school = "Dragon" + "Academy" ;
      								
      								
      is the same as if we would do
      								
      				var school = "DragonAcademy" ;
      								
      								
      Notice there is no space between "Dragon" and "Academy" as we forgot to include a space after the 'n' (or one before the 'A').

      We can also use the + sign with variables that contain strings:
      								
      				var type = "High School" ;
      				var school = "Dragon Academy" ; 
      				var fullName = type + school ;
      								
      								
      In this example, the variable fullName contains the string literal "High SchoolDragon Academy". Notice there is something weird there: we missed a space between 'School' and 'Dragon'! Here is how we can do it so that the space gets included:
      								
      				var type = "High School" ;
      				var school = "Dragon Academy" ; 
      				var fullName = type + " " + school ;
      								
      								
      The change is in the last statement: we add 3 strings together at once: the string contained in the variable type, the white-space literal given by " " and the string contained in the variable school.

      Write the code that assigns the value 'Keanu' to the variable givenName:
    10. Write the code that assigns the value 'Reeves' to the variable familyName:
    11. Write the code that assigns the value 'Jonny Mnemonic' to the variable alias:
    12. Now use these last two variables to write the code that assigns the value 'Keanu Reeves aka. Jonny Mnemonic' to the variable fullName:
  2. What will be produced by the following code fragment
    
    						function choose(x,y){
    							if ( x > y ) return True;
    							return False
    						}
    						alert choose(2,2) ) ;
    					
    					
    True
    False
    None
    Will print an error message
  3. What will be produced by the following code fragment
    
    						function choose(x,y){
    							if ( x === y ) return True;
    							return False
    						}
    						alert choose(2,2) ) ;
    					
    					
    True
    False
    None
    Will print an error message
  4. What will be produced by the following code fragment
    
    						function choose(x,y){
    							if ( x === y ) return True;
    							return False
    						}
    						alert choose(1,2) ) ;
    					
    					
    True
    False
    None
    Will print an error message
  5. What will be produced by the following code fragment
    
    						function choose(x){
    							if ( x === 2 ) return True;
    						}
    						alert choose(2) ) ;
    					
    					
    True
    False
    None
    Will print an error message
  6. What will be produced by the following code fragment
    
    						function choose(x,y){
    							if ( x === 2 ) return True;
    						}
    						alert choose(3) ) ;
    					
    					
    True
    False
    None
    Will print an error message
  7. Enter the value that will be produced by the following code fragment.
    							
    								x = 3 ; 
    								y = 10 ;
    								if (x > 0){
    								   y = 12;
    								}
    								y = y + 5 ;
    								alert(y) ;
    							
    						
  8. Enter the value that will be produced by the following code fragment.
    							
    								x = -3 ;
    								y = 10 ;
    								if (x > 0)}
    								   y = 12 ;
    								}
    								y = y + 5 ;
    								alert(y) ;
    							
    						
  9. Enter the value that will be produced by the following code fragment.
    							
    								x = -3 ;
    								y = 10 ; 
    								if ( x > 0 ) {
    								   y = 12 ;
    								   y = y + 5 ;
    								}
    								alert(y) ;
    							
    						
  10. Select the values of y for which the following code will produce the value "yes". There may be more than one correct choice.
    
    						function choose(x){
    							if ( ! (x < 5) ) {
    							 return "yes";
    							}
    							else {
    							 return "No" ;
    							}
    						}
    						alert choose(y) ) ;
    						
    						
    3
    5
    10
    50
  11. 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" ;
    							}
    						}
    						alert choose(y) ) ;
    						
    						
    3
    5
    10
    100
  12. Write a function is_time that consumes two integers, one representing hours in 24-hour time and the other minutes in 24-hour time.

    The function should produce "Passes" if the values are legal values for a time (that is, from 0 to 23 for hours and from 0 to 59 for minutes), and "Fails" otherwise.