QC Javascript : for in

yanomano's picture

I'am learning Javascript with QC and wondering why this code return just one of the two properties of the Object "myObject" .

var result = new Object();
var myObject= { posX:2, posY:3 };
 
function (__string outputString) main ()
{
   result.outputString=enumeration(myObject);
   return result
}
 
function enumeration(a)
{
   var name;
   for(name in a){
      return name
   }
}

i'am doing a mistake ?

PreviewAttachmentSize
enumeration.qtz5.1 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

.lov.'s picture
Re: QC Javascript : for in

because the return name; will put the !last! value to the output. When the enumartion runs first, the value will posy, but last the value will posy.

see the comp bellow.

PreviewAttachmentSize
enumeration_0.qtz5.15 KB

yanomano's picture
Re: QC Javascript : for in

yes ! Thanks! Those machines never take any initiative ;) This lesson make my day :)

.lov.'s picture
Re: QC Javascript : for in

you're welcome ;)

and, it's a typo in my first comment

"the value will posX, but last the value will posy."

yanomano's picture
Re: QC Javascript : for in

So with your change i can now list the 2 properties :

var result = new Object();
 
var myObject= { posX:2, posY:3 };
 
function (__string outputString) main ()
{
   result.outputString=enumeration(myObject);
   return result
}
 
function enumeration(a)
{
   var name = "";
   var current;
   for(current in a){
      name=name+""+current
   }
   return name
}

So here is my first loop ( i realize it's a very little step for the humanity ;) to list every property :

var result = new Object();
 
var myObject= { posX:2, posY:3, posZ:7, rotX:3, rotY:5, rotZ:0 };
 
function (__string outputString) main ()
{
   result.outputString=enumeration(myObject);
   return result
}
 
function enumeration(a)
{
   var result="";
   for( var everyProperties in a){
      result+=everyProperties+"\n";
   }
   return result
}
PreviewAttachmentSize
enumeration_1.qtz5.16 KB

cwright's picture
Re: QC Javascript : for in

yanomano: I edited the formatting on your comment a bit so that it's easier to read. I hope you don't mind.

leegrosbauer's picture
Re: QC Javascript : for in

How can this assembly be utilized to display values? Looks very useful, but I don't know how to apply it.

yanomano's picture
Re: QC Javascript : for in

Thanks chris....i'am just discovering the javascript code tag :)

yanomano's picture
Re: QC Javascript : for in

I don't know if it is very useful, it was just a basic learning test to display properties of an object ;)

leegrosbauer's picture
Re: QC Javascript : for in

ah, understood. Thanks! I hope to learn Javascript, too.