About lesson 6: chapter 5


#1

I wrote to the code to match the same places but there is a thing which I can not understand.

let myPlaces = [‘Istanbul’,‘Bursa’,‘Ankara’];

let friendPlaces = [‘istanbul’,‘Adana’,‘Denizli’];

for (let myPlacesIndex = 0; myPlacesIndex < myPlaces.length; myPlacesIndex++){

for (let friendPlacesIndex = 0; friendPlacesIndex < friendPlaces.length; friendPlacesIndex++){
if(myPlaces[myPlacesIndex]===friendPlaces[friendPlacesIndex]){
return console.log('Match: '+myPlaces[myPlacesIndex]);

}

}
}

When we write like above, there is no problem and the code works smoothly. Let’s add new same places but in a different position.

let myPlaces = [‘istanbul’,‘Bursa’,‘Ankara’,‘England’];

let friendPlaces = [‘istanbul’,‘England’,‘Adana’,‘Bursa’];

for (let myPlacesIndex = 0; myPlacesIndex < myPlaces.length; myPlacesIndex++){

for (let friendPlacesIndex = 0; friendPlacesIndex < friendPlaces.length; friendPlacesIndex++){
if(myPlaces[myPlacesIndex]===friendPlaces[friendPlacesIndex]){
return console.log('Match: '+myPlaces[myPlacesIndex]);

}

}
}

The output is the same again. The code writes only Istanbul. Why doesn’t write England too?
How can I fix this?


#2

Remove return from your loop, and just log some output.


#3

Ups okay I understood but I want to ask something. What is the function of the return exactly?


#4

return cannot be used outside of a function. It’s purpose is to send data back to the caller. A function assigns an output to an input by way of the return statement.

function (input) --> process --> return (output)
function add_ab(a, b):
    return a + b

c = add_ab(6, 7)
console.log(c)     // 13