JavaScript Flashcards

1
Q

How would you add a simple button to change the text in the following:

<div>CHANGE THIS</div>

A

Click

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The HTML below displays an image, update this so that when you click the image it runs a function called YouClickedMe()

<img></img>

A

<img></img>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write the myFunction() for the button below which which will change the text to green:

<p>JavaScript can change the style of an HTML element.</p>

Click Me!

A
function myFunction() {
    var x = document.getElementById("demo");
    x.style.color = "green"; 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

IN JS, how do you find an element on a page by it’s Id?

A

Use:

document.getElementById(“[elementsId]”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you get a window alert to pop up whenever someone loads a page?

A

Include something along the lines of:

window.alert(“HELLO”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Complete the following:

JavaScript has ??????? types

A

JavaScript has DYNAMIC types

ie a var x can be assigned a number and then later a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many number types does JavaScript have?

A

Just one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

set up a JavaScript array containing the following words:
Cat
Dog
Rabbit

A

var animals = [“Cat”, “Dog”, “Rabbit”];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Setup a javaScript object for the following person:
firstName = John,
lastName = Fentiman,
Age = 37

A

var john = {firstName:”John”, lastName:”Fentiman”, age:37};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you find the type of a variable?

A
var x = "John";
typeof x;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you clear out a variable?

A
var person = "John";  //This would set it up as a string
person = undefined; //This would clear it out
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Add a function to this object to return the full name:

var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    }
};

document.getElementById(“demo”).innerHTML = person.fullName();

A
var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
       return this.firstName + " " + this.lastName;
    }
};

document.getElementById(“demo”).innerHTML = person.fullName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you find the 3rd character in the following:

var str = “HELLO WORLD”;

A

str.charAt(2);

DO NOT USE str[2]
This is not compatiable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you convert the value on an innerHTML to a number?

A

var i = parseInt(document.getElementById(“IdName”).innerHTML);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a JavaScript object call Person which includes a name and an age attribute along with a sub object called favouritePerson

A
var person = {
   name: "John",
   age: 37,
   favouritePerson:  {
        name: "Gemma",
        age: 38
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does this function do and what would we call it?

(function(){
   var userName = "John";
   var currentTime = new date();
   function userDateStamp(){
      return userName + currentTime;
   }
})();
A

This is a called an ANONYMOUS SELF EXECUTING FUNCTION

ANONYMOUS: It has no name
SELF EXECUTING: Everything within between (function{ and )(); would just be run and return the userDateStamp string, with all the bit’s inside being kept ‘private’ - they would be out of scope for anything else

17
Q

How would you create the equivalent of a NAMESPACE in JS?

A

var MyNameSpace = MyNameSpace || {};

If MyNameSpace doesn’t exist, it will create an empty object, otherwise it will use the exisitng MyNameSpace object

18
Q

You have an object set up as an name space, as shown below, how do you then add functions, sub areas and variables to it?

var MyNameSpace = MyNameSpace || {};

A
//Function
MyNameSpace.currentTime = function () { return new Date() };
//Sub Area
MyNameSpace.SubArea = MyNameSpace.SubArea || {};
//Variable
MyNameSpace.myName = "John";
19
Q

What is the global name space in JS?

A

window

20
Q

Convert the following C# class into a JS class and implement it:

class Artist ()
{
  string Name;
  string Label;
  int NumberOfReleases
}
A
function Artist (name, label, numberOfReleases)
{
  this.Name = name;
  this.Label = label;
  this.NumberOfReleases = numberOfReleases;
}

…..

artist = new Artist(“Debasser”, “WIDE”, 12);

21
Q

What is DUCK TYPING?

A

If it looks like a duck, walks like a duck and sounds like a duck, then it’s probably a duck

This means that we don’t need to specify the type of something being fed into a function. So long as the object or parameters going in are sufficient for the function, then it will work just fine.

This elements the need for interfaces

22
Q

What is the JS following doing:

var svr = new SmptClient();

svr.send({
to: “john.fentiman@mandg.co.uk”,
from: “gemmanorton@hotmail.com”,
body: “Hi favourite, missing you”,
subject; “Hello”
});

A

It’s creating an instance of a SmptClient and then using that objects send function.

Within the send function we are then building an ad-hoc object which contains the required parameters for the send function to process

This is an example of Duck Typing

23
Q

What does DOM stand for?

A

Document Object Model

24
Q

How would you use JS DOM to find all the <p> elements in the main section of the code below:

</p><div>TITLE</div>
<div>
<p>First Line</p>
<p>Second Line</p>
ThirdLine
</div>
<div>END</div>
A
var x = document.getElementById("main");
var y = x.getElementsByTagName("p");
25
Q

Using a JS script, how could you add an onclick function to the following element:

<div>Click Me</div>

A

document.getElementById(“myBtn”).onclick = myFunction;

NB: Elsewhere you would need to have a function called myFunction for this to be of any use

26
Q

What does the follow jQuery do?

$(function(){

A

This is the same as writing:

$(document).ready(function(){

and it means that it won’t run anything in the following function until the page is fully loaded

27
Q

What does the following jQuery do?

$(“p”)

A

Finds all <p> elements on a page</p>

28
Q

What does the following jQuery do?

$(“#myThing”)

A

It will find the element on the page with id=”mything”

29
Q

What does the following jQuery do?

$(“.box”)

A

If will find any elements that have class=”box”

30
Q

What does the following do?

$(document).ready(function(){
$(“p”).click(function(){
$(this).hide();
});

A
  1. It waits for the page to load
  2. Identifies any <p> elements on the page
  3. Adds a click event to these so that when they’re clicked they will disappear </p>