// Define a variable
var person = "John"
// Log the value of the variable person out to the console
console.log(person)
var num = 10;
if (num > 5) {
console.log("Number is greater than 5");
}
if (num > 5) {
console.log("Number is greater than 5");
} else {
console.log("Number is not greater than 5");
}
if (num > 5) {
console.log("Number is greater than 5");
} else if (num <= 0) {
console.log("Number is less than 0");
} else {
console.log("Number is between 0 and 5");
}
// Define and array with 4 people
var people = ["Bob", "Sally", "John", "Marry"]
// Length of an array
people.length // => 4
// Access the first person in the array
console.log(people[0]) // => Bob
// Access the last person in the array
console.log(people[3]) // => Marry
// Replace a value in the array
people[0] = "Jeff"
console.log(people[0]) // => Jeff
// Push onto an array
people.push("Jenny")
console.log(people) // => ["Bob", "Sally", "John", "Marry", "Jenny"]
// Pop off of an array
people.pop()
console.log(people) // => ["Bob", "Sally", "John", "Marry"]
// Print out each person in the array using for syntax
for(var i = 0; i < people.length; i++) {
console.log(people[i])
}
// Print out each person in the array using for each syntax
for (var personIndex in people) {
console.log(people[personIndex])
}
// Get the first 3 people in the array. slice takes two arguments, the start index (inclusive) and the ending index (exclusive)
var firstThree = people.slice(0, 3)
console.log(firstThree) // => ["Bob", "Sally", "John"]
var john = {
eyes: "blue",
hair: "brown",
height: 72
}
// Print out the john object's hair color
console.log(john.hair) // => brown
console.log(john["hair"]) // => brown
// Change the john object's eye color
console.log(john.eyes) // => blue
john.eyes = "green"
console.log(john.eyes) // => green
// Iterate over all of the keys in an object
// => eyes = green
// => hair = brown
// => height = 72
for (var key in john) {
console.log(key + " = " + john[key]);
}
/*
The syntax for a function name(parameterOne, parameterTwo, ...) {
- function logic goes here -
}
A function can have 0, 1, or many different parameters
*/
// Example of a function is no parameters
function sayHello() {
console.log("Hello World!")
}
sayHello() // => "Hello World!"
// An example of a function with one parameter
function sayHelloTo(name) {
console.log("Hello " + name + "!")
}
sayHelloTo("John") // => "Hello John!"
sayHelloTo("Sally") // => "Hello Sally!"
// An example of a function with more than one parameter is
function add(firstNumber, secondNumber) {
console.log(firstNumber + secondNumber)
}
add(1, 2) // => 3
// Function can also return a value
function add(firstNumber, secondNumber) {
return firstNumber + secondNumber;
}
var result = add(1,2)
console.log(result) // => 3
// Generate a random number between 0 and 1
var number = Math.random()
console.log(number) // => 0.7618375958354093
// Round a number down to the nearest whole number.
var number = Math.floor(10.5)
console.log(number) // => 10
// Round a number up to the nearest whole number. Ceil is short of ceiling or up
var number = Math.ceil(10.5)
console.log(number) // => 11
// Round a number to the closest number
var number = Math.round(10.2)
console.log(number) // => 10
var number = Math.round(10.7)
console.log(number) // => 11