Preview

05 - JavaScript Input data types and Output

 1. JS Output: What is being changed in this example?
<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My First Paragraph</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html>

  window.alert()

  document.write()

  console.log()

  innerHTML

 2. For testing purposes, it is convenient to use

  document.write()

  console.log()

  window.alert()

  innerHTML

 3. This example will display a pop up window with '11' in it.

  TRUE

  FALSE

 4. For debugging purposes, you can use the ____________ method to display data.

  window.alert()

  innerHTML

  document.write()

  console.log()

 5. The mini-window with the message is called a _____ window. The word “____” means that the visitor can’t interact with the rest of the page, press other buttons etc, until they have dealt with the window.
alert("Hello");

  boxed

  executable

  modal

  alerted

 6. What is the '100' in the following example?
<script>
let age = prompt('How old are you?', 100);
</script>

  a default or initial value (optional parameter)

  a user validated response

  a constant

  a variable

 7. ______ shows a message asking the user to input text. It returns the text or, if CANCEL or Esc is clicked, all browsers return null

  console

  alert

  modal

  prompt

 8. _____ shows a message and waits for the user to press “OK” or “CANCEL”. It returns true for OK and false for CANCEL/Esc
let isBoss = confirm("Are you the boss?");

alert( isBoss ); // true if OK is pressed

  modal

  confirm

  prompt

  console

 9. The document method getElementById is used to manipulate particular HTML elements on your page. Fill in the blanks for the following
As its name suggests, though, the HTML element needs
______ attached to it. 

  an ID

  a function

  a variable

  a constant

 10. The following code (try it yourself in trinket) should, on button click, move the dinosaur. What is wrong?
<html>
   
   <head>
      <title>JavaScript Animation</title>
      
      <script type="text/javascript">
         <!--
            var imgObj = null;
            
            function init(){
               imgObj = document.getElementById('dinosaur');
               imgObj.style.position= 'relative'; 
               imgObj.style.left = '0px'; 
            }
            
            function moveRight(){
               imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
            }
            
             function moveLeft(){
               imgObj.style.left = parseInt(imgObj.style.left) - 10 + 'px';
            }
            
            
            
            window.onload =init;
         //-->
      </script>
      
   </head>
   
   <body>
   
      <form>
         <img id="myImage" src="https://gallery.yopriceville.com/var/thumbs/Free-Clipart-Pictures/Cartoons-PNG/The_Good_Dinosaur_PNG_Clip_Art_Image.png?m=1448855702" />
         <br><br>
         <input type="button" value="Right" onclick="moveRight();" />
         <input type="button" value="Left" onclick="moveLeft();" />
      </form>
      
   </body>
</html>

  The two functions (moveLeft and moveRight) should be outisde the script tags

  There are no mistakes

  Line 11 - 13 do not need to be inside a function

  Line 11 and line 35 do not have the same ID. They should both be 'myImage' and it will work

 11. What does the following JavaScript code do?
<script type="text/javascript">
   <!--
      function validateEmail()
      {
         var emailID = document.myForm.EMail.value;
         atpos = emailID.indexOf("@");
         dotpos = emailID.lastIndexOf(".");
         
         if (atpos < 1 || ( dotpos - atpos < 2 )) 
         {
            alert("Please enter correct email ID")
            document.myForm.EMail.focus() ;
            return false;
         }
         return( true );
      }
   //-->
</script>

  The email address must contain at least an '@' sign and a dot.

  Validates an input email address

  The '@' sign must not be the first character of the email address

  All of these options are correct in terms of what the code does

 12. Variables may hold values that have different data types. Which of the following is NOT a JavaScript datatype?
String
Number
Modal
Boolean
Operator
DOM
Array
Object

  Boolean

  Array, Object

  Modal, Operator, DOM

  String, Modal. Object

 13. Everything in JavaScript is an object, and can be stored in a variable. This is useful to keep in mind as you learn
var myVariable = document.querySelector('h1');

  TRUE

  FALSE

 14. A structure that allows you to store multiple values in one single reference is _________________

  a DOM model

  a series

  an array

  a variable modal

 15. Real interactivity on a website needs______. These are code structures which listen for things happening in browser, running code in response
document.querySelector('html').onclick = function() {
    alert('Ouch! Stop poking me!');
}

  modals

  outputs

  events

  inputs

 16. When a JavaScript variable is declared with the keyword "new", the variable is created as an object
var x = new String();        // Declares x as a String object
var y = new Number();        // Declares y as a Number object
var z = new Boolean();       // Declares z as a Boolean object

  TRUE

  FALSE

 17. JavaScript has dynamic types. This means that the same variable can NEVER be used to hold different data types
var x;           // Now x is undefined
x = 5;           // Now x is a Number
x = "John";      // Now x is a String

  TRUE

  FALSE

 18. In this example (boolean variables) line 4 and 5 return what?
var x = 5;
var y = 5;
var z = 6;
(x == y)       // Returns _____
(x == z)       // Returns _____

  line 4 returns 'True' and line 5 returns 'False'

  Both lines return 'False'

  Both lines return 'True'

  Line 4 returns 'False' and line 5 returns 'True'

 19. What do you need to change in order for it to display 'Nissan' instead of 'Honda'?
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p>Array indexes are zero-based, which means the first item is [0].</p>

<p id="demo">DISPLAY A CAR HERE</p>

<script>
var cars = ["Honda","Nissan","Mercedes"];

document.getElementById("demo").innerHTML = cars[0];
</script>

</body>
</html>

  None of these options apply

  This would not be possible as values are hard coded

  Change cars[0] on line 14 to cars[1]

  Change the id="demo" on line 9 to id="Nissan"

 20. You can use the JavaScript typeof operator to find the type of a JavaScript variable
typeof ""                  // Returns "string"
typeof "Jonathan Peter"              // Returns "string"
typeof "Ruth Marvin"          // Returns "string"

  TRUE

  FALSE