Hi @hummingcowbird,
I will give my observations and comments according to versions. Will try to make it short and understandable.
Version 1:
var myPlaces = ['here', 'new job', 'Hawaii'];
var friendPlaces = ['in', 'there', 'jamaica'];
//Computer reads the first loop, execute codes inside the loop:
for(var i = 0; i < myPlaces.length; i++){
//This is another nested loop inside a loop, computer will execute all code inside the second loop (nested loop) first
for(var j = 0; j < friendPlaces.length; j++){
if (myPlaces[i] === friendPlaces[j]){
console.log('Our favorite place in the world is: ' + ' ' + myPlaces[i] + '!!!');
}
}
//nested loop finishes, back to the first loop
}
//This will continue until all loop finishes
//Only then, computer will read the next code, no matter how, the code below will get executed and print "There are no places........."
console.log('There are no places we have in common Christi Pants!!!');
The last console.log
will be executed as code no matter what, because the computer reads JavaScript from Top to Bottom, the way you write the code in Version 1, computer will finish all the loop then only reach the console.log(('There are no places we have in common....")
part. As you just purely write console.log
, of course, the console.log
will get executed and print the sentences you want.
Version 2:
var myPlaces = ['here', 'new job', 'Hawaii'];
var friendPlaces = ['in', 'there', 'jamaica'];
//Same thing, computer will execute from top to bottom
for (var i = 0; i < myPlaces.length; i++) {
for (var j = 0; j < friendPlaces.length; j++) {
if (myPlaces[i] === friendPlaces[j]) {
console.log('Our favorite place in the world is: ' + ' ' + myPlaces[i] + '!!!');
}
}
}
//By this line, the above loop and nested loop inside are all executed. That's why you can print out something IF there's a match
//Until this part, the above loop already finish executed, so the below IF is NOT part of the loop
//Therefore when myPlaces[i] !== friendPlaces[j], is actually undefined !== undefined which equals to false, so the code below will forever NOT get executed
if (myPlaces[i] !== friendPlaces[j]) {
console.log('There are no places we have in common Christi Pants!!!');
}
You need to understand when you write myPlaces[i]
outside of a loop, your i
is not defined, because i
is only defined inside the for
loop statement. Same goes for friendPlaces[j]
as j
is not defined. You can test this by putting console.log(myPlaces[i])
before your IF statement to see what is printed.
var myPlaces = ['here', 'new job', 'Hawaii'];
var friendPlaces = ['in', 'there', 'jamaica'];
//Same thing, computer will execute from top to bottom
for (var i = 0; i < myPlaces.length; i++) {
for (var j = 0; j < friendPlaces.length; j++) {
if (myPlaces[i] === friendPlaces[j]) {
console.log('Our favorite place in the world is: ' + ' ' + myPlaces[i] + '!!!');
}
}
}
//Loop finishes
//What is the value of myPlaces[i]? friendPlaces[j]?
console.log(myPlaces[i]); // result is undefined
console.log(friendPlaces[j]); // result is undefined
if (myPlaces[i] !== friendPlaces[j]) {
console.log('There are no places we have in common Christi Pants!!!');
}
Result:
undefined
undefined
The console.log(myPlaces[i]);
and console.log(friendPlaces[j]);
will print undefined
, so when you test for if (undefined !== undefined)
, the result is false
, therefore the console.log
inside will never get executed.
For slightly advance explanation:
(Just in case you wonder, and for completion):
(Ignore this if it’s getting too hard to understand, it’s okay for you to not understand it now)
The leakage of i
value using var
in for loop statement, means that i = 3 when the loop ends, if you console.log(i)
, you will get value 3, like below:
var myPlaces = ['here', 'new job', 'Hawaii'];
var friendPlaces = ['in', 'there', 'jamaica'];
//Same thing, computer will execute from top to bottom
for (var i = 0; i < myPlaces.length; i++) {
for (var j = 0; j < friendPlaces.length; j++) {
if (myPlaces[i] === friendPlaces[j]) {
console.log('Our favorite place in the world is: ' + ' ' + myPlaces[i] + '!!!');
}
}
}
//Loop finishes
//What is the value of myPlaces[i]? friendPlaces[j]?
console.log(i); // result is 3;
console.log(j); // result is 3;
console.log(myPlaces[i]); // result is undefined
console.log(friendPlaces[j]); // result is undefined
if (myPlaces[i] !== friendPlaces[j]) {
console.log('There are no places we have in common Christi Pants!!!');
}
so with that logic, myPlaces[i]
, i
replaced by 3, myPlaces[3]
=== undefined
because there’s no value at index 3 inside array myPlaces
. Same goes for j
. This leakage is why let
is introduced in the ES6 JavaScript newer syntax, where if you console.log(i)
after for loop by using let
, there’s no leakage value of i
, instead an error ReferenceError: i is not defined
will be returned.
Version 3:
For Version 3, you’re building a faulty code based on the Version 2, that will get complicated very quickly, just remember when you use break
, it will break the loop, important point: think of this, if you have three places in common, when you use break
, the loop will be broken and ended at break
and the rest of the same places will not get printed anymore because the loop stop running.
Lastly, I would really suggest you to learn how to format your code in your post, refer here, as it will take me or anyone else who wants to help much more time to format your code by copying it and change it one by one because how the forum twisted your code.
Give my suggested solution a go, like I said, there will be different ways to write codes, but I think that solution may be easier to solve your situation. Also, go through the previous exercises, if you’re having trouble with the foundation concepts, it’s better to make clear before you move to the harder ones. I think this is getting very long, that’s all.
