espace Posted January 21, 2018 Share Posted January 21, 2018 hi, I have a gameProgress (e) where each position in my array are stars (1,2) or blocked levels (-1) I want to count the position of the blocked level so i use this ; var e=[1,2,1,1,-1]; var n = 0; while (e[n] < 0) { n++; var position=n; } console.log(position); // expected output: 4 and result is 3 ??? but the result is not 4 => e=[1,2,1,1,-1] could you tell me why ? Quote Link to comment Share on other sites More sharing options...
in mono Posted January 21, 2018 Share Posted January 21, 2018 Just swap the increment step with the next line. Quote Link to comment Share on other sites More sharing options...
espace Posted January 21, 2018 Author Share Posted January 21, 2018 sorry but i don't understand very well ....i do that but it's not true : var e=[1,2,1,1,-1]; var n = 0; while (e[n] < 0) { var position=n; n++; } console.log(position); // undefined Quote Link to comment Share on other sites More sharing options...
espace Posted January 21, 2018 Author Share Posted January 21, 2018 find by myself var n = 0; var e=[0,2,1,-1] while (e[n] != -1) { n++; } console.log(n); // 3 Quote Link to comment Share on other sites More sharing options...
mkardas91 Posted January 22, 2018 Share Posted January 22, 2018 var e=[0,2,1,-1] console.log(e.indexOf(-1)); // 3 https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/indexOf espace 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.