-1

I am working on a Javascript exercise [1]. Below is what I have written. I think, I am right. But it is obviously flawed. I do not have any clue what went wrong. Do you see?

I tried debugging. How do I debug? Do you see any mistake?

var lost = [4, 8, 15, 16, 23, 42];
var isLost = function (n) {
 for (var i=0; i++; i <lost.length ) {
 if ( n === lost[i]) {
 return false;
 }
 }
 return true;
};
var ret = isLost(12);
if ( ret === true) {
 console.log('12 is a lost number');
}
var ret = isLost(16);
if ( ret === true ) {
 console.log('16 is a lost number');
}
asked Feb 20, 2012 at 2:06

3 Answers 3

3

You got the for header wrong. Should be:

for ( var i = 0; i < lost.length; i++ )
answered Feb 20, 2012 at 2:09
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Wew!! Shoot me, I am such a dumbo.. But that still got me this. Not sure what is still wrong. 12 is a lost number 16 is not lost number Oops, try again.
@GP You got the return statements reversed. Inside the if statement it should be return true, and at the end of the function should be return false...
2

I tried debugging. How do I debug?

Use FireBug, or the debugger in Chrome. Debugging is probably the single most important skill to have in any platform, so spend some time learning this as one of the first things you do.

answered Feb 20, 2012 at 2:35

1 Comment

+1 Thanks! I was using alert and then console.log. I am still need to get used to Firebug. Will do. Thanks!
-1
var lost = [4, 8, 15, 16, 23, 42];
var count = lost.length;
var isLost = function (n) {
 for (var i = 0; i < lost.length; i++) {
 if (n === lost[i]) {
 return true;
 }
 }
 return false ;
};
if ( isLost(12) ) {
 console.log('12 is a lost number');
}
if ( isLost(16) ) {
 console.log('16 is a lost number');
}
answered Jan 14, 2014 at 11:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

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.