DrakaSAN
DrakaSAN

Reputation: 7853

Comparison to -1 or undefined don t work, either with == or ===

Here a sample of code:

do
{
    line=gets(smil);
    if (line===-1){console.log("Abort");callback(-1);}
    console.log("line= "+line);
}while(line.search("<video")===-1);

The first output goes well, but suddenly, I get

line= -1
TypeError: Object -1 has no method 'search'

gets is a custom function, wich return either a string or -1 in case of error.

I tried with == instead of ===, but it just do the same.

I tried to replace the return -1 of gets by undefined, but I just get

line= undefined
TypeError: Cannot call method 'search' of undefined

instead.

Why the if don t execute?

EDIT: Tried with

var pline
do
{
    pline=gets(smil);
    if (pline===-1){console.log("Abort");callback(-1);}
    console.log("line= "+pline);
}while(pline.search("<video")===-1);

to avoid overriding another variable, got the same result

Upvotes: 1

Views: 64

Answers (2)

Henrik Andersson
Henrik Andersson

Reputation: 47172

It's a two-fer this question.

1st - You're overwriting the variable line right after the do. That's why you're getting Uncaught TypeError: Object -1 has no method 'search'

Change that line to parsed_line = gets(smil); //also never forget semicolons

2nd - search isn't a function of String, you want the indexOf() function.

So change while(line.search()) to while(line.indexOf()).

I made a fiddle for you that demonstrates it here

Upvotes: 1

kpull1
kpull1

Reputation: 1653

try adding a break.

do
{
    line=gets(smil);
    if (line===-1){
        console.log("Abort");
        callback(-1);
        break;
    }
    console.log("line= "+line);
}while(line.search("<video")===-1);

Upvotes: 0

Related Questions