Coding diary to reflect concepts that I am learning and revisiting
- Combining filter (array method) and .length to find the total number of elements that are above or below a certain value
(E.g. Given an array of different ages. Find the total number of people that are above the age of 21)
/**
* @param {number[]} ages
*/
const numOfEligibleVoters = ages => {
let total = 0;
ages.forEach(age => {
if (age >= 21) {
total += 1;
}
})
return total;
}
Using Filter + length
/**
* @param {number[]} ages
*/
const numOfEligibleVoters = ages => {
return ages.filter(ages => age >= 21).length
}
2. The find()
array method
3. Classes in Js (Syntax recap + elementary concept refresher)
Creating a new instance of a class
const person1 = new Person("Anne Smith");
// person1 is the variable for the instance of the class Person
The this
keyword
- Inside of the instance method, the "this" keyword refers to the current instance of the class
Basic Constructor syntax
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
fullName() {
return `${this.firstName} ${this.lastName}`;
}
sayHi() {
// calling the instance method
const name = this.fullName();
return `Hi ${name}`
}
}
If you want an instance method to call another instance method use the following syntax: this.nameOfFunction()
Setters and Getters
- Underscore syntax "_" --> a convention used to denote that the property is internal to the class and should not accessed from outside