How is correctly called "parent" of nested loop? - terminology

I have a general question across the languages. How is correctly called the "parent" loop? The higher one:
for(int A=0;A<10;A++)
{
for(int B=0;B<10;B++)
{
}
}

I would say the "outer loop" and the "inner loop".

Everything you need to know about loops:
http://www.wisegeek.com/what-is-a-nested-loop.htm

Related

Using splice() to remove items that matches condition from array

Im trying to via a for-loop remove all the items that matches a condition in the state array. But it seems to only be removing the last items in the array and not the ones that matches. Am I using the .splice() incorrectly? Thanks in advance. Code is:
rmTravel() {
for(var i = 0; i < this.cards.length; i++){
if(this.cards[i].sg_categories.includes("travel")){
this.cards.splice(i, 1);
console.log('Removed following card:', this.cards[i].slug)
}
}
console.log('Cards in cards state: ', this.cards)
}
This is a bit of a classic problem; you're iterating forward and shrinking the array at the same time so you're going to end up skipping over records.
I suggest using Array.prototype.filter() instead
this.cards = this.cards.filter(({ sg_categories }) =>
!sg_categories.includes('travel'))
This will reduce the array to entries who's sg_categories property does not include "travel".

How to detect how many objects are in the display object? (AS3)

I'm making a game in which coins are randomly generated. Once a coin (multiple on the screen at once) is collected it respawns in another location. I have a few ways of doing this, but to advance I need these questions answered:
How would I go about detecting how many objects are in the display list?
How to detect when an object is removed from the display list?
All answers are appreciated in advance.
for detecting number of objects, you could do a recursive function that loops through each all children, and returns the count
import flash.display.DisplayObjectContainer;
function numAllChildren($do:DisplayObjectContainer):int{
var $c:int = 0;
for (var $i:int = 0; $i < $do.numChildren; $i++){
if($do.getChildAt($i) is DisplayObjectContainer){
$c += numAllChildren($do.getChildAt($i) as DisplayObjectContainer);
}
$c++;
}
return $c;
}
trace(numAllChildren(_mc));
To see if there has been a change, you'd use an event listener on ADDED and/or REMOVED. But you'll need to put it on every DisplayObject
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#ADDED
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#REMOVED
Take a look here : DisplayObjectContainer
It contains all what you need, for your 2nd question see Events part.

On every children add 1 (+1) Umbraco Razor

In the code below i need it to add (+1) on every children on data-slide-index="0".
Starting at 0 and then 1,2,3,4,5
It should look like this:
data-slide-index="0"
data-slide-index="1"
data-slide-index="2"
I am thinking something like this.
int theCount = 0;
theCount += 1; // Adds 1 to count
But I dont know how to use it correctly in the code.
#foreach (var image in #Model.Children)
{
foreach (dynamic d in image.imageDampSingle)
{
<a data-slide-index="0" href="#DAMP_Helper.GetImageCropperUrl(d, "projectSingle")"><img src="#DAMP_Helper.GetImageCropperUrl(d, "projectSingleThumb")" title="#Model.captionText" alt="#d.Image.nodeName" /></a>
Or is there a easier / another way to do this?
Thanks in advance!
//René
Razor is just a view engine, you are using C# code in there to do some logic operation. Recursive methods are algorithmic operations that have the capability to call themselves until a certain condition is satisfied. You can use a helper in this case to achieve that. An example for your case:
#helper DoSomething(IEnumerable<Something> myList)
{
foreach (var item in myList)
{
#item.CertainProperty
#if (item.Children.Any())
{
#DoSomething(item.Children)
}
}
}
Learn and read about recursion, and a whole new world of fundamental concepts will appear.
http://en.wikipedia.org/wiki/Recursion_(computer_science)
http://learnyousomeerlang.com/recursion
It's a fundamental topic, I was actually programming some snippets right now when I read this topic, where's other example in Python:
def contaVogais(cadeia):
if not cadeia:
return 0
return (1 if cadeia[0] in 'aeiou' else 0) + contaVogais(cadeia[1:])
As you can see by the examples, recursive solutions are usually more elegant, but in some cases might have a decrease the code performance.
I found a solution. See below:
#{
var countNum = -1;
}
#foreach (var image in #Model.Children)
{
{
countNum += 1;
}
foreach (dynamic d in image.imageDampSingle)
{
<a data-slide-index="#countNum" href="#DAMP_Helper.GetImageCropperUrl(d, "projectSingle")"><img src="#DAMP_Helper.GetImageCropperUrl(d, "projectSingleThumb")" title="#Model.captionText" alt="#d.Image.nodeName" /></a>
}
}
You are welcome to comment on it regarding improvement of the code.
//René

How to suppress the warning "Assignment within conditional. Did you mean == instead of =?"

With the new ASC 2.0 compiler I get warnings when I code like below:
// (_achievementsFromServer is an Array)
while(item=_achievementsFromServer.pop())
{
// do something with item here
}
The warning reads: "Assignment within conditional. Did you mean == instead of =?"
While in general I appreciate all warnings from the compiler, I'd like to suppress this one in this case because I did not mean == here. I want to pop all items in the array and do something with it until the array is empty.
while( (item=_achievementsFromServer.pop())==true )
seems to work but looks a bit confusing. Any other ideas?
This may seem better.
while(_achievementsFromServer.length > 0) {
var item:Object = _achievementsFromServer.pop();
}
Just like removeChild
var d:DisplayObjectContainer;
while(d.numChildren > 0) {
d.removeChildAt(0);
}
While I was hoping for some other way, I think #AmyBlankenship improved my own suggestion:
while((item=_achievementsFromServer.pop())!=null)
{
//....
}
It's clear and understandable what's going on, and doesn't rely on checking the length of the Array on every iteration.
Googling some more I found a compiler option -compiler.warn-assignment-within-conditional that could be set to false but then you won't be warned anywhere in your project anymore. And I'm not so confident that I never accidently type = instead of ==, so that's not a good solution I think.

Linq2Sql referring to entity column by variable creation

I have a linq2sql class with fields
WeekEnding1
WeekEnding2
WeekEnding3
WeekEnding4
I want to write some c# using the fields in a for loop.
Take this for example:
for(int i=1; i<=4; i++)
{
Msgbox(myClass.WeekEnding + i)
}
I realise that wont work but what will??
Malcolm
Unless you want to get into something with reflection, this will:
MsgBox(myClass.WeekEnding1);
MsgBox(myClass.WeekEnding2);
MsgBox(myClass.WeekEnding3);
MsgBox(myClass.WeekEnding4);
You can do what you're trying to do with reflection by putting this inside the loop:
PropertyInfo info myClass.GetType()
.GetProperty("WeekEnding" + i.ToString(),
BindingFlags.Public | BindingFlags.Instance);
MsgBox(info.GetValue(myClass, null));
But I'd recommend the first approach! The second approach will have to find the property in question on each pass through the loop, adding a considerable overhead.
In any event, your underlying data model sounds very much like it might need normalising - this is a common bad smell!