...

View Full Version : Please Explane - var parts = {front:1<<0, back:1<<1, cd:1<<2,} _____ [Resolved]



Misspell
09-22-2005, 07:27 AM
Hello all... looking for alittle help, here.

Can somone explane to me how this code works.

:snip:


var parts = {front:1<<0, back:1<<1, cd:1<<2, cd2:1<<3, cd3:1<<4, cd4:1<<5, inside:1<<6, inlay:1<<7};

var titleCode = elem.options[elem.selectedIndex].value;

for (var i in parts) {
if(titleCode&parts[i]) {
//alert(titleCode + " <>" + i);
buffer += '[<a href="" onClick="titlePart.value=\''+i+'\';titleSubmit.focus();return false;" ';
buffer += 'onMouseOver="window.status=\''+ucFirst(i)+'\';return true;" onMouseOut="window.status=\'\';return true;">';
buffer += '<strong>'+ucFirst(i)+'</strong></a>]&nbsp;&nbsp;';
}
}

:/snip:


I know what the out come is and where the info is coming from, but dont know how it does its thing.
Heres an example of what it does:

elem.options[elem.selectedIndex].value is 197 (taking from a <select> box off a form)

when looping through the 'parts' array (is that what it is ? thought arrays where defined differently in javascript) it will turn that 197 into;
[Front] [Cd] [Inside] [Inlay] (takin from the 'parts' "array")

The ucFirst() function just capitalize the first letter of the values held in the 'parts' array.

Another example...
If elem.options[elem.selectedIndex].value = 1 then it would return;
[Front]
If elem.options[elem.selectedIndex].value = 5 it would return;
[Front] [Cd]

Theres all different values for elem.options[elem.selectedIndex].value which display different combinations of the 'parts' array.

So... whats going on with all that ???
And what does the #<<# in the 'parts' array mean/do ?

As you can see, I dont know too much about Javascript (im more of a simple PHP and VB programmer).
The attach file is the javascript and a clip of the form that calls the script (so you can see it in action).

Heres what im doing (if you care :) ).
I have a VB program ive made for myself and a friend.
With-in this program i 'Inet' a url and then i phase through the HTML source and pull out the info we want, then display that info in a listview. Noramly part of that info would work with the javascript, but im converting that to function under VB. Im not looking for the "javascript => VB" code here, i would just like to know excatly how the code above translates the numbers into the words and what the #<<# does.


Hope that made sense, thanks for any help.

rm-f
09-22-2005, 03:43 PM
And what does the #<<# in the 'parts' array mean/do ?

x << y is binary left shift (shift x, y times to the left)

as result:
parts.front == 1
...
...
...
parts.inlay == 128

here it is as 2^n (2^0 == 1, 2^7 == 128)

Misspell
09-23-2005, 08:34 AM
Thank you so much, rm-f.

Like i said... im a simple programer so i had to do alittle diggin to get an understanding of Binary, but now i know enough to get me over this hump; thank you.

This link helped me alot, maybe it will help others...
http://www.devarticles.com/c/a/JavaScript/The-Power-of-Javascript-Operators-continued/

You have saved me so much time and typing.
As of now my VB app has a bunch of IF Statments that check the 'titleCode' ID then manuly (hard-coded) the result. Now im gonna change it to a ID -> Binary convertion and then pull the info (Front, CD, Inside, Inlay) out of an array.

Thanks again !



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum