Chapter 2 - Basic Javascript Instructions Flashcards

1
Q

A statement is…

A
An individual instruction or step in a script
e.g. var today = new Date();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is Javascript case-sensitive?

A

Yes

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

Give a detailed description of a statement…

A

A statement is an individual instruction that the computer should follow.
Each one starts on a new line and ends with a semicolon
The semicolon lets the JS interpreter know when the statement is over, and that it should move on to the next step.

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

What is a code block?

A

A statement surrounded by a curly brace.
The closing curly brace is not followed by a semicolon
Code blocks will often be used to group together many statements.
This helps programmers organise their code and make it more readable.

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

Why write comments?

A

Comments help to explain what your code does
They help make your code easier to read and understand.
This can help you and others who ead your code.

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

How would you write a single-line comment?

A

A single line comment would be anything on a line that follows two forward slash characters

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

How would you write a multi-line comment?

A

You start with a /*
You end with a */
Anything between these two is not processed by the JS interpreter

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

When might you use a multi-line comment?

A

To describe how a script works

To stop a section of your script from running when testing it.

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

What is a variable?

A

A variable is a piece of information that a script needs to store temporarily so that it can do its job.

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

How would variables factor in the calculation of the area of wall?

A

You would need to multiply the width and the height of a wall to calculate its area
Variables would be used to temporarily store values for the height and width.

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

Which type of human memory might you compare variables to?

A

Short-term memory

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

If you have a script which can calculate the area of a wall, which part of this task is best represented by variables?

A

Variables are best used to represent the values in your script that are likely to change
In this case, it would be the specific heights and widths of each wall
Even though these change, the area of a wall will always be height x width.

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

What kind of data is generated by variables, when used together in a script?

A

Calculated or computed data.

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

How is a variable declared?

A

var is a keyword that the interpreter recognises and anticipates the creation of a variable
The variable must be given an identifier e.g. quantity in var quantity.

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

Give an example of how you would declare a variable?

A

var quantity;

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

How would you write a variable name of more than one word?

A

You would use camelCase

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

What does it mean to assign a value to a variable?

A

Telling a variable what information you want it to store for you.

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

What are some good practices when naming variables?

A

A variable name should describe the kind of data that the variable holds.

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

What is the significance of the equals (=) sign?

A

This is known as the assignment operator

It can assign or update the value given to a variable

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

If a variable is declared, but has now value assigned, it is said to be…

A

Undefined.

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

What are the three data types?

A

Numeric - integers, decimals and negatives
String - enclosed within single or double quotes - can contain HTML code
Boolean - True or false

22
Q

What can numbers be used for?

A

Calculations
Determining screen size
Moving elements on the screen
Timing fading in of elements

23
Q

What are the other data types?

A

Arrays
Objects
Undefined
Null

24
Q

Do you need to specify a data type when declaring a variable in JavaScript?

A

No

25
Q

Give and example of using a variable to store a number…

A
// Create three variables to store the information needed.
var price;
var quantity;
var total;
// Assign values to the price and quantity variables.
price = 5;
quantity = 14;
// Calculate the total by multiplying the price by quantity.
total = price * quantity;
// Get the element with an id of cost.
var el = document.getElementById('cost');
el.textContent = '$' + total;
26
Q

How does variable assignment mirror algebra?

A

Once a variable has been assigned a value, you can use that variable’s name to represent that value.

27
Q

Write an example of using a variable to store a string…

A

var username;

username = ‘Molly’;

28
Q

Can you write strings on more than one line?

A

No.

29
Q

Name two ways in which you can use quotes within a string?

A

If you want to use a double quote, surround the string in single quotes or vice versa.
Use a single backslash before the relevant quotation character. This is called escaping.

30
Q

Give an example of using a variable to store a Boolean…

A

var inStock;

inStock = true;

31
Q

What are two popular instances for the use of Booleans?

A

When the answer can only be true or false.

When your code can take more than one path.

32
Q

Name four ways in which variables can be declared…

A

Variables declared and values assigned at the same time.
var price = 5;
Variables declared, then values assinged
var price, quantity, total;
price = 5; etc etc.
var price = 5, quantity = 14;
var total = price * quantity;
Variable is used to hold a reference to an to an HTML element
This allows you to work directly with the element stored in that variable.
var el = document.getElementById(‘cost’);
el.textContent = ‘$’ + total;

33
Q

How might you change the value of a variable?

A
var inStock = true;
inStock = false;
34
Q

What are the six rules for naming variables?

A

The name must begin with a letter, a dollar sign or an underscore.
Names can contain letters, numbers , dollar sign and underscore.
Names must not use keywords or reserved words
Variables are case-sensitive. Don’t create two with the same name in different cases.
Use a name that describes the type of information the variable stores.
If your variable name is more than one word, use camelCase.

35
Q

What is an array?

A

A special type of variable used when with a list or a set of values related to eachother.
When using an array you do not need to know or specify how many values it will hold.

36
Q

How would you create an array?

A

ARRAY LITERAL
var colors;
colors = [‘white, ‘black’, ‘custom’];

ARRAY CONSTRUCTOR
var colors = new Array(‘white’, ‘black’, ‘custom’);
37
Q

Which method might you use to retrieve data from an array?

A

item() - index number of the item goes in the parenthese.

38
Q

What is the standard first index value of the first item in an array?

A

zero (0)

39
Q

How would you access an item in an array?

A

var itemThree;

itemThree = colors[2];

40
Q

How would you find the length of an array?

A

var numColors;

numColors = colors.length;

41
Q

How would you change a value in an array?

A

colors[2] = ‘beige’;`

42
Q

What is an expression?

A

Something that evaluates into a single value

43
Q

What are the two main types of expression?

A
Expressions that assign a value to a variable.
var color = ‘beige’;
Expressions that use two or more values to return a single value
var area = 3 * 2;
44
Q

What types or operator are commonly used in JavaScript?

A

Assignment Operators - color = ‘beige’;
Arithmetic Operators - area = 3*2;
String Operators - greeting = ‘Hi’ + ‘Molly’;
Comparison Operators - buy = 3 > 5;
Logical Operators - buy = (5 > 3) && (2

45
Q

What are the types of arithmetic operators?

A
Addition +
Subtraction -
Division /
Multiplication *
Increment ++
Decrement --
Modulus %
46
Q

In what order are arithmetical operations executed?

A

Multiplication and Division before Subtraction and Addition

Brackets can be used to overcome this.

47
Q

Give an example of using Arithmetic Operators…

A
// Create a variable for the subtotal and make a calculation
var subtotal = (13 + 1) * 5; // Subtotal is 70
// Create a variable for the shipping and make a calculation
var shipping = 0.5 * (13 + 1); // Shipping is 7
// Create the total by combining the subtotal and shipping values
var total = subtotal + shipping; // Total is 77
// Write the results to the screen
var elSub = document.getElementById('subtotal');
elSub.textContent = subtotal;
var elShip = document.getElementById('shipping');
elShip.textContent = shipping;
var elTotal = document.getElementById('total');
elTotal.textContent = total;

// Note: textContent does not work in IE8 or earlier - see explanation on website

48
Q

Joining two or more strings together is called…

A

Concatenation

49
Q

What do you need to remember when mixing numbers and strings?

A

When you place quotes around a number it becomes a string. It will be concatenated, not added.
When you add numeric data to a string, it becomes part of the string.
If you try to use any arithmetic operators on string apart from +, the result is NaN.

50
Q

Give an example of using String Operators

A
// Store the greeting in a variable
var greeting = 'Howdy ';
// Store the users name in a variable
var name = 'Molly';
/* Create the welcome message by concatenating the strings in the two variables */
var welcomeMessage = greeting + name + '!';
// Get the element that has an id of greeting
var el = document.getElementById('greeting');
// Replace the content of this element with the personal message
el.textContent = welcomeMessage;

// Note: textContent does not work in IE8 or earlier - see explanation on website

51
Q

Give an example of Basic JavaScript Instructions.

A
// Create variables for the welcome message
var greeting = 'Howdy ';
var name = 'Molly';
var message = ', please check your order:';
// Concatenate the three variables above to create the welcome message
var welcome = greeting + name + message;
// Create variables to hold details about the sign
var sign = 'Montague House';
var tiles = sign.length;
var subTotal = tiles * 5;
var shipping = 7;
var grandTotal = subTotal + shipping;
// Get the element that has an id of greeting
var el = document.getElementById('greeting');
// Replace the content of that element with the personalized welcome message
el.textContent = welcome;
// Get the element that has an id of userSign then update its contents
var elSign = document.getElementById('userSign');
elSign.textContent = sign;
// Get the element that has an id of tiles then update its contents
var elTiles = document.getElementById('tiles');
elTiles.textContent = tiles;
// Get the element that has an id of subTotal then update its contents
var elSubTotal = document.getElementById('subTotal');
elSubTotal.textContent = '$' + subTotal;
// Get the element that has an id of shipping then update its contents
var elShipping = document.getElementById('shipping');
elShipping.textContent = '$' + shipping;
// Get the element that has an id of grandTotal then update its contents
var elGrandTotal = document.getElementById('grandTotal');
elGrandTotal.textContent = '$' + grandTotal;

// Note: textContent does not work in IE8 or earlier - see explanation on website