I've encountered a curious issue with the code snippet below. Everything seems to be running smoothly except for one thing - after a few iterations, I start getting "undefined" as an output.
You can test this for yourself by running the code multiple times. Initially, you will see the expected output of three random cities, but at some point, "undefined" will pop up. This usually occurs within less than 10 iterations.
I'm puzzled as to why this is happening. What am I overlooking in my code?
var coolWords = ["Amsterdam", "Berlin", "Sofia", "Prague", "Lisbon", "London", "Malaga", "Copenhagen", "Milan"];
var newList = [];
function niceTripleCombo(coolWords) {
for (let i = 0; i < 3; i++) {
var combo = coolWords[Math.floor(Math.random()*coolWords.length)];
newList.push(" " + combo);
};
};
function iterator(x) {
if (newList[0] !== newList[1] && newList[1] !== newList[2] && newList[1] !== newList[3]) {
return newList;
} else {
return niceTripleCombo(coolWords);
}
};
console.log(iterator(niceTripleCombo(coolWords)));