
previous

page 4
next
missing letter(s) is easily fixed
if (arr.length==letters.length) {
var s='[';
var t=',';
for (var j=0;j<arr.length;j++) {
if (j==(arr.length-1)) t='],<br>';
s=s+"'"+arr[j]+"'"+t;
}
document.write(s);
}
var s='[';
var t=',';
for (var j=0;j<arr.length;j++) {
if (j==(arr.length-1)) t='],<br>';
s=s+"'"+arr[j]+"'"+t;
}
document.write(s);
}
so too is 'no country is the same as the answer'
if (l==first_letter && c!=c0) {
but 'repetition' is not so easy
We need to ask is a country already in the array we are building (arr)?
This means we need a way to ask the question, "Is this item in this array?"
JavaScript offers the indexOf function, returning -1 if the item is not in the array.
if (l==first_letter && c!=c0) {
if (arr.indexOf(c)==-1) {
arr.push(c);
break;
}
}
if (arr.indexOf(c)==-1) {
arr.push(c);
break;
}
}
previous
next