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!

  client-facing

  server-side

  dynamic

  static

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

  FALSE

  TRUE

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

  TRUE

  FALSE

 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

  FALSE

  TRUE

 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

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

  create a varying length string or numeric field

  delete a record e.g. var delete record x

  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";

  FALSE

  TRUE

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

  command

  error

  exception

  comment

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

  identifiers

  varchars

  comments

  sqls

 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 1

  Option 3

  None of these options are correct

  Option 2

 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>

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

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

  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

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

  TRUE

  FALSE

 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>

  onclick function

  the body tag

  the .innerHtml function

  id="demo"

 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>

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

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

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

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

 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> 

  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!"

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

  The button's text changes to "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>

  convert the array to strings and display them

  Sort the array in ascending order

  Delete the array so it is not visible

  display the array inside the button

 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().

  javascript.write()

  script.write()

  write.js()

  document.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

  alert

  vart

  cart

  prompt