38 terms

Javascript

STUDY
PLAY

Terms in this set (...)

How do you find the number of characters in a string including spaces or elements in an array?
"string".length

or

var cities = ["Melbourne", "Amman", "Helsinki", "Kansas City"];

for (var i = 0; i < cities.length; i++) {
console.log(cities[i]);
}

evaluates to Melbourne Amman Helsinki Kansas City
Math operators.
+, -, *, /
How do you make a comment in the code?
//add comment here
How to make the user confirm something on the webpage?
confirm("Confirm this!");
How can you obtain user input in the form of a question?
prompt("Where are you from?");
What is the difference between numbers and strings?
number = 5 string = "5"
boolean
A boolean is always either true or false. It is a way of comparing things. For example, 5 > 2 is True
How do you print out code?
console.log(2*5)
Greater than
>
Less than
<
Less than or equal to
<=
Greater than or equal to
>=
Equal to
===
Not equal to
!==
if statements
if(condition){
console.log("");
}
if/else statements
if (condition){
console.log("");
}
else
{
console.log("");
}
Order of operations for math problems is controlled by:
( )
Ex: 5 * (3+1) comes to equal 20
How does the modulo symbol work?
modulo = % leaves remainder after division problem
Example:
23%10 evaluates to 3
How do you find specific characters of a string?
.substring(x, y)
Example: "Denver".substring(2, 5); evaluates to nve
How to save values within the code to be brought back later?
create a variable
Example:
var myAge = 22
console.log(myAge); evaluates to 22
How to use variables with commands.
Example:
var myState = "Missouri"

console.log(myState.length);
console.log(myState.substring(0,3);

evaluates to: 8
Mis
How to change the value of the variable.
Simply restate the variable with the new value.
Example
var myState = "Missouri"
console.log(myState);
var myState = "Illinois"
console.log(myState);

evaluates to: Missouri
Illinois
How to create a function.
Example:
var greeting = function(name){
console.log("Great to see you,"+" "+ name);
};

greeting(Dustin);

evaluates to
Great to see you, Dustin
How to use the return function.
The return keyword gives the programmer back the value that was calculated by the function.
Example:
var timesTwo = function(number){
return number * 2;
};

var newNumber = timesTwo(3);
console.log(newNumber);

evaluates to 6
How to use the return function with if/else statements.
Example:

var quarter = function(number){
return number/4;
};

if (quarter(24) % 3 === 0){
console.log("This statement is true!");
}

else{
console.log("This statement is false!");
}


evaluates to: This statement is true!
Functions with multiple variables.
Example:

perimeterBox = function(length, width){
return length + length + width + width;
};

perimeterBox(2,4);

evaluates to 12
Global vs Local variables
Global variables are defined outside of a function and are accessible anywhere.
Local variables are defined within a function and are only accessible within said function.
Important lesson with var...
var my_number = 7; //this has global scope

var timesTwo = function(number) {
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);

evaluates to
Inside the function my_number is: 14
Outside the function my_number is: 7
Important lesson with var two...
var my_number = 7; //this has global scope

var timesTwo = function(number) {
my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);

evaluates to
Inside the function my_number is: 14
Outside the function my_number is: 14

var is needed to display that the variable is local within the function otherwise it will replace the original functions value as seen above
Math.random
Makes to computer generate a random number between 0 and 1.

Example:

var computerChoice = Math.random()
console.log(computerChoice);
rock, paper, scissors game
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2){
return "The result is a tie!";
}
else if (choice1 === "paper"){
if (choice2 === "rock"){
return "paper wins";
}
else {
return "scissors wins";
}
}
};

compare(userChoice, computerChoice);
for loop
for(var i = 1; i < 11; i = i +1){
console.log(i);
}

evaluates to: 1 2 3 4 5 6 7 8 9 10
how to count up by increments of one in for loops
for(var i = 1; i < 11; i++)
how to count down by increments of one in for loops
for(var i = 11; i > 1; i--)
how to count up or down by any increment
for(var i = 1; i < 11; i += 3)

or

for(var i = 11; i > 1; i -= 3)
arrays
used to store data

var arrayName = [data, data, data];

anytime you see data surrounded by [ ]; it is an array
finding specific data in an array
varJunk = [ "Dustin", "Red", 22, 3];

console.log(Junk[3]);

evaluates to 3
Using for loop with an array
var names = ["Dustin", "Nep", "Phil", "Will", "Chip"];

for(var i = 0; i < names.length; i++){
console.log("I know someone called " + names[i]);
}

evaluates to: I know someone named Dustin
I know someone named Nep
I know someone named Phil
I know someone named Will
I know someone named Chip