Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 07:32 PM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
returning out the parent function from a foreach function

This is typescript but works similarly to JS.

How can I get the main function hasEnemyLeftBounds to return true. In the for each it currently only leaves the current for each


Code:
   hasEnemyLeftBounds(): bool {
        this.enemies.forEach(function (enemy: GameObjects.GameObjects.Enemy) {
            if ((enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH)) {
                return true;
            }
        });
        return false;
    }
__________________

nikos101 is offline   Reply With Quote
Old 01-05-2013, 07:40 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Presumably it should return true if any one of the enemies has left? In JS:

Code:
hasEnemyLeftBounds(): bool {
        var hasLeft = false; // TypeScript: bool hasLeft = false; ??
        this.enemies.forEach(function (enemy: GameObjects.GameObjects.Enemy) {
            if ((enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH)) {
                hasLeft = true;
                // has TypeScript a 'break' statement to break out of the forEach?
            }
        });
        return hasLeft;
    }
Currently, it will continue searching even if the current enemy has left. You might investigate to see if 'break', or similar, will break out of the forEach loop; there's no point checking the rest. Added: Yeah, a 2 second search tells me that you can just add break; to break out of the loop. Added Again: Actually, I don't think you can(?).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-05-2013 at 07:51 PM..
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 07:44 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
var wibble: bool; // TypeScript
wibble = false;
Seems a bit unnecessary to me
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 07:57 PM   PM User | #4
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
seems I should just use a for (int i" instead for simplicity
__________________

nikos101 is offline   Reply With Quote
Old 01-05-2013, 08:05 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by nikos101 View Post
seems I should just use a for (int i" instead for simplicity
Unless you are unfortunate enough to have hundreds of enemies then it is not a big deal. But, yes, the forEach is designed to look at each/EVERY item, so it does not have an inbuilt method (or simple way) to break out of the sequence.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 08:14 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
This TypeScript/JS/Array business is confusing me a little, but if this.enemies is an array then you might be able to do

Code:
for (index in this.enemies) {
    if (this.enemies[index].x < 0 || etc..) {
        hasLeft = true;
        break;
    }
}
return hasLeft;
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-05-2013 at 08:17 PM..
AndrewGSW is offline   Reply With Quote
Old 01-07-2013, 10:30 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Code:
function hasEnemyLeftBounds(enemies) {
       return enemies.every( function(enemy) {
                return enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH ;
        } );
}
where enemies is an array and you want to know if all enemies satisfy the condition.
every() terminates upon the first falsy return value, so you don't need a collector or status var like you would if you tried to use .forEach(), and you can bail-out early, which is much faster in practice.

if you want all enemies that satisfy the condition, replace the word "every" with "filter".

if you want to end iteration when the first condition is trueish (as opposed to falsy), use .some() instead of .every().
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:34 PM.


Advertisement
Log in to turn off these ads.