Nested for loops, lesson 5


#1

Hi. How to make both words ‘Barcelona’ and ‘Paris’ appear in a colon, not in a separate line???


#2

I have this, and its works :slight_smile:

let myPlaces = [‘Barcelona’, ‘Honolulu’, ‘Paris’];
let friendPlaces = [‘New York’, ‘Paris’, ‘Barcelona’];

let matches = []

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

for (let friendPlacesIndex = 0; friendPlacesIndex < friendPlaces.length; friendPlacesIndex++) {

if (myPlaces[myPlacesIndex] === friendPlaces[friendPlacesIndex]) {
  
  matches.push(myPlaces[myPlacesIndex])
  
}

}
}

console.log(Match: + matches);


#3

then use .join() on matches so it displays as string and not as array:

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/join


#4

I have another problem :wink:

Its print: “Match: Barcelona,Paris”, but I wanna “Match: Barcelon, (space) Paris”.

How to do it?

Thanks


#5

.join() is ideal for that? You can decide on the characters between the elements of the arary


#6

Thanks a lot stetim94, it was so easy but did it around :smiley:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.