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
Hello guys. I need help with a HTML structure. I have a header tag which is styled as a flexbox. It has two contents: a picture to the left and navigation links to the right. Now I want to introduce a line in the middle of the picture and the navigation link and the way I am going about it is to use flexbox order. For example:
.nav{ position: relative;
order: 0;
//other styles}
.nav::after{
content: '';
display: block;
height: 1px;
flex: 1 1 auto;
width: 100%;
background: #fff;
order: -1;
}
the result should produce a line between the picture icon on the left hand side and the navigation container on the right hand side, but the line ends up in the navigation container(i.e the .nav class) instead of outside it. It's confusing how this is happening so I'd like a second pair of eyes to help solve it. Thanks.
::after
and ::before
always go inside the matching element rather than outside it. You can attach the pseudoelement to the header element instead.
https://torre.bio/api/bios/
and I usually get this error Access to XMLHttpRequest at 'https://torre.bio/api/bios/john' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
on close investigation at the network tab I found out that any time I hit the above url I am being redirected to https://bio.torre.co/api/bios
. Which seems to show that I may need a proxy server backend to handle access to this url. I have searched and nothing comes close to a solution. A stackoverflow respose "https://stackoverflow.com/questions/70114502/access-to-xmlhttprequest-at-from-origin-http-localhost4200-has-been-bloc " is a close answer but it was implemented with .Net. My app codebase is in react and I need help on setting up a proxy server to consume the API above. Thanks.
Hi guys, I'm reaching to ask for a little help. I'm working through the codecademy js course and I've just done this task.
function monitorCount(rows, columns) {
return rows * columns;
}
function costOfMonitors(rows, columns) {
return monitorCount(rows, columns) * 200;
}
const totalCost = costOfMonitors(5, 4);
console.log(totalCost);
I want to check my understanding of how js handles the data and what the call stack status is during the evaluation. I think I have a good idea, but I'd like your feedback in case I've missed something or I'm totally wrong..
return !!(confirmOne && confirmTwo);
return !!(confirmOne && confirmTwo);
return !!(confirmOne && confirmTwo);
The discussion stack overflow is why? I did think of one case that might apply.
if you wanted to place a boolean into a value in an object from the existence of a value in a variable
{
value: anObject,
exists: !!anObject
}
I believe this will ensure that exists becomes a boolean value
Hi everyone, I have been doing a really cool John Smilga project (the Reviews project https://www.youtube.com/watch?v=3PHXvlpOkf4&t=2736s&ab_channel=freeCodeCamp.org) and would like to check my understanding..
Starter files
const reviews = [ {
id: 1,
name: "susan smith",
job: "web developer",
img:
"https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg",
text:
"I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry",
},
{object 2} etc];
const img = document.getElementById('person-img');
let currentItem = 0;
We have a DOM with an image in, which of course has a src attribute..
When the html loads we are running this so far...
window.addEventListener('DOMContentLoaded', function(){
const item = reviews[currentItem];
img.src = item.img;
})
I can see that img in this case refers to our getElementById variable, but I wanted to check about the .src part.
src is an attribute, but it seems to be being used as a property. The dot notation is selecting it? Is this correct?
item.img is pointing to the array index 0, but when it says .img it's reaching into the object property for img. Am I correct?
So when it says img.src = item.img it means that the source of the img variable should equal the value of the img property within the array?
Sorry for the waffling, but this is baffling me!
item.img is pointing to the array index 0, but when it says .img it's reaching into the object property for img. Am I correct?
correct. item is an object, and img
is one of it's properties, and the value of img
can be got with item.img
debugger;
to insert a breakpoint, then run the code with node inspect
or use the inspect
subcommand or --inspect
option of whatever software sits between you and node
, such as mocha
, jest
, etcetera.
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.