Midterm Study
Introduction to Web Pages
- A web page is a container for the following three technologies. Describe the purpose of each for a web page:
Answers:
- HTML is used for defining web elements and the rough layout of a website
- CSS is used to style the default HTML elements
- JavaScript is used for data input, manipulation and other logical components
Introduction to javascript
- Browser pop-up dialogue box functions
- alert()
- prompt()
- confirm()
Answers:
- Reload webpage to see examples
Data Types and Variables
- Name JavaScript's three data types. Describe the types of data they store
- JavaScript is a weakly-typed language. Describe what this means.
- What are the naming rules for a variables name? What are naming conventions?
- What is a constant in JavaScript? Describe how you would declare a JavaScript constant.
Answers:
- Number, String and Boolean
- weakly-typed language implies that data types do not need to be defined at initialization, the compiler will
decide, cast and convert the data type unless explicitly cast
- A naming convention is a recommended way of naming variables to ensure readability and organize code.
JavaScript uses camelCase for variables and classes and UPPER_SNAKE_CASE for constants
let customerAge = 23;
const ONT_TAX_RATE = 0.13;
- A constant is a variable whose value cannot be changed once initialized, making it constant. To define a
constant, use the "const" keyword.
Operators
- Explain the following binary operators: + - * / %
- Explain the multiple uses of the + operator.
- Describe how the increment/decrement operators (++ and --) work.
Answers:
- The + operator is used to get the sum, the - operator is to get the difference, the * operator multiplies,
the / operator divides and the % (modulus) operator determines if two numbers divided have a remainder and
how much of one
- The + operator can be used to add two numbers together to find their sum, or you can use it to concatenate
multiple Strings together.
- The ++ (increment) operator is used to increase the value of a variable by exactly one. The -- (decrement)
operator is used to decrease the value of a variable by exactly 1.
The JavaScript Math object
- Describe the operation and the return for the following Math methods:
- Math.PI
- Math.abs(x)
- Math.pow(x,2)
- Math.round(x)
- Math.ceil(x)
- Math.floor(x)
- Math.trunc(x)
- Math.random()
- Math.min(x,y)
- Math.max(x,y)
Answers:
- Math.PI is a constant value for PI
- Math.abs(x) returns the absolute value of X (makes a negative positive)
- Math.pow(x,2) returns the value of x to the power of y (in this case 2)
- Math.round(x) returns x rounded to the nearest whole. Below 0.5 is rounded down, above 0.5 is rouded up.
- Math.ceil(x) returns the "ceiling" of x, which is the next highest whole integer.
- Math.floor(x) returns the "floor" of x, which is the next lowest whole integer.
- Math.trunc(x) returns x truncated, which means it cuts off the decimal places regardless of what they are.
- Math.random() returns a random number between 0 and 1, can be manipulated to return in between any range
- Math.min(x,y) returns the minimum of two numbers. The smaller of the two
- Math.max(x,y) returns the maximum of two numbers. The larger of the two
JavaScript Number Methods
- Describe the operation and the return for the following Number methods:
Answers:
- toFixed() returns x but at a fixed number of decimal places, defined by y
- toPrecision() returns x but at a fixed number of significant digits. This means it may round deicimal places
before dropping them.
JavaScript Global Functions
- Describe the operation and the return for the following global functions:
- parseInt(number)
- parseFloat(number)
Answers:
- parseInt(number) takes a string as input and returns the first integer in the string. Can also accept a
radix option
- parseFloat(number) takes a string as input and returns the first float value in the string.
JavaScript String Methods
- Describe the operation and the return for the following String methods.
- length
- toLowerCase()
- toUpperCase()
- charAt()
Answers:
- length gets the character count of a string including spaces
- toLowerCase() turns all uppercase letters to lowercase letters
- toUpperCase() turns all lowercase letters to uppercase letters
- chatAt() takes a number for the index of the word to search through and returns the character at that index.
Playground
Interactive JavaScript follows