[SOLVED]I need help on In my name JavaScript

I can’t seem to fix this error:

Here is my code
// 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);


5 Likes

Can you give a link to the level Jonathan?

3 Likes

Sure thing, give me a second

4 Likes
4 Likes

@Monsty ? Are you there?

4 Likes

Hey is anyone able to help me? Ive been stuck on this for a while…

6 Likes

@Monsty ? @anon62741001 ?

4 Likes

Yes I am here I am looking over it

4 Likes

Thank you @anon62741001

6 Likes

@abc? you liked my post, can you help me?

4 Likes

I don’t know much Javascript, but doesn’t JS require parenthesis in while statements, if statements, and for statements?

3 Likes

I still get the same error

4 Likes

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

6 Likes

The for loop works now, but I get the error 'expected an Identifier on line 8, but saw ‘]’ ’ on line 8

4 Likes

@JoPro_8000 put the i from the for loop into the brackets
like this

        var letter= letterIndex[i]

add the i onto the brackets

3 Likes

Try putting this under an else statement.

4 Likes

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);

4 Likes

yeah delete the var from the var letter= letterIndex[i]

3 Likes

Thanks, I fixed most of the bugs, but in line 6 it says maxNumber not defined

4 Likes

I dont think you are supposed to use max number…

3 Likes