Part 7- A Smarter Way to Learn JavaScript Flashcards
Can you create a new Date object and pull a part of that object in the same statement?
Yes.
var d= new Date(“January 1, 1969”).getDay();
How do you modify one of the elements of a Date object?
Use “set” instead of “get”:
setHours() instead of getHours()
What is a function?
a block of code that executes whenever you invoke its name
How do you call a function?
nameOfFunction();
With functions, what’s the difference between a parameter and an argument?
Parameters are the names listed in the function declaration.
Arguments are the real values passed to (and received by) the function by a function call.
from W3 Schools:
Function parameters are listed inside the parentheses () in the function definition.
Function arguments are the values received by the function when it is invoked.
How do you code a function definition?
- keyword FUNCTION
- name of the function
- ()
- ;
Ex. function makeToast();
What makes a variable local?
If it is DECLARED in a function. Just using it in a function does not count– that will be be global.
Where are local variables used?
Only inside the function where they are declared.
Where are local variables understood?
Only inside the function where they are declared.
Where are global variables declared?
In the main code and not in a function.
Where are global variables understood?
Everywhere.
What is the syntax for the first line of a SWITCH statement?
switch(variable being tested) {
When would you use a SWITCH statement?
It’s a more elegant way to do an IF statement, especially if you have a lot of conditions.
What is the syntax for the second line of a SWITCH statement?
case (argument being tested) :
What does DEFAULT do in a SWITCH statement?
*The code that follows it executes if none of the conditions above it are met
*It works like an ELSE statement works in an IF…ELSE series
*same syntax as a CASE statement
*used at the end of the code block
In a SWITCH statement, is the DEFAULT line optional?
*Yes, just as else code is optional after an if
statement.
*Without the default code, if none of the cases test true, nothing happens.
*However, careful coders include a break statement after the last
condition anyway as insurance, even though it may be superfluous. If you decide to add a new
condition to the end later, you won’t have to remember to add a break to the block above it in
order to avoid a disastrous cascade of statements.
When should you use a FOR loop as opposed to a WHILE loop?
*FOR loop: you know how many times the loop should run.
*WHILE loop: you want the loop to break based on a condition other than the number of times it runs
How do you code a WHILE loop?
How can you crash the browser using a WHILE loop?
If you don’t increase the variable used in the condition, the loop will never end. This will crash your browser.
What the difference in how these execute? (Code is incomplete– I’m only showing the parts of a loop that change):
var i = 0;
(i < 5)
i++
var i = 5;
(i > 0);
i–
There is no difference in the effect, but one subtracts on each loop and the other adds on each loop.