Getting Started With JS Part TWO
Getting Started With JS Part TWO
Part TWO
HTML
CSS
Javascript
Front-end
By the end of this section, you should:
● Feel comfortable working with more Javascript array functions
● Feel comfortable working with Javascript objects
● Feel comfortable with Javascript variable and function scope
● Feel comfortable working with Javascript Timers
● Feel comfortable working with Javascript Date Object
More array methods (20 min)
1. find(): Returns the first element in the array that satisfies the provided testing
function. const array = [5, 12, 8, 130, 44]; const found = array.find(element
=> element > 10); console.log(found); // Output: 12
Without find(): You would need to loop through the array manually to find the
first element that meets the condition.
2. findIndex(): Returns the index of the first element in the array that satisfies the
provided testing function. Returns -1 if no elements satisfy the testing function.
const array = [5, 12, 8, 130, 44];
const index = array.findIndex(element => element > 10);
console.log(index); // Output: 1
● some() checks conditions
● Tests whether at least one element in the array passes the test implemented by
the provided function. It returns a Boolean value.
● const array = [1, 2, 3, 4];
● const hasEvenNumber = array.some(element => element % 2 === 0);
● console.log(hasEvenNumber); // true
● forEach() forEach(): Executes a provided function once for each array element.
It does not return anything. Iterates elements
● const array = [1, 2, 3, 4];
● array.forEach(element => console.log(element));
● // Output:// 1 // 2 // 3 // 4
● map(): Creates a new array populated with the results of calling a provided
function on every element in the calling array.
const array = [1, 2, 3, 4];
const doubledArray = array.map(element => element * 2);
console.log(doubledArray); // [2, 4, 6, 8]
sort(): Sorts the elements of an array in place and returns the sorted array. The
default sort order is ascending, built upon converting the elements into strings.
const array = [4, 2, 3, 1];
array.sort();
console.log(array); // [1, 2, 3, 4]
2. Create a cube object with a area method and an edge property with a value
of 3
a. Make the area method returns 3*3
b. Call the area method and use its return value in a console logs to make sure that i returned
the expected value
c. Add a volume method to the cube object that returns 3*3*3
d. Call the volume method and use its return value in a console logs to make sure that i
returned the expected value
Can you spot the inefficiency?
const dog = {
noise: “Woof!!!”, ● this keyword inside of function refers to the
makeNoise: function(){
console.log(“Woof!!!”)
caller of the function
} ○ In dog.makeNoise(), the dog object is the caller of
} the makeNoise method
○ capital
○ snows (bool)
○ population
○ getCountryInfo (method that console logs all the properties of this country)
console.log(this.noise); console.log(this.noise);
} }
} }
dog.makeNoise(); // “woof”
const cat = { var dog = new Animal(“woof”);
noise: “meow!”, dog.makeNoise(); // “woof”
makeNoise: function(){
var cat = new Animal(“meow”);
console.log(this.noise); cat.makeNoise(); // “meow”
}
}
Recap: JS objects are key/value pair data structures
const objectName = {
key1: value1,
key2: value2,
key3: value3
}
● Values can be anything: number, bool,
string, array, function, or another object
● To access the object value we use the
“dot” notation or the bracket notation:
○ .
objectName key1; // value1
○ objectName[“key1”]; // value1
Recap: “dot” or bracket notation
ACCESSING MODIFYING
let name = “Nelly”; // can be accessed and altered from other scopes
// variables declared inside a function are in the local scope of that function Func1
let age = 29; // can only be accessed and altered from this function and its
children functions (lexical scope)
name = “Denzel”; // name is in global scope, so it is accessible here
3 function greet(user){
4 alert(`Hello ${user}`);
The function to run ONE TIME
5} after the milliseconds in the
second argument have passed
You do: Practice with setTimeout (10 min)
● Open js_timers_set_timeout.html and follow instructions in the file
Review: js_timers_set_timeout.html
You do: setInterval research (15 min)
● Find a partner and figure out what setInterval is and how it is used
● Make sure to play with it in your console or visual studio code
● Be ready to share your code with the class
Review: setInterval
1 var intervalId = setInterval( genRand , 15 * 1000 ); Number of milliseconds to wait
before calling the function in first
argument
2 function genRand(){
2 function genRand(){
6 } else {
7 clearInterval( intervalId );
8 }
You do: Practice with clearInterval (5 min)
● Open js_timers_clear_interval.html and follow instructions in the file
Demo: JS Date Object
You do: Play with the Date object in your console
Demo: Moment.js
You do: Play with the Moment.js in your console
In Class reading: JS Hoisting