Skip to main content
  1. About
  2. For Teams
Asked
Viewed 927 times
2

making (lame) game, and i want to check if selected square contains a monster, however my indexOf keeps returning -1. relevant code below.

intialization:

var monsters = [];

var monsterPositions = [2,8,13,15,22];

var player = { "currentPosition": 0 };

for ( var i in monsterPositions ) {
    monsters[i] = new createMonster("monster",monsterPositions[i],1); }

function createMonster(name,startingPoint,level) {
    this.currentPosition = startingPoint; }

function triggered with <td> onclick:

function processClick(trigger){
    console.log( trigger.id + " " + monsterPositions );
    if ( monsterPositions.indexOf(trigger.id) >= 0 ){
        if ( !fight( findMonster( parseInt( trigger.id ) ) ) ){
            return;
        }
    }
    if ( gameOn ) move( parseInt( trigger.id ) );
}

the page always goes straight to move(), even when it is a monster spot, here is console.log() results:

1 2,8,13,15,22 adventure.js (line 79)
2 2,8,13,15,22 adventure.js (line 79)
7 2,8,13,15,22 adventure.js (line 79)
8 2,8,13,15,22 adventure.js (line 79)
13 2,8,13,15,22 adventure.js (line 79)
18 2,8,13,15,22 adventure.js (line 79)
23 2,8,13,15,22 adventure.js (line 79)
22 2,8,13,15,22 adventure.js (line 79)
21 2,8,13,15,22 adventure.js (line 79)
20 2,8,13,15,22 adventure.js (line 79)
15 2,8,13,15,22 adventure.js (line 79)

(link here if above code isnt enough), and im stumped help please.(of course why else would i be here)

2 Answers 2

5

trigger.id is probably returning a string where your array is full of numbers.

This jsfiddle shows that in action as well as it working with a Number and how to convert your string IDs to numbers using parseInt().

Sign up to request clarification or add additional context in comments.

4 Comments

oh wow i knew that just spaced, lol i had this before, (as you can see i use parseInt on the next line) thanks.
Be sure to always pass 10 as the second argument of parseInt(trigger.id,10)
yeah whats up with that? i dont really get the whole "radix" thing.
According to MDN "An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified." If you do parseInt('08') on certain browsers, you won't get 8.
1

try this

 if ( monsterPositions.indexOf(Number(trigger.id)) >== 0 )

5 Comments

ive never heard of number before i will check it up and try it, but parseInt is a known valid function, i dont see why it would work.
It is bad practice to use Number
thanks anyway was a bit spaced didnt notice that i missed parseInt as you can see from my comment
@Zero21xxx Can you also add why it's bad practice? :D
Well for one reason, it can easily be overridden. Anyone in any file can simply do Number = function() {return false} and BAM, now your code is broken. It is generally preferred to use literal notation if possible or any of the dozen other ways to convert a string to a number.

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.