A general chat about JavaScript. Ask anything you like or share your best cat picture. Please check Google/StackOverflow for answers before asking for help. And remember, be nice! Don't ask to ask, just ask
w-50
class but the issue is that it did not account for other screens. Any help will go a long way. Thanks.
console.log(sumUntil(5)); // OUTPUT: 1 + 2 + 3 + 4 + 5 = 15
I don't want to ruin your learning experience with regard to loops, but you can calculate the sum of a linear series of numbers using a formula. No need to iterate over all the numbers. :-)
function sumSeries(first, last, stride = 1) {
const length = (last - first) / stride + 1;
return (first + last) * length / 2;
}
How to use:
// sum of [1, 2, 3, 4, 5]
sumSeries(1, 5)
// sum of [5, 8, 11, 14] (step size 3)
sumSeries(5, 14, 3)
Credit due to Carl Friedrich Gauss (who was not the first to invent it).
const person = {
get firstName() {
return this.firstName;
},
set firstName(value) {
this.firstName = value;
},
get lastName() {
return this.lastName;
},
set lastName(value) {
this.lastName = value;
},
fullName() {
return `${this.firstName} ${this.lastName}`
},
}
console.log(john.fullName()); // John Doe
console.log(simon.fullName()); // Simon Collins
/*
# Object Constructor - Exercise 1
Create the `person` object and define the methods within it
getter / setter for the properties: `firstName` and` lastName`.
Also define the `fullName ()` method within the object
which takes care of concatenating and returning the name and surname
of the person. Finally, construct two objects: `john` and` simon`,
both go to the value of `firstName` and` lastName`
and print the full name in the console
*/
@justoverclockl The reason your code doesn't work is that you are writing functions that keep infinitely calling themselves. To see why this is the case, let's take a moment to see how getters and setters work.
Normally when you write anObject.firstName
, it literally means "give me the value of the firstName
property of anObject
". Likewise, when you write anObject.firstName = 'John'
, it means "set the value of the firstName
property of anObject
to 'John'
".
Getters and setters change this. If you define a getter for firstName
, this means that the firstName
property of anObject
is now a function. When you write anObject.firstName
, this function is implicitly called and the result is returned. Likewise, if you define a setter for firstName
, writing anObject.firstName = 'John'
actually means that you are passing 'John'
as an argument to a function.
In your code, you are doing this:
const person = {
get firstName() {
return this.firstName;
},
// ...
};
The middle line return this.firstName
is implicitly calling the getter function itself. Which you just defined to run that middle line. So this will repeat in itself and never end. The other accessors have the same problem.
The way to fix this is to have a separate property that will hold the actual data. A common practice is to use the same name but with an underscore before it. In that case, your person
object will look like this:
const person = {
get firstName() {
return this._firstName;
},
set firstName(value) {
this._firstName = value;
},
get lastName() {
return this._lastName;
},
set lastName(value) {
this._lastName = value;
},
fullName() {
return `${this.firstName} ${this.lastName}`
},
};
Now you can create objects that inherit these accessors and the fullName
method. You can supply the properties that hold the actual data while doing this. I'll show you the example for john
:
const john = Object.create(person, {
_firstName: 'John',
_lastName: 'Doe',
});
For completeness, I'll mention that the use of accessors in this example is absolutely pointless. You can get the same result with less code and better performance if you just use regular properties:
const person = {
fullName() {
return `${this.firstName} ${this.lastName}`;
},
};
const john = Object.create(person, {
firstName: 'John',
lastName: 'Doe',
});
Of course the exercise is still useful to get a grasp of how accessors work, but just remember that using them is not a goal in itself. You only need to spend code and effort on accessors if you need to perform extra work when a property is read or written, for example to ensure that multiple properties stay consistent with each other or to trigger events.
Oops, forgot that Object.create
doesn't work exactly like _.create
. You actually need to write this:
const john = Object.create(person, {
firstName: {value: 'John', writable: true},
lastName: {value: 'Doe', writable: true},
});
or this (using Underscore):
const john = _.create(person, {
firstName: 'John',
lastName: 'Doe',
});
const person = {
get fullName() {
return `${this.firstName} ${this.lastName}`;
},
set fullName(full) {
const [first, last] = full.split(' ');
this.firstName = first;
this.lastName = last;
},
};
const john = _.create(person, {
firstName: 'John',
lastName: 'Doe',
});
console.log(john.fullName); // 'John Doe'
john.fullName = 'Joe Dalton';
console.log(john.firstName); // 'Joe'
console.log(john.lastName); // 'Dalton'
const notes = [
{
id: 1,
description: 'Workout program',
todos: [
{
id: 1,
name: 'Push ups - 10 x 3',
done: false
},
{
id: 2,
name: 'Abdominals - 20 x 3',
done: true
},
{
id: 3,
name: 'Tapis Roulant - 15min',
done: true
}
]
},
{
id: 2,
description: 'Front-end Roadmap',
todos: [
{
id: 1,
name: 'Learn HTML',
done: true
},
{
id: 2,
name: 'Learn CSS',
done: true
},
{
id: 3,
name: 'Learn JavaScript',
done: true
},
{
id: 4,
name: 'Learn Angular',
done: true
}
]
}
]
const filt = notes.filter((el) =>{
console.log(el.todos.done) // undefined
})
/* expected output
{
id: 1,
description: 'Workout program',
todos: [
{
id: 1,
name: 'Push ups - 10 x 3',
done: false //this contain a false flag...so this object need to be returned
},
{
id: 2,
name: 'Abdominals - 20 x 3',
done: true
},
{
id: 3,
name: 'Tapis Roulant - 15min',
done: true
}
]
} */
el.todos.done
todos is an array and doesn't have a done property. You need to use another function on the todos array.
You can use the .any function to return true if any value in the array passes the function.array.any(fn)
where fn
is passed the elements and if it returns true for any element .any return true.
notes.filter(note => notes.todos.any(todo => todo.done))
const notes = [
{
id: 1,
description: 'Workout program',
todos: [
{
id: 1,
name: 'Push ups - 10 x 3',
done: false
},
{
id: 2,
name: 'Abdominals - 20 x 3',
done: true
},
{
id: 3,
name: 'Tapis Roulant - 15min',
done: true
}
]
},
{
id: 2,
description: 'Front-end Roadmap',
todos: [
{
id: 1,
name: 'Learn HTML',
done: true
},
{
id: 2,
name: 'Learn CSS',
done: true
},
{
id: 3,
name: 'Learn JavaScript',
done: true
},
{
id: 4,
name: 'Learn Angular',
done: true
}
]
}
]
const hasFalseDone = notes.filter(note => note.todos.some(todo => todo.done === false))
/**
[[object Object] {
description: "Workout program",
id: 1,
todos: [[object Object] {
done: false,
id: 1,
name: "Push ups - 10 x 3"
}, [object Object] {
done: true,
id: 2,
name: "Abdominals - 20 x 3"
}, [object Object] {
done: true,
id: 3,
name: "Tapis Roulant - 15min"
}]
}]
**/
{
id: 1,
description: 'Workout program',
todos: [
{
id: 1,
name: 'Push ups - 10 x 3',
done: false //this contain a false flag...so this object need to be returned
},
{
id: 2,
name: 'Abdominals - 20 x 3',
done: true
},
{
id: 3,
name: 'Tapis Roulant - 15min',
done: true
}
]
}
can someone explain this in detail? so...what is the first parameter of notes.filter?
A function that returns a boolean. filter
returns all elements of the array (or more generally, of the collection if you use _.filter
) for which that function returns true
.
and why the arrow?
That's basically a shorthand for an anonymous function. (a, b) => c
means roughly (not exactly) the same as function(a, b) { return c; }
.
class BankAccount {
#amount = 0;
constructor(initialAmount) {
this.#amount = initialAmount;
}
deposit(amount) {
this.#amount += amount;
}
withdraw(amount) {
this.#amount -= amount;
}
view() {
console.log(this.#amount);
}
}
class BankAccountVip extends BankAccount { // i've started to create this
constructor(initialAmount, interest) {
super(initialAmount);
}
}
const bankAccountVip = new BankAccountVip(1000);
bankAccountVip.deposit(500);
bankAccountVip.deposit(1200);
bankAccountVip.withdraw(800);
bankAccountVip.deposit(3500);
bankAccountVip.view(); // this need to be returned with 3% of interests if the balance is >= 1000
class BankAccountVip extends BankAccount {
constructor(initialAmount, interest) {
super(initialAmount);
this.interest = interest
}
deposit(amount) {
super.deposit(amount);
if (amount >= 1000) {
console.log(amount)
return amount + (amount *= 3 / 100)
}
}
}
super.deposit(amount);
with the updated amount if >= 1000