CONDITIONALS
IF:
function isEven(num){
if (num % 2 === 0)
console.log ("even");
}
ELSE IF:
const age = 9;
if (age < 5){
console.log("you get for free")
} else if (age< 10){
console.log("please pay $10")
} else if (age < 65) {
console.log("please pay $20")
}
ELSE:
const dayOfWeek=prompt("enter a day").toLowerCase();
if (dayOfWeek==="monday" ) {
console.log("i hate mondays")
} else if (dayOfWeek==="saturday" ){
console.log("i love saturdays")
} else {
console.log("meh")
}
Exercise Conditionals "Get Color":
function getColor(phrase){
if (phrase==="stop" ){
console.log("red")
} else if (phrase==="slow" ){
console.log("yellow") }
else if (phrase==="go" ){
console.log("green")
} else { console.log("purple")
}
}
NESTING:
const password = prompt("enter password");
if (password.length >=6) {
 if(password.indexOf(" ") === -1){ // indexOf(" ") means space on the string // -1 means "no match found"
  console.log("valid password");
 } else {
  console.log("password cannot contain spaces");
 }
} else {
 console.log("password too short, must contain 6 character")
}
&& (and)
const password = prompt("enter your password");
if (password.length >= 6 && password.indexOf(" ") === -1) { (considers both logics)
console.log("valid")
} else {
console.log("invalid")
}
|| (or)
const age = 9;
if (age >= 0 && age < 5 || age >= 65) { (considers one of both logics)
console.log("free");
} else if (age >=5 && age < 10) {
console.log("$10")
} else if (age >= 10 && age < 65) {
console.log("$20")
} else {
console.log("invalid")
}
! (not)
let firstName = prompt ("enter your first name");
if (!firstName) { //meaning entry was false or empty
firstName = prompt("try again");
}
Switch:
Will search through the cases and execute when finds the variable.
Without the break it will continue to execute the next ones until the end or a break.
const day = 1;
switch (day){
case 1:
console.log("monday");
break;
case 2:
console.log("tuesday");
break;
case 3:
console.log("wednesday");
break;
case 4:
console.log("thursday");
break;
case 5:
console.log("friday");
break;
case 6:
case 7:
console.log("weekend");
default:
console.log("i dont know")
ARRAYS :
Data structure that colects information
Methods:
- PUSH: adds an element to the queue of the array.
- POP: removes the last element of the array
- SHIFT: removes the first elemnt of the array
- UNSHIFT: adds a new element to the beggining of the array
- CONCAT: merges arrays
- INCLUDES: display boolean condition
- indexOf: display the element searched (if there is not, will be -1)
- REVERSE: backflip the queue
- SLICE: copy and paste to the new variable from the 0 to the number between ()
- SPLICE: delete elements from the array
- SORT: rearanges the array using some critearia.
let colors = ["red", "orange", "yellow"];
colors[0] = "red";
colors[1] = "orange";
colors[2] = "yellow";
colors[5];
colors[5] = "blue"
//["red","orange","yellow", empty x 2, "blue"]
let lottNums = [19,22,56,12,51];
lottNums.push(5)
[19,22,56,12,51,5]
lottNums.pop();
[19,22,56,12,51]
lottNums.shift();
[22,56,12,51]
lottNums.unshift(76);
[79,22,56,12,51]
//ej
const planets = ['The Moon','Venus', 'Earth', 'Mars', 'Jupiter'];
planets.shift();
planets.push("Saturn");
planets.unshift("Mercury");
let cats= ["blue","kitty"];
let dogs= ["rusty","wyatt"];
let comboParty = dogs.concat(cats);
comboParty
["rusty","wyatt","blue","kitty"]
cats.includes("blue");
true
cats.includes("Blue");
false
cats.indexOf("kitty")
1
comboParty.reverse();
(4) ['kitty', 'blue', 'wyatt', 'rusty']
let colors = ["red", "orange", "yellow","green","blue","indigo","violet"];
let coolColors = colors.slice(3)
coolColors
(4) ['green', 'blue', 'indigo', 'violet']
colors.slice(2,4)
(2) ['yellow', 'green']
colors.slice(-3);
(3) ['blue', 'indigo', 'violet']
colors.splice(5,1)
['indigo']
colors.splice(3,0,"ambar","magenta");
colors
(9) ['red', 'orange', 'yellow', 'ambar', 'magenta', 'green', 'blue', 'indigo', 'violet']
colors.sort();
(9) ['ambar', 'blue', 'green', 'indigo', 'magenta', 'orange', 'red', 'violet', 'yellow']
const gameBoard = [["x","o","x"],["o","null","x"],["o","o","x"]]
gameBoard
(3) [Array(3), Array(3), Array(3)]
0: (3) ['x', 'o', 'x']
1: (3) ['o', 'null', 'x']
2: (3) ['o', 'o', 'x']
length: 3
[[Prototype]]: Array(0)
gameBoard[1] //display the array #1
(3) ['o', 'null', 'x']
gameBoard[1][1] //display the element #1 on array #1
'null'
const airplaneSeats = [
['Ruth', 'Anthony', 'Stevie'],
['Amelia', 'Pedro', 'Maya'],
['Xavier', 'Ananya', 'Luis'],
['Luke', null, 'Deniz'],
['Rin', 'Sakura', 'Francisco']
];
airplaneSeats[3].splice(1,1,"Hugo");
airplaneSeats
(5) [Array(3), Array(3), Array(3), Array(3), Array(3)]
0: (3) ['Ruth', 'Anthony', 'Stevie']
1: (3) ['Amelia', 'Pedro', 'Maya']
2: (3) ['Xavier', 'Ananya', 'Luis']
3: (3) ['Luke', 'Hugo', 'Deniz']
4: (3) ['Rin', 'Sakura', 'Francisco']
length: 5
[[Prototype]]: Array(0)
OBJECTS
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities
//example:
const kitchenSink = {
favNum: 2412442,
material: ["metal", "ceramic"],
warranty: true
}
const user = {name:"agustin",surename:"bruno"}
//exercise
const product = {name:"Gummy Bears",inStock:true,price:1.99,flavors:["grape","apple","cherry"]}
//extract data from objects
const years = {1999:"good", 2020:"bad"};
years["1999"]
"good"
years[1999]
"good"
const happy= {birthday:"today",newyear:"yesterday"};
happy.birthday
'today'
//EXERCISE
const restaurant = {
name: 'Ichiran Ramen',
address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`,
city: 'Brooklyn',
state: 'NY',
zipcode: '11206',
}
const fullAddress = restaurant.address+ restaurant.city+ restaurant.state+ restaurant.zipcode
'90 Johnson AveBrooklynNY11206'
//MODIFYING OBJECTS
const midterms = {danielle:96, thomas:78}
midterms.thomas = 79
midterms.ezra
midterms.ezra = 60
midterms["antonio"]
midterms["antonio"] = 100
midterms
{danielle: 96, thomas: 79, ezra: 60, antonio: 100}
//OBJECTS & ARRAY NESTING
const shoppingCart= [
{
product: "jenga classic",
price: 6.88,
quantity: 1,
}
{
product: "Echo Dot",
price: 29.99,
quantity: 3
}
]
const student = {
firstName: "david",
strengths:["music","art"],
exams: {
midterm:92,
final:88
}
}