I don't understand this at all. Adding a while loop causes a variable to throw an "Uncaught type error cannot read property of 'perp'". I tried many solutions, separating code, adding additional typing, trying to make :any type, I am truly at a loss.
Little background info: I am trying to make a quick little game called "Three's a Crime", essentially I have an algorithm that displays up to 3 criminals, at most 2 of them may be the actual perpetrators.
All the code is publicly view-able:
function main(num) {
var is_over = false;
var game = new GameObject.Game(shuffled_criminals, num);
$('body').append(game.display());
$("#status").text("Turn in progress");
$('body').append('<div class="container"><p>Enter your guess<p><div class="input-append" id="turn_form"><form>\
<input class="span2" id="Text1" type="text" />\
<button class="btn" id="Button1" type="button">Go!</button>\
</form>\
</div></div>');
console.log(game.getThree());
while (true) //removing this while loop causes above line to work fine.
{ }
}
here is the getThree method within the game class
getThree() {
var result: Criminal[] = [];
var temp: Criminal[] = _.shuffle(this.criminals);
var num_perp = 0;
while (result.length != 3) {
while ((num_perp != 3) && (result.length != 3)) {
var x: Criminal = temp.pop();
result.push(x);
if (x.perp == true)
num_perp++;
temp = _.shuffle(temp);
}
do {
temp = _.shuffle(temp);
var y: Criminal = temp.pop();
if (y.perp == false)
result.push(y);
}
while (result.length != 3)
}
return result;
}//end displayThree
I am using underscorejs for the shuffling, and the DefinitelyTyped underscore typings.
Feel free to view or clone the repo to try. This error is occuring in Google Chrome 26. All the code is within game.ts, gameobject.ts and main.ts, theres lots of references to outside libraries like underscore and jquery ui which I plan to spruce up the GUI a little once I get the main game logic plete.
Thank you for any who have even bothered to read all this, your help is much appreciated.
edit: so I'm trying to use a loop like this to put a "turn" in the game
while (is_over)
{
break;
}
I don't understand this at all. Adding a while loop causes a variable to throw an "Uncaught type error cannot read property of 'perp'". I tried many solutions, separating code, adding additional typing, trying to make :any type, I am truly at a loss.
Little background info: I am trying to make a quick little game called "Three's a Crime", essentially I have an algorithm that displays up to 3 criminals, at most 2 of them may be the actual perpetrators.
All the code is publicly view-able: https://github./treehau5/ThreesACrime
function main(num) {
var is_over = false;
var game = new GameObject.Game(shuffled_criminals, num);
$('body').append(game.display());
$("#status").text("Turn in progress");
$('body').append('<div class="container"><p>Enter your guess<p><div class="input-append" id="turn_form"><form>\
<input class="span2" id="Text1" type="text" />\
<button class="btn" id="Button1" type="button">Go!</button>\
</form>\
</div></div>');
console.log(game.getThree());
while (true) //removing this while loop causes above line to work fine.
{ }
}
here is the getThree method within the game class
getThree() {
var result: Criminal[] = [];
var temp: Criminal[] = _.shuffle(this.criminals);
var num_perp = 0;
while (result.length != 3) {
while ((num_perp != 3) && (result.length != 3)) {
var x: Criminal = temp.pop();
result.push(x);
if (x.perp == true)
num_perp++;
temp = _.shuffle(temp);
}
do {
temp = _.shuffle(temp);
var y: Criminal = temp.pop();
if (y.perp == false)
result.push(y);
}
while (result.length != 3)
}
return result;
}//end displayThree
I am using underscorejs for the shuffling, and the DefinitelyTyped underscore typings.
Feel free to view or clone the repo to try. This error is occuring in Google Chrome 26. All the code is within game.ts, gameobject.ts and main.ts, theres lots of references to outside libraries like underscore and jquery ui which I plan to spruce up the GUI a little once I get the main game logic plete.
Thank you for any who have even bothered to read all this, your help is much appreciated.
edit: so I'm trying to use a loop like this to put a "turn" in the game
while (is_over)
{
break;
}
Share
Improve this question
edited Apr 16, 2013 at 19:17
darethas
asked Apr 16, 2013 at 17:51
darethasdarethas
7,6973 gold badges25 silver badges32 bronze badges
8
-
The
while
loop makes it "work" because it pletely freezes JavaScript evaluation at that point. – Pointy Commented Apr 16, 2013 at 17:53 - Also multi-line strings like that won't work in all browsers. – Pointy Commented Apr 16, 2013 at 17:53
- @Pointy the while loop is actually what causes the error to happen, not vice-versa. The multi-line string is the least of my concern right now its only gui related. I'm more concerned where the logic error may be. That is, unless there is some weird correlation between multi-line strings and type errors. – darethas Commented Apr 16, 2013 at 17:57
- ah - well that's ... interesting. – Pointy Commented Apr 16, 2013 at 17:59
- What is the resulting javascript – Esailija Commented Apr 16, 2013 at 17:59
2 Answers
Reset to default 2In your question you are using the following example:
while (true) //removing this while loop causes above line to work fine.
{ }
This tight loop will prevent pretty much anything from happening (and usually the browser will ask if you want to terminate the script.
If this isn't the code you are using, please show the code you are using - ideally a shortened version that demonstrates the issue rather than an entire program. I have tested several variations of loops in TypeScript and can find no problem with a while loop.
Try
while(false){
// Unreachable code
do_something;
break;
}
If what is inside the condition ie. anything
in your case never bees false then it is effectively while(true)
which as mentioned by steve fenton would be stopped by browsers or crash.
A slightly better way to loop would be:
function loop(){
var result = do_something();
// Repeat
if (result) {setTimeout(loop,100);}
// Break
else return;
}
setTimeout(loop,100);
Although I would remend using a game engine instead of setTimeout. e.g http://www.createjs./#!/EaselJS