This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Javascript: 101

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Code Samples

Go back to: General discussion

Hi allindecision,

Anyone know where is the best place to get good code samples to review?

Thanks

Maya Incaand's picture
Maya Incaand
Thu, 2011-01-20 21:40

Thanks Jakub

Plenty there, although I am not sure which is good or not.

Jakub Pocentek's picture
Jakub Pocentek
Thu, 2011-01-20 22:41

There is a column named level/Author. You can check there if it's novice, intermidiate etc.

Nick's picture
Nick
Thu, 2011-01-20 18:53

I've recently discovered Github but haven't yet had the time to dig into it. It appears to be a shared/open source repository of code and projects. You can even contribute to projects if you're so inclined.

Anyone else on here with more knowledge than I please correct or elaborate.

Thx,
n

Maya Incaand's picture
Maya Incaand
Thu, 2011-01-20 21:44

Hi Nick

Signed up there, not sure why, LOL...

I was looking around for an editor (I know Netbeans for Java but seems a bit heavy for Javascript).

Anyway, came across http://www.ixedit.com/ - amazing, prebuilt inpage(!) designer.

Must be good because even I could figure it out....

Melipone Moody's picture
Melipone Moody
Thu, 2011-01-27 19:07

I don't know about editors but I was looking for a javascript shell and I found Rhino and SpiderMonkey. Any pros and cons? Rhino is java-based and seems easy enough to install ...

ozzie sutcliffe's picture
ozzie sutcliffe
Thu, 2011-01-27 19:38

java can be memory hog ..
You should not need a JS shell for this class..
For sure not in the lite class

Parag Shah's picture
Parag Shah
Thu, 2011-01-27 19:48

I agree with Oz. We do not need a shell for this course.

The console in Eloquent Javascript is very nice and should suffice for most things. JSFiddle also has a good environment for running Javascript programs.

ozzie sutcliffe's picture
ozzie sutcliffe
Thu, 2011-01-27 19:56

Also the shell has some limitations as its not a browser , so in the end you still need to put your script in a web page .

Edoardo Batini's picture
Edoardo Batini
Sat, 2011-01-29 14:37

I use the console given by Google Chrome developer tools (similar to FireBug for Firefox). There are even command line console on Mozilla site, I suggest this https://developer.mozilla.org/en/Rhino/Downloads_archive

Parag Shah's picture
Parag Shah
Fri, 2011-01-21 06:53

Nick,

Git is a distributed version control system. GitHub is a web based service which allows users to host their source code on GitHub's Git repository. When you use the free account your code is automatically available in the public domain.

GitHub is used by many people who create open source software as well as companies who don't. GitHub also promotes the concept of social coding, because users can follow code commits made by other users and even fork other's projects.

Sorry if I introduced too many new concepts :-)

To use GitHub you will need a working knowledge of Git. Since most people here will be working on the code individually, a very deep knowledge of Git is not required. You should be able to read a basic Git tutorial (I am sure GitHUb will also have some tutorials) and get started.

--
Regards
Parag

Ivan Teoh's picture
Ivan Teoh
Fri, 2011-01-21 00:00

JQuery (http://jquery.com/) is a good JavaScript library code to review. Here is an article on 10 Things I Learned from the jQuery Source. http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/

Melipone Moody's picture
Melipone Moody
Thu, 2011-01-27 19:07

What exactly is jquery? Do we need it for the course?

Parag Shah's picture
Parag Shah
Thu, 2011-01-27 19:46

Melipone, JQuery is a Javascript library for creating web apps. It does a whole lot of things, including manipulating the DOM.

No we do not need it for this course. I believe someone is thinking of offering a separate JQuery course next semester.

Darth Binamira's picture
Darth Binamira
Sat, 2011-01-22 16:03

Hi Maya,

I would not recommend w3schools at the moment. You can look in here instead to see why this is so: http://w3fools.com/

This is a note for everyone too.

Cheers,
Darth

Maya Incaand's picture
Maya Incaand
Sat, 2011-01-22 16:22

Hi Darth

Thanks for the headsup, will stay away from that one, the link you gave had some other good sources so I will use them instead.

Maya Incaand's picture
Maya Incaand
Sat, 2011-01-22 20:32

Darth,

I am doing the php course as well and I just noticed that the course organizer is providing w3schools as a principal reference....

Jason Lydon's picture
Jason Lydon
Sat, 2011-01-22 21:49

I pretty sure it is dependent on the discipline. For JS, not so much...

Darth Binamira's picture
Darth Binamira
Thu, 2011-01-27 14:38

Maya,

I'm not sure about their other offerings. I don't know much about PHP, but I do recall they have a manual available online. You can always counter check on that..

Good luck!

Ronald Sahagun's picture
Ronald Sahagun
Sun, 2011-01-23 01:31

Oh wow, I've been using w3schools to study Javascript. I'm gonna check out that w3fools website. Thanks.

Michelle Flinchbaugh's picture
Michelle Flinchbaugh
Thu, 2011-01-27 13:03

I was playing with an example int he Eloquent Javascript book using a while statement and wanted to make it display number 1-10 but get 1-11 and am wondering how to make it stop at 10. Here is my code:

var total = 0, count = 1;
document.write (count);
document.write ( "," ) ;
while (count <= 10)

{
total += count;
count += 1;
document.write (count);
document.write ( "," ) ;
}

Amene Katanda's picture
Amene Katanda
Thu, 2011-01-27 13:09

try this : while (count<=9) or while (count<10). remember the loop runs once more before stopping.

Darth Binamira's picture
Darth Binamira
Thu, 2011-01-27 14:30

you are printing the value of count before it is incremented in the loop. and your while condition loops 10 times, that is why you are getting 1-11.

another way would also be doing all the "document.write(count)" inside the loop.
try this:

var total = 0, count = 1;
while (count <= 10)
{
// print the count variable first
document.write(count);
document.write(",");

total += count;

// increment the count variable
count += 1; // can also be written as "count++" or "++count"
}

Parag Shah's picture
Parag Shah
Thu, 2011-01-27 14:38

The reason why this is happening:
When the value of 'count' is 10, the program is able to enter the while condition, because, 10 <= 10. The value of count is incremented inside the loop, so it becomes 11, which is printed.

On the following run, the program does not enter the while condition, because 11 <= 10 is not true.

You must either stop the while condition at 9, or increment after printing.

Amene Katanda's picture
Amene Katanda
Thu, 2011-01-27 13:08

try this : while (count<=9) or while (count<10). remember the loop runs once more before stopping.

Michelle Flinchbaugh's picture
Michelle Flinchbaugh
Thu, 2011-01-27 17:00

Thank you all for your help.

Parag Shah's picture
Parag Shah
Thu, 2011-01-27 18:33

Hi Michelle,

We also have QA forums for every week of the course on this page: http://webcraftp2p.appspot.com/courses/course/javascript101p2pujanuary2011/

You can also use these forums if you wish.

Ronald Sahagun's picture
Ronald Sahagun
Fri, 2011-01-28 08:08

I don't know if this is the right place to ask this, but is there a difference where you put the left bracket that is the start of a function, like:

function message() {
somethingsomething
}

Is this different from:

function message()
{
somethingsomething
}

I'm just dying to know if there is any major difference, and if it affects your website in any way.

Amene Katanda's picture
Amene Katanda
Fri, 2011-01-28 08:35

Well no. There is absolutely no difference. Javascript doesn't read white spaces. Please feel free to use any of those - whatever is more readable to you.

Parag Shah's picture
Parag Shah
Fri, 2011-01-28 11:30

There is no difference in where you place the {}.

Usually it is a matter of coding conventions, which the individual, or team, or company follows. It is a good idea to be consistent in where the brackets are placed, and follow conventions of the team if you are working with others.

You can also ask the questions on the weekly forums. The forum for the first week of the course is here: http://webcraftp2p.appspot.com/courses/course/javascript101p2pujanuary20...

Michelle Flinchbaugh's picture
Michelle Flinchbaugh
Sat, 2011-01-29 00:18

I'm not getting the \n (backslash n) to work to create a new line. I was trying here: http://userpages.umbc.edu/~flinchba/2tothe10th.html and then made a file just with that breaking some text here: http://userpages.umbc.edu/~flinchba/newline.html and that didn't work either. This looked and should have been very simple and I just don't know why it doesn't make a new line the way it's supposed to?

wim sun's picture
wim sun
Sat, 2011-01-29 02:50

Hi Michelle,
wanted to try and help so - regarding to the second text link - first line second line,
with the
>>alert ("text \n text")
will break the lines in a pop up
or like an html
>>document.write("Line 1
Line2
Line3");
hope i am helping. new here myslef

wim sun's picture
wim sun
Sat, 2011-01-29 03:08

oops where did the html
break gone ?
there suppose to be --- line 1
line 2
....

lol - i guess it is working since it appears to be in new lines
the html break is < b r > with no spacing in the <>
so write --- line1 < b r > line2 < b r > and so on

wim sun's picture
wim sun
Sat, 2011-01-29 03:04

oops where did the html
break gone ?
there suppose to be --- line 1
line 2
....