// You must to find the treasure.
// This function should return the index of a letter:
function letterIndex(word, letter) {
// Step through each letter as an index of the word.
for i++ in range 0, (length (word), 1){
// Store a character from the word with the current index.
var letter= letterIndex[]
// If it is the required letter:
if (word [i++] == letter){
// Then return the current index (number).
return index
}
}
// If nothing, return a default value
return -1;
}
var ogreLetter = "z";
var shaman = hero.findByType("thoktar")[0];
// Find the index and use it for finding the treasure.
var chestIndex = letterIndex(shaman.id, ogreLetter);
hero.moveXY(16 + chestIndex * 8, 36);
The main problem is your for loop. What you’ve got above is not how you do it unfortunately.
For loops in javascript take the following format:
for (var i = 0; i < maxNumber; i++) {
Notice the three sections separated by semi colons ( ; ).
Section one declares the variable i as 0.
Section two declares the boolean statement (true of false) which must be true for the loop to run. Meaning, i must be less than a certain maxNumber for it to run. You can use array.length, which contains the number of items in a certain array (like word).
The rest is also wrong, but I’ll let you try and fix that bit first, now that the for loop should work.
Danny
Thanks guys, but now I get the error letter is already defined on line 8:
// You must to find the treasure.
// This function should return the index of a letter:
function letterIndex(word, letter) {
// Step through each letter as an index of the word.
for (var i = 0; i < maxNumber; i++) {
// Store a character from the word with the current index.
var letter = letterIndex[i];
// If it is the required letter:
if (word [i++] == letter){
// Then return the current index (number).
return index
}
// If nothing, return a default value
else{
return -1
}
}
}
var ogreLetter = "z";
var shaman = hero.findByType("thoktar")[0];
// Find the index and use it for finding the treasure.
var chestIndex = letterIndex(shaman.id, ogreLetter);
hero.moveXY(16 + chestIndex * 8, 36);