Preview

03 - JavaScript

 1. JavaScript is a programming language for making web pages ___________. It has all the features you would expect of a language such as variables, operators, arrays
Note: the trinket shows how CSS, HTML and JavaScript all fit together. Feel free to have a play!

  dynamic

  client-facing

  server-side

  static

 2. JavaScript was invented by Brendan Eich in 1995, and according to Wikipedia, in just 10 days.

  TRUE

  FALSE

 3. JavaScript is an interpreted programming language. Browsers need to have a Javascript interpreter installed in order to run it.

  FALSE

  TRUE

 4. It is not possible to write JavaScript directly into an HTML page. Instead you can have a separate .js text file, that you include on a web page using the "jscript" tag

  TRUE

  FALSE

 5. Look at the following examples of JavaScript code. 'var' is used to:
var x, y;          // How to declare variables
x = 5; y = 6;      // How to assign values
z = x + y;         // How to compute values

  delete a record e.g. var delete record x

  create a variable e.g. var x = 3;

  create a varying length string or numeric field

  set the length of a text field

 6. The most important rule in JavaScript for writing fixed values is that both strings and integers are written inside quotations. See example
var number 1; var name; number1="1";  name="myname";

  TRUE

  FALSE

 7. Code after double slashes // or between /* and */ is treated as a ___________

  command

  error

  comment

  exception

 8. In JavaScript,___________ are case sensitive and are used to name variables (and keywords, and functions, and labels).

  sqls

  comments

  identifiers

  varchars

 9. In HTML, JavaScript code must be inserted between certain tags. Which of the following options is correct?
1=========
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>


2===========
<javascript>
document.getElementById("demo").innerHTML = "My First JavaScript";
</javascript>

3============
<js>
document.getElementById("demo").innerHTML = "My First JavaScript";
</js>

  Option 3

  Option 1

  Option 2

  None of these options are correct

 10. You can place any number of scripts in an HTML document. Scripts can be placed in the , or in the section of an HTML page, or in both.

  TRUE

  FALSE

 11. What is happening in this JavaScript example?
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>

<body>

<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>

  A JavaScript function is inserted into the html page. It creates a paragraph and renders it for viewing

  A JavaScript function has been placed in the header. It would therefore fail to work and the button would not invoke or call the function

  A JavaScript function is placed in the 'head' tag section of an HTML page. The function is invoked (called) when a button is clicked

  This code would fail to run as the JavaScript function has been placed in the wrong place

 12. Placing scripts at the bottom of the body tag element improves the display speed, because script compilation slows down the display.

  FALSE

  TRUE

 13. External scripts are NOT practical when the same code is used in many different web pages.
<!DOCTYPE html>
<html>
<body>

<h2>External JavaScript</h2>

<p id="demo">A Paragraph.</p>

<button type="button" onclick="myFunction()">Try it</button>

<p>(myFunction is stored in an external file called "myScript.js")</p>

<script src="myScript.js"></script>

</body>
</html>

  FALSE

  TRUE

 14. In this example, the JavaScript code changes the content of the element with _____________
<!DOCTYPE html>
<html>
<body>

<button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button>

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

</body>
</html>

  id="demo"

  onclick function

  the .innerHtml function

  the body tag

 15. In this example, the code changes the content of its own element (using this.innerHTML):
<!DOCTYPE html>
<html>
<body>

<button onclick="this.innerHTML=Date()">The time is?</button>

</body>
</html>

  FALSE

  TRUE

 16. In the following example, what will happen when you click the 'button' which says 'Try it'
<!DOCTYPE html>
<html>
<body>

<h2>External JavaScript</h2>

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

<button type="button" onclick="myFunction()">Try it</button>

<script src="myScript.js"></script>

</body>
</html>

  The text 'TTIO' will be deleted to leave a blank space

  It is not possible to know as on button click it will load the external JavaScript file (contents not shown)

  The text 'TTIO' will change to 'Try it'

  Nothing will happen - the button does not call the function correctly

 17. In this code, on clicking the button, ___________________________________
<!DOCTYPE html>
<html>
<body>

<h2>What Can JavaScript Do?</h2>

<p>JavaScript can do a lot of cool things like..............</p>

<p id="demo" style="display:none">Hello JavaScript!</p>

<button type="button" onclick="document.getElementById('demo').style.display='block'">Click Me!</button>

</body>
</html> 

  There is an error as the JavaScript code has not been set up correctly

  The button's text changes to "Hello JavaScript!"

  hidden elements are displayed, in this case the text "Hello JavaScript!" appears

  the button disappears and is replaced with some text, in this case "Hello JavaScript!"

 18. In the following example, clicking on the button "Try it", will __________________-
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array </h2>

<p>Click the button</p>

<button onclick="myFunction()">Try it</button>

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

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;    

function myFunction() {
    points.sort(function(a, b){return a - b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

  display the array inside the button

  convert the array to strings and display them

  Delete the array so it is not visible

  Sort the array in ascending order

 19. JavaScript can "display" data in different ways: Read the following excerpt and fill in the blanks.
JavaScript can "display" data in different ways: for example: 
Writing into an HTML element, using innerHTML.
Writing into the HTML output using ___________________
Writing into an alert box, using window.alert().
Writing into the browser console, using console.log().

  write.js()

  document.write().

  script.write()

  javascript.write()

 20. The _____() method displays a dialog box that prompts the visitor for input. A _____box is often used if you want the user to input a value before entering a page

  prompt

  cart

  alert

  vart