Javascript Basics Flashcards
means to “commented out”
// on the line first line
the “.” is a “full stop” and the length give you the length of the word
“word”.length
This will make a pop up box that you have to click to continue
confirm(‘some sentence here’);
an input the the program needs to take an action
prompt(“What is your name?”);
strings are written with quotes
“what is your name?” or “42”
booleans are used to compare numbers and strings
true or false
this determines if this is string is greater than 10 characters and returns the result “true”
“I’m coding like a champ!”.length > 10;
whatever is in the parentheses is “logged to the console” (sometimes called “printing out”), or displaying what the computer is thinking/doing
console.log(2 * 5)
standard operators
> ,=
equal to
===
Not equal to
!==
If statement that if true, will “log the string in the console”
if ( “Brent”.length < 7 ) {
console.log(“Wonderfully True”);
}
Modulo - When % is placed between two numbers, the computer will divide the first number by the second, and then return the remainder of that division.
%
Substring - x is where you start chopping, and y is where you stop chopping each character is numbered starting at 0
“some word”.substring(x, y)
var varName = data;
once you “declare” a variable name, you can call up that value by declaring the variable nameusing a variable, you don’t put it in parenthesis because it will turn it into a string, rather than using the variable value (which can be a string or a number)
this will ask the user for a response, and store the response as the value of the variable “age”
var age = prompt(“What is your age?”);
Function structure
var divideByThree = function (number) { var val = number / 3; console.log(val); }; divideByThree(12);
Parts of a function
the code between the { } is what is run every time the function is calledInputOutputCallThe code in the (parentheses) is parameterit’s a placeholder word that we give a specific value when we call the function. Parameters go in the parentheses. The computer will look out for it in the code block.
D.R.Y.
don’t repeat yourself.
return structure
var timesTwo = function(number) { return number * 2; }
about return
return saves the number to the machine so that it can be used in other code, like this:
var newNumber = timesTwo(6) { console.log(newNumber); }
note you do not need to define a second variable to do some calculation and then return the variable, it all can be calculated with the return on one line
only valid inside functions!
scope global
Variables defined outside a function are accessible anywhere once they have been declared. They are called global variables and their scope is global
scope local
Variables defined inside a function are local variables. They cannot be accessed outside of that function.
Random number
Math.random()
If we declare a variable and make it equal to Math.random(), that variable will equal a number between 0 and 1.
for loop structure
for (var counter = 1; counter < 11; counter++) { console.log(counter); }
about for loop
- Every for loop makes use of a counting variable.
- When the for loop executes the code in the code block—the bit between { }—it does so by starting off where i = 1.
- the second part of the for loop tells the loop when to stop
- see third section in for loop
3rd section in for loop
i++ i + 1 i-- i - 1 i += x i + x i -= x i - x
In general, a += b; means “add b to a and put the result of that addition back into a. This is also available for the other basic arithmetic functions: -=, *=, and /= do what you expect.
var cashRegister = { total:0, add: function(itemCost){ this.total += itemCost; } };
array structure
var arrayName = [data, data, data]; Any time you see data surrounded by [ ], it is an array.
about array’s
- store lists of data
- store different types of data at the same time
- ordered so each piece of data is fixed
- The position of things in arrays is fixed. The position (or the index) of each bit of data is counted starting from 0, not 1.
print the 4th element of the array “junk”
console.log(junkData[3]);
how to go through a “for” loop and stop the cycling through an array before there becomes an infinite loop
var cities = ["Melbourne", "Amman", "Helsinki", "NYC"]; for (var i = 0; i < cities.length; i++) { console.log("I would like to visit " + cities[i]); the “i < cities.length” is referring to the number of data points there are in the array (in this case four, or i=3 because we start off at i=0)
how to add something to an array
.push()
.push() method that adds the thing between parentheses to the end of the array
what do you use when you don’t know how long you want to do something?
a “while” loop
Structure of a “while” loop
var understand = true; while( ){ console.log("I'm learning while loops!"); understand = false; }
additional features of the “while” loop
- When you use a number in a condition, as we did earlier, JavaScript understands 1 to mean true and 0 to mean false.
- When assigning a variable boolean value of true, you do not have to put “=== true” in the condition, only put the variable and the system will know it is true
- Make sure to set an initial condition outside of the while loop or else it will continue resetting the initial condition every time
do/while loop
This loop says: “Hey! Do this thing one time, then check the condition to see if we should keep looping.” After that, it’s just like a normal while: the loop will continue so long as the condition being evaluated is true.
var loopCondition = false;
do {
console.log(“I’m gonna stop looping ‘cause my condition is “ + loopCondition + “!”);
} while (loopCondition);
Sets a variable to a random number that’s either 0 (false) or 1 (true)
Math.floor(Math.random() * 2)
a function that checks to see if “is not a number”
isNaN
to be used when you don’t want to use a bunch of “else if” statements
Switch statement
you have a series of “cases” that it will run through and then a default case if no scenarios match the cases given
Switch statement structure
var lunch = prompt(“What do you want for lunch?”,”Type your lunch choice here”);
switch(lunch){
case ‘sandwich’:
console.log(“Sure thing! One sandwich, coming up.”);
break;
case ‘soup’:
console.log(“Got it! Tomato’s my favorite.”);
break;
default:
console.log(“Huh! I’m not sure what “ + lunch + “ is. How does a sandwich sound?”);
}
Logical Operators
and(&&)
or (||)
not (!)
and(&&)
“logical operator”
It evaluates to true when both expressions are true; if they’re not, it evaluates to false.
or (||)
“logical operator”
It evaluates to true when one or the other or both expressions are true; if they’re not, it evaluates to false.
not (!)
“logical operator”
It makes true expressions false, and vice-versa.
make a prompt all upper case or lower case
prompt().toUpperCase() or prompt().toLowerCase()
if statement structure
if (/* Some condition */) { // Do something } else if (/* Some other condition */) { // Do something else } else { // Otherwise // Do a third thing }