
previous

page 2
next
my diff_letters function
function diff_letters(word) {
var arr = word.split('');
Sort(arr);
for (var i=0; i<(arr.length-1); i++) {
if (arr[i]==arr[i+1]) return false;
}
return true;
}
var arr = word.split('');
Sort(arr);
for (var i=0; i<(arr.length-1); i++) {
if (arr[i]==arr[i+1]) return false;
}
return true;
}
You probably had i<3. I decided to make my function more general and introduce you to the length property of an array at the same time. Maybe you'll want to create a Words 5 game when we finish?
array of words with all letters different
More on arrays: we can add an item to an array using the push function. For example: if we had an array called words and we wanted to add the item word to the array:
words.push(word);
In your words4.html fill the array words with words that have each letter different.
<script src='words4.js'></script>
<script src='Sort.js'></script>
<script src='diff_letters.js'></script>
<script>
var words = [];
for (var i=0; i<words4.length; i++) {
if (diff_letters(words4[i])) {
words.push(words4[i]);
}
}
</script>
<script src='Sort.js'></script>
<script src='diff_letters.js'></script>
<script>
var words = [];
for (var i=0; i<words4.length; i++) {
if (diff_letters(words4[i])) {
words.push(words4[i]);
}
}
</script>
previous
next