Semi-colons tell the compiler or interpreter* when something ends. For example:
console.log("I am a programmer");
The ; says that the statement is done.
Take a look at this conditional
const x = 5;
if (x > 10) {
console.log("x is grater than 10");
} else {
console.log("x is less than 10");
};
When we declare the variable x we add a Semi-colons saying we finished declaring the variable. Then for each of the console.log statements, we say we finished the statements. And for the last } for the conditional, we use one to say that the conditional is finished.
NOTE: some languages such as JS do not require semi-colons.
*depends on the language for js it is an interpreter.
JavaScript is not compiled, but interpreted. The semi-colon is an end of statement token that tells the parser when to stop reading the line. JS is read, interpreted and run one line at a time.
JS does require semi-colons. Other languages such as C, C++, C#, Java, PHP and possibly others also require them, strictly.
When we leave off the semi-colons, the browser may insert them as needed, however this leads to lazy programming habits that can come back to haunt us.
Today’s browsers and JS engines are much more refined than in the past, even to the point of using a Just In Time compiler (JIT), and covering our back with s/c insertion. However, we should not take this for granted since the script is running on the client and we don’t know how old their browser version is. Best to adopt the practice of using semi-colons where required.
Thank you for your effort, I still don’t see how the comparison between the two arrays are syntactically stated, I will just have to deal with it as it is until I have deeper understanding of Javascript.
I understand it now, some times I have brain freezes for not apparent reason.
I left for a couple of days looked at it again and it makes complete sense now.
Thank you for your effort anyhow.
This way of doing something to each element of an array seems complicated to me, having to grab each one by its index.
What’s a more elegant nice shorthand in JS for doing arbitrary things to each element of an array, first to last?
we get the mutual followers results if we are just pushing tina’s followers into the mutualFollowers array, this operation does not specifically chooses the mutual followers between bob and tina. Can someone explain. I am confused about how this code actually chose the mutual names.
The mutual followers array is a collection of all the entries that appear in both of the other arrays. Which one gets pushed is of no concern since they are both the same, anyway.
I’m getting an error for line 3 (const tinasFollowers), and I can’t figure it out.
// Write your code below
const bobsFollowers = [‘John’, ‘Sally’, ‘Fred’, ‘Scooby’];
const tinasFollowers = [‘Ginger’, ‘Fred’, ‘Sally’];
const mutualFollowers = ;
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; i < tinasFollowers.length; j++) {
if (bobsFollowers[i] === tinasFollowers[j]) {
mutualFollowers.push(tinasFollowers[j]);
}
}
}
console.log(mutualFollowers);
Here is the error:
/home/ccuser/.bin/node: line 3: 248 Killed /usr/bin/node --max-old-space-size=200 $@
It always says line 3, but the # killed changes each time.